Skip to content

Instantly share code, notes, and snippets.

@jeffturcotte
Last active December 22, 2015 16:39
Show Gist options
  • Save jeffturcotte/6501188 to your computer and use it in GitHub Desktop.
Save jeffturcotte/6501188 to your computer and use it in GitHub Desktop.
Cycle 2 BG lookahead plugin
/*! Cycle2 lookahead plugin for backgrounds
* Copyright (c) 2013 M. Alsup; Dual licensed: MIT/GPL
* @author Jeff Turcotte <jeff@imarc.net>
*/
(function($) {
"use strict";
$(document).on( 'cycle-initialized', function(e, opts) {
var key = 'cycle-look-ahead';
opts.container.on( 'cycle-before', function( e, opts, outgoing, incoming, fwd ) {
var index = fwd ? (opts.nextSlide + 1) : (opts.nextSlide - 1),
slide = $( opts.slides[ index ] ),
images;
if ( slide.length && ! slide.data( key ) ) {
slide.data( key, true );
backgrounds = slide.find('[data-cycle-bg]');
if (slide.is('[data-cycle-bg]')) {
backgrounds.add(slide);
}
backgrounds.each(function() {
var elem = $(this);
elem.css('background-image', elem.attr('data-cycle-bg'));
elem.removeAttr( 'data-cycle-bg' );
});
}
});
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment