Home >Web Front-end >JS Tutorial >A small example of javascript jittering elements_javascript skills

A small example of javascript jittering elements_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:18:36786browse

复制代码 代码如下:

 
 
 
    
     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> <p>         var originalStyle = e.style.cssText;<br>         e.style.position = "relative";<br>         var start = (new Date()).getTime();</p> <p>         animate();</p> <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> <P>             } // end else</P> <P>         } // end animate()</P> <P>     } // end shake()</P> <P>     </script>
 
 

    


    

 

 
 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Js operation Select encyclopedia (value, setting selection, etc.)_javascript skillsNext article:Js operation Select encyclopedia (value, setting selection, etc.)_javascript skills

Related articles

See more