JSFiddle - React, Tailwind, and code Playground
HTML
<div class='a'></div>
CSS
div.a {
width: 50px;
height:50px;
background-color:red;
position:fixed;
}
JavaScript
$(document).ready(function(){
animateDiv();
});
function makeNewPosition(){
// Get viewport dimensions (remove the dimension of the div)
var h = $(window).height() - 50;
var w = $(window).width() - 50;
var nh = Math.floor(Math.random() * h);
//var nw = Math.floor(Math.random() * w);
var nw = 88;
return [nh,nw];
}
function animateDiv(){
var newq = makeNewPosition();
$('.a').animate({ top: newq[0], left: newq[1] }, function(){
animateDiv();
});
};