Div around edges

animate a div around edges of the window, with $

by CME64

HTML

<div></div>

CSS

div{
    background-color: red;
    position: absolute;
    height: 50px;
    width: 100px;
}

JavaScript

var elem = $('div:first');
var w = $(window).width()-elem.width();
var h = $(window).height()-elem.height();
var anim = [{left:w,top:0},
            {left:w,top:h},
            {left:0,top:h},
            {left:0,top:0}];
(function fn(i){
elem.animate(anim[i],1500,function(){
    setTimeout(fn((i+1)%4),1000);   
    });
})(0);