Skip to content

Instantly share code, notes, and snippets.

@yuchant
Created February 11, 2012 05:51
Show Gist options
  • Save yuchant/1796790 to your computer and use it in GitHub Desktop.
Save yuchant/1796790 to your computer and use it in GitHub Desktop.
jQuery cross fade..
/*
Cross Fade Any Element
Version 0.01
By Yuji Tomita
February 10, 2012.
*/
(function( $ ){
$.fn.crossFadeTo = function( fadeToElement, speed, options) {
defaultOptions = {
wrap: false,
background: false,
css: {}
}
if (typeof options == 'object') {
options = $.extend(defaultOptions, options);
} else {
options = defaultOptions;
};
var data = this.data('crossFadeTo');
if (options['wrap']) {
$fadeToElement = $(options['wrap']).html(fadeToElement);
} else {
var $fadeToElement = $(fadeToElement);
}
if (data == undefined) {
data = {};
};
if (!data['initialized']) {
var $relative = $('<div class="cf-relative" />').css({ position: 'relative', width: '100%', height: '100%'});
var faderCSS = { position: 'absolute', left: '0px', top: '0px', width: '100%', height: '100%'};
faderCSS = $.extend(faderCSS, options['css']);
var $fader = $('<div class="cf-fader cf-fader-element"/>').css(faderCSS);
var $main = $('<div class="cf-main cf-fader-element"/>').css(faderCSS).css('z-index', '10');
$relative.append($fader).append($main);
$main.html(this.html());
this.html($relative);
data['initialized'] = true;
} else {
var $fader = this.find('.cf-relative > .cf-fader:first');
var $main = this.find('.cf-relative > .cf-main:first');
};
$fader.html($main.html());
$main.hide().html($fadeToElement).stop(true, true).fadeIn(speed || 300);
this.data('crossFadeTo', data);
return this;
};
var cachedAJAX = {};
$.cachedAJAX = function(params) {
// pull result from cache if possible.
return $.ajax(params);
// this doesn't work because what we need cached is what the successs callback does.
// -------------------
// var url = params['url'];
// if (cachedAJAX[url] != undefined) {
// console.log("Cache found - returning from cache");
// return cachedAJAX[url];
// };
// response = $.ajax(params);
// cachedAJAX[url] = response;
// return response;
};
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment