Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.