Last active
August 29, 2015 14:12
-
-
Save JSila/b87d25076cb3798950ec to your computer and use it in GitHub Desktop.
Displaying multiple blocks alternately with fadeIn/fadeOut
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
// 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