Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ElfhirDev/f4fbe5432c946d94cc7c to your computer and use it in GitHub Desktop.
Save ElfhirDev/f4fbe5432c946d94cc7c to your computer and use it in GitHub Desktop.
iframe lazy loading
function iframeLazyLoading(iframe_height) {
var iframes = $('iframe');
$(document).on("scroll", function(event) {
iframes.each(function(index) {
var iframe = $(this);
if ($(document).scrollTop() > index*iframe_height) {
if (iframe.attr('src') === undefined || iframe.attr('src') === '') {
iframe.attr('src', function() {
return iframe.data('src');
});
iframe.attr('data-src', function() {
var src = iframe.attr('src');
iframe.removeAttr('data-src');
return src;
});
}
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment