lazy load iframe (delay src http call) with jquery

后端 未结 6 2331
有刺的猬
有刺的猬 2020-12-05 05:13

I am looking for something similar to jQuery image lazy load plugin, but for iframes.

6条回答
  •  日久生厌
    2020-12-05 06:06

YouTube videos

For YouTube embedded video iframe more specifically: Lazy Load youtube video from iframe API

On Chromium 81, it also works on jsfiddle: https://jsfiddle.net/cirosantilli/3p0tk5nc but didn't work here on Stack Overflow. I'm not sure why. Removing the loading="lazy" makes no difference, so I think it fails for some other reason.

Keep in mind that embedding YouTube videos on a local HTML file also fails randomly to me which makes testing harder: Embed Youtube code is not working in HTML

document.getElementById('load-now').addEventListener('click', function(){
  for (const img of document.getElementsByTagName('iframe')) {
    img.loading = 'eager';
  }
});
.separator {
    height: 1000px;
    width: 100px;
    border: 5px solid red;
}
img {
    height: 340px;
    border: 5px solid black;
}
  #load-now {
  border: 5px solid black;
  }
Click me to load all images now!

提交回复
热议问题