首页 >web前端 >js教程 >javascript抖动元素的小例子_javascript技巧

javascript抖动元素的小例子_javascript技巧

WBOY
WBOY原创
2016-05-16 17:18:36787浏览

复制代码 代码如下:

 
 
 
    
     xxxxxx
    
     <script><BR> function shake(e, onComplete, distance, interval)<BR> {<BR> if (typeof e === "string")<BR> {<BR> e = document.getElementById(e);<BR> } // end if<BR> distance = distance || 8;<BR> interval = interval || 800; <P> var originalStyle = e.style.cssText;<BR> e.style.position = "relative";<BR> var start = (new Date()).getTime(); <P> animate(); <P> function animate()<BR> {<BR> var now = (new Date()).getTime();<BR> var elapsed = now - start;<BR> var progress = elapsed / interval;<BR> if (progress < 1)<BR> {<BR> var y = distance * Math.sin(Math.PI * progress * 4);<BR> var x = distance * Math.cos(Math.PI * progress * 4);<BR> e.style.left = x + "px";<BR> e.style.top = y + "px";<BR> console.log(e.style.cssText);<BR> setTimeout(animate, Math.min(25, elapsed));<BR> } // end if<BR> else<BR> {<BR> e.style.cssText = originalStyle;<BR> if (onComplete)<BR> {<BR> onComplete(e);<BR> } // end if <P> } // end else <P> } // end animate() <P> } // end shake() <P> </script>
 
 

    


    

 

 
 

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn