Fade transition plugin for jQuery
Monday, May 25th, 2009Update
After requests from Juanma and Manic, below is a modified version of the plugin which demonstrates custom navigation:
All source code is completed untested on any browser except FF3.5, and comes without warranty of any kind. Source code is contained within this file
I’ve been a huge fan of jQuery for a few years now, and it is incorporated in most of my online projects. I’ve never created a plugin for jQuery though, so I thought that maybe I should try my hand at it. My attempt builds on some code I wrote quite a while ago to transition a sequence of images (or any HTML elements, for that matter) by fading out one image, and fading in another. There are plenty of other plugins which achieve the same effect (and are probably better), but feel free to experiment with this one.
There’s really not much source code to this plugin:
(function ($) {
$.fn.fadeTransition = function(options) {
var options = $.extend({pauseTime: 5000, transitionTime: 2000, ignore: null, delayStart: 0, pauseNavigation: false}, options);
var transitionObject;
Trans = function(obj) {
var timer = null;
var current = 0;
var els = (options.ignore)?$("> *:not(" + options.ignore + ")", obj):$("> *", obj);
$(obj).css("position", "relative");
els.css("display", "none").css("left", "0").css("top", "0").css("position", "absolute");
if (options.delayStart > 0) {
setTimeout(showFirst, options.delayStart);
}
else
showFirst();
function showFirst() {
if (options.ignore) {
$(options.ignore, obj).fadeOut(options.transitionTime);
$(els[current]).fadeIn(options.transitionTime);
}
else {
$(els[current]).css("display", "block");
}
}
function transition(next) {
$(els[current]).fadeOut(options.transitionTime);
$(els[next]).fadeIn(options.transitionTime);
current = next;
cue();
};
function cue() {
if ($("> *", obj).length < 2) return false;
if (timer) clearTimeout(timer);
if (!options.pauseNavigation) {
timer = setTimeout(function() { transition((current + 1) % els.length | 0)} , options.pauseTime);
}
};
this.showItem = function(item) {
if (timer) clearTimeout(timer);
transition(item);
};
cue();
}
this.showItem = function(item) {
transitionObject.showItem(item);
};
return this.each(function() {
transitionObject = new Trans(this);
});
}
})(jQuery);
Usage is also quite straightforward. Include jQuery, and the plugin source, and use this:
$(document).ready(function() { $(".container").fadeTransition(); });
There are a couple of options to configure the animation properties:
$(".container").fadeTransition({pauseTime: 5000, transitionTime: 2000, ignore: "#introslide", delayStart: 3000, pauseNavigation: true});
pauseTime is the number of milliseconds each child of .container will be displayed for.
transitionTime is the number of milliseconds the fade in / fade out animation will last for.
ignore is a jquery selector that excludes a particular element from being included in the animation cycle. This is useful for showing an "introductory" slide
delayStart is a time in milliseconds that must elapse before the animation begins
pauseNavigation only allows the fade transition to occur when the user clicks on the navigation buttons
See also: DHTML Fade Effect