Skip to content

Instantly share code, notes, and snippets.

@JSila
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JSila/b87d25076cb3798950ec to your computer and use it in GitHub Desktop.
Save JSila/b87d25076cb3798950ec to your computer and use it in GitHub Desktop.
Displaying multiple blocks alternately with fadeIn/fadeOut
// inspired by http://stackoverflow.com/a/6033994
// for better visual performance hide fading elements via css not via js
$.fn.fadeAlternately = function (options) {
var options = options || {};
var defaults = {
timeOn: 500,
timeOff:100,
fadeInSpeed:1500,
fadeOutSpeed:1500
};
var faderIndex = 0,
self = this,
options = $.extend({}, defaults, options);
(function nextFade() {
$(self[faderIndex++])
.delay(options.timeOn)
.fadeOut(options.fadeOutSpeed, function() {
faderIndex %= self.length;
$(self[faderIndex])
.delay(options.timeOff)
.fadeIn(options.fadeInSpeed, nextFade);
});
})();
}
// usage example
//$(document).ready(function() {
// $('#titles h1').fadeAlternately();
//});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment