Skip to content

Instantly share code, notes, and snippets.

@thisislawatts
Created July 23, 2013 11:56
Show Gist options
  • Save thisislawatts/6061813 to your computer and use it in GitHub Desktop.
Save thisislawatts/6061813 to your computer and use it in GitHub Desktop.
Vine Lazy Loader
/**
*
* Vine - Lazy Load Wrapper
*
*
* @requires jQuery
*/
jQuery(document).ready(function($) {
var $vine = [],
window_h = window.innerHeight,
window_scroll;
/**
* Set up our array of jQuery objects for fast iteration
* Also performs a quick set up
* - Wrap in styleable element
* - Sets data-loaded as false
*/
$('iframe[data-src*="vine"]').each(function() {
var $el = $(this);
$el.wrap('<div class="vine--wrapper"></div>').data('loaded', false);
$vine.push($el);
});
function checkVineLoad() {
var offset_top = 0;
window_scroll = $(window).scrollTop();
for ( var i = 0, l = $vine.length; i < l; i++ ) {
// Quick bailout for loaded videos
if ($vine[i].data('loaded')) continue;
offset_top = $vine[i].offset().top;
if ( offset_top > window_scroll && offset_top < window_scroll + window_h ) {
$vine[i].attr('src', $vine[i].data('src')).data('loaded', true);
}
}
}
checkVineLoad();
$(window).on('scroll', checkVineLoad );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment