Created
October 5, 2012 09:40
-
-
Save lucasmotta/3838966 to your computer and use it in GitHub Desktop.
Play a sequence of images on rollover, using jquery.
This file contains 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
(function($) { | |
$.fn.imageSequence = function(options) { | |
var params = $.extend({ | |
interval: 100, | |
height: parseInt(this.find("li").css("height")), | |
skipFirst: false | |
}, options); | |
var interval, total, id = 0; | |
var current = null; | |
this.find("li:nth-child(3n+3)").addClass("last"); | |
this.find("img").each(function(id) { | |
this.onload = function() { | |
$(this).css("display", "block"); | |
}; | |
}); | |
function animate() { | |
id++; | |
id %= total; | |
if(params.skipFirst) { | |
if(id === 0) { | |
id = 1; | |
} | |
} | |
$(current).find(".images").css("margin-top", -params.height * id); | |
} | |
$("a").bind("mouseenter", function() { | |
current = $(this); | |
total = 0; | |
current.find("img").each(function(id) { | |
if (this.complete) { | |
total++; | |
} | |
}); | |
if (total <= 2) { | |
return; | |
} | |
id = 0; | |
interval = setInterval(animate, params.interval); | |
animate(); | |
}); | |
$("a").bind("mouseleave", function() { | |
clearInterval(interval); | |
id = 0; | |
total = 0; | |
$(current).find(".images").css("margin-top", -params.height * id); | |
}); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment