jquery animation
HTML
<div class="image-wrap">
<img class="image" src="http://www.vistaar.com/wp-content/uploads/2011/05/dell-logo-100X100.gif">
</div>
CSS
body {
margin: 0;
height: 100%;
}
.image-wrap {
height: 100%;
position: absolute;
width: 100%;
}
.image {
display: block;
height: 100%;
left: 0;
max-height: 100px;
max-width: 100px;
position: fixed;
top: 0;
width: 100%;
}
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);
return [nh,nw];
}
function animateDiv(){
var newq = makeNewPosition();
var oldq = $('.image').offset();
var speed = calcSpeed([oldq.top, oldq.left], newq);
$('.image').animate({ top: newq[0], left: newq[1] }, speed, function(){
animateDiv();
});
};
function calcSpeed(prev, next) {
var x = Math.abs(prev[1] - next[1]);
var y = Math.abs(prev[0] - next[0]);
var greatest = x > y ? x : y;
var speedModifier = 0.1;
var speed = Math.ceil(greatest/speedModifier);
return speed;
}