Created
January 5, 2012 00:42
-
-
Save irace/1563083 to your computer and use it in GitHub Desktop.
JavaScript image animations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $.each([ | |
| { images: ['foo.jpg', 'foo2.jpg'], interval: 200 }, | |
| { images: ['bar.jpg', 'bar2.jpg'], interval: 300 } | |
| ], function(index, animation) { | |
| $('<img/>') | |
| .attr('src', animation.images[0]) | |
| .load(function () { | |
| var $img = $(this), count = 0; | |
| $img.unbind(); | |
| setInterval(function() { | |
| $img.attr('src', animation.images[count % animation.images.length]); | |
| count++; | |
| }, animation.interval); | |
| }) | |
| .appendTo('body'); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ | |
| { images: ['foo.jpg', 'foo2.jpg'], interval: 200 }, | |
| { images: ['bar.jpg', 'bar2.jpg'], interval: 300 } | |
| ].forEach(function(animation) { | |
| var img = document.createElement("img"); | |
| img.src = animation.images[0]; | |
| img.onload = function() { | |
| img.onload = null; | |
| var count = 0; | |
| setInterval(function() { | |
| img.src = animation.images[count % animation.images.length]; | |
| count++; | |
| }, animation.interval); | |
| }; | |
| document.body.appendChild(img); | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Attempt at implementing basic image animation in JavaScript with and without using jQuery. If there are better ways to do any of this, please let me know.