抖动图片2
JS
var aImg=document.getElementsByTagName("img"); for (var i=0;i<aImg.length;i++) { aImg[i].onclick=function(){ shake(this,'top'); } } function shake(obj,attr,fn){ var timer=null; var arr=[]; for(var i=20;i>0;i-=2){ arr.push(i,-i); } arr.push(0); var num=0; clearInterval(timer); timer=setInterval(function(){ var seep=parseInt(getStyle(obj,attr))+arr[num]; num++; obj.style[attr]=seep+"px"; if(num===arr.length){ clearInterval(timer); } },30) } function getStyle(obj,attr){ if(obj.currentStyle){ return obj.currentStyle[attr]; }else{ return getComputedStyle(obj)[attr]; } }