Skip to content

Instantly share code, notes, and snippets.

@jbuchbinder
Last active December 15, 2015 22:59
Show Gist options
  • Save jbuchbinder/5337199 to your computer and use it in GitHub Desktop.
Save jbuchbinder/5337199 to your computer and use it in GitHub Desktop.
jquery.bgImageTween.js Plugin
/* bgImageTween, a Jquery plugin for smooth background image transition loops.
Copyright (C) 2010 Toni Anzlovar
Certain changes and updates
Copyright (c) 2013 Jeff Buchbinder (github.com/jbuchbinder)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
(function( $ ){
$.fn.bgImageTween = function(images, staytime, fadetime) {
$(this).wrapInner('<div class="bgImageTweenfaderContent"></div>');
var content = $('.bgImageTweenfaderContent', this);
$(this).prepend('<div class="bgImageTweenfader"></div>');
var fader = $('.bgImageTweenfader', this);
var obj = this;
content.css('position', 'absolute');
fader.css('position', 'absolute');
fader.css('width', $(this).width() );
fader.css('height', $(this).height() );
fader.css('opacity', 0);
console.log("Binding to: "); console.log(this);
n = images.shift(); images.push(n);
$(this).css('backgroundImage', 'url('+n+')');
n= images.shift(); images.push(n);
fader.css('backgroundImage', 'url('+n+')');
function switcher() {
//$(obj).trigger("bg-change", [ images[0], 1 ]);
fader.delay(staytime);
fader.animate({opacity: 1},fadetime);
fader.queue(function(){
var o = images[(images.length - 1) % images.length];
n = images.shift(); images.push(n);
//$(obj).trigger("bg-change", [ n, 2 ]);
$(obj).trigger("bg-change", [ o, 2 ]);
fader.parent().css('backgroundImage', 'url('+n+")");
$(this).dequeue();
});
fader.delay(staytime);
fader.animate({opacity: 0},fadetime);
fader.queue(function(){
var o = images[(images.length - 1) % images.length];
n = images.shift(); images.push(n);
//$(obj).trigger("bg-change", [ n, 3 ]);
$(obj).trigger("bg-change", [ o, 3 ]);
fader.css('backgroundImage', 'url('+n+")", switcher());
$(this).dequeue();
});
};
switcher();
};
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment