首页 > 代码库 > js抖动

js抖动

效果


demo image dithering effect

代码

 
1
2
3
4
5
6
7
8
9
10
11
<img src="http://www.qqoq.net/wp-content/uploads/200100.jpg" onMouseover="shake(this,‘onmouseout‘)" >
<script type="text/javascript">
var typ = ["marginTop","marginLeft"],rangeN=10,timeout=20;
function shake(o,end){
    var range=Math.floor(Math.random()*rangeN);
    var typN=Math.floor(Math.random()*typ.length);
    o["style"][typ[typN]]=""+range+"px";
    var shakeTimer=setTimeout(function(){shake(o,end)},timeout);
    o[end]=function(){clearTimeout(shakeTimer)};
}
</script>