让图片跳舞的js代码

imgs=document.getElementsByTagName('img');
for(i=0;i<imgs.length;i++){    
    imgs[i].display='block';
    imgs[i].style.position='absolute';
}
t=0;
function foo(){   
    for(i=0;i<imgs.length;i++){        
        x=Math.sin((t+i*8)/180*Math.PI)*300+400;
        y=Math.cos((t+i*6)/180*Math.PI)*300+300;
        imgs[i].style.left=""+x+"px";
        imgs[i].style.top=""+y+"px";   
    }
    t+=1;
    setTimeout('foo()', 20);
}
foo();

将以上代码用firebug之类的扩展运行一下,或者将如下代码复制到地址栏,就可以让网页上的图片动起来。

1 thought on “让图片跳舞的js代码

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.