Skip to content

Instantly share code, notes, and snippets.

@kel
Created October 12, 2017 18:57
Show Gist options
  • Save kel/4f4e77105ea66f0d493166a4a3ab6508 to your computer and use it in GitHub Desktop.
Save kel/4f4e77105ea66f0d493166a4a3ab6508 to your computer and use it in GitHub Desktop.
Simple way to lazyload iframes.
/**
* Simple way to lazyload iframes.
*
* Add class of 'lazyload' to iframe and change
* the 'src' attribute to 'data-src'
*/
jQuery(function($) {
function isVisible(element) {
var element = element.getBoundingClientRect()
return element.top < window.innerHeight && element.bottom >= 0
}
$(window).on('scroll', function() {
$('.lazyload').each(function() {
if (isVisible($(this)[0])) {
$(this).attr('src', $(this).attr('data-src')).removeClass('lazyload')
}
})
}).trigger('scroll')
})
/**
* Simple way to lazyload iframes.
*
* Add class of 'lazyload' to iframe and change
* the 'src' attribute to 'data-src'
*/
jQuery(function(t){function n(t){return(t=t.getBoundingClientRect()).top<window.innerHeight&&t.bottom>=0}t(window).on("scroll",function(){t(".lazyload").each(function(){n(t(this)[0])&&t(this).attr("src",t(this).attr("data-src")).removeClass("lazyload")})}).trigger("scroll")});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment