Advertisement
Guest User

Untitled

a guest
Jan 31st, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. <!-- element we are going to animate -->
  2. <div id="box"> Ninja </div>
  3.  
  4. <script type="text/javascript">
  5.  
  6. function animateIt(elementId) {
  7.  
  8. //inside the animateIt() function, we get a reference to that element.
  9. var elem = document.getElementById(elementId);
  10. //establishes a counter to keep track of animation ticks (steps)
  11. var tick = 0;
  12.  
  13. //creates and starts an interval timer given a callback function that will be invoked every 10ms.
  14. //For 100 ticks, it will adjust the position of the element.
  15. var timer = setInterval(function() {
  16. if (tick < 100) {
  17. elem.style.left = elem.style.top = tick + "px";
  18. tick++;
  19. }
  20. else {
  21. clearInterval(timer);
  22. assert(tick == 100, "Tick is accessed via a closure");
  23. assert(elem, "Element also accessed via closure");
  24. assert(timer, "Timer reference also obtained via closure");
  25. }
  26. }, 10);
  27. }
  28.  
  29. animateIt('box');
  30. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement