Created
April 19, 2011 07:31
-
-
Save csytan/926966 to your computer and use it in GitHub Desktop.
Lazy loads videos as you scroll. Place the iframe src in a "data-embed" attribute. Use with jQuery.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function(){ | |
var vid_links = $('a.video'); | |
function showVideos(){ | |
if (!vid_links.length) return; | |
var window_bottom = $(window).scrollTop() + $(window).height() + 500; | |
vid_links.each(function(){ | |
var link = $(this); | |
if (link.offset().top < window_bottom){ | |
link.replaceWith( | |
'<iframe src="' + link.attr('data-embed') + '" class="video" frameborder="0"></iframe>'); | |
vid_links = vid_links.slice(1); | |
} | |
}); | |
} | |
showVideos(); | |
$(window).scroll(showVideos); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment