Created
October 12, 2017 18:57
-
-
Save kel/4f4e77105ea66f0d493166a4a3ab6508 to your computer and use it in GitHub Desktop.
Simple way to lazyload iframes.
This file contains 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
/** | |
* 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') | |
}) |
This file contains 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
/** | |
* 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