JSFiddle - React, Tailwind, and code Playground

by Tushar Jadhav

HTML

<h3 id="success"></h3>

<div id="box"></div>

CSS

#box {
    width: 50px;
    height:50px;
    background-color:red;
    position:fixed;
}

JavaScript

var div = $('#box');
$(document).ready(function () {
    Animate();

    div.on('mousedown', function () {
        // Code here
        console.log('clicked');
    });
});

function GetNewPosition() {
    // Get dimentions of the window and remove the div area
    var h = $(window).height() - 50;
    var w = $(window).width() - 50;
    // New height and width is auto generated
    var nh = Math.floor(Math.random() * h);
    var nw = Math.floor(Math.random() * w);
    return [nh, nw];
}

function Animate() {
    var _newPosition = GetNewPosition();
    // Sets new position of div
    div.animate({
        top: _newPosition[0],
        left: _newPosition[1]
    }, (Math.floor(Math.random() * 5000) + 1), Animate);
}