Home > Web Front-end > JS Tutorial > body text

A small example of javascript jittering elements_javascript skills

WBOY
Release: 2016-05-16 17:18:36
Original
771 people have browsed it

复制代码 代码如下:

 
 
 
    
     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>
 
 

    


    

 

 
 

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template