Moving Divs

by Joel Parks

HTML

<div class='a'><img class="image" src="https://i.imgur.com/kxoRpPC.png" alt="" width="100" height="100"></div>
<div class='b'><img class="image" src="https://i.imgur.com/kxoRpPC.png" alt="" width="100" height="100"></div>
<div class='c'><img class="image" src="https://i.imgur.com/kxoRpPC.png" alt="" width="100" height="100"></div>
<div class='d'><img class="image" src="https://i.imgur.com/kxoRpPC.png" alt="" width="100" height="100"></div>
<iframe width="0" height="0" src="https://www.youtube.com/embed/4d0Ku_TpcW8?autoplay=1" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

CSS

div.a {
width: 100px;
height:100px;
position:fixed;
    
}

div.b {
width: 100px;
height:100px;
position:fixed;
    
}
div.c {
width: 100px;
height:100px;
position:fixed;
    
}
div.d {
width: 100px;
height:100px;
position:fixed;
    
}
.image {
    border-radius: 50%;
    position: absolute;
    top: 50%;
    left: 50%;
    -webkit-animation:spin 1s linear infinite;
    -moz-animation:spin 1s linear infinite;
    animation:spin 1s linear infinite;
}
@-moz-keyframes spin { 100% { -moz-transform: rotate(360deg); } }
@-webkit-keyframes spin { 100% { -webkit-transform: rotate(360deg); } }
@keyframes spin { 100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } }

JavaScript

$(document).ready(function(){
    animateDiv('.a');
    animateDiv('.b');
    animateDiv('.c');
    animateDiv('.d');
});

function makeNewPosition(){
    
    // Get viewport dimensions (remove the dimension of the div)
    var h = $(window).height() - 100;
    var w = $(window).width() - 100;
    
    var nh = Math.floor(Math.random() * h);
    var nw = Math.floor(Math.random() * w);
    
    return [nh,nw];    
    
}

function animateDiv(myclass){
    var newq = makeNewPosition();
    $(myclass).animate({ top: newq[0], left: newq[1] }, 1000,   function(){
      animateDiv(myclass);        
    });
    
};