gpt4 book ai didi

javascript - 滚动后再次停止加载 iframe 吗?

转载 作者:行者123 更新时间:2023-11-28 04:08:23 40 4
gpt4 key购买 nike

我有一个带有 iframe 的页面...目前我使用此代码来加载 iframe onscroll...

Javascript:

function lazyLoad() {
for (var e = document.getElementsByClassName("lazy"), t = 0; t < e.length; t++) isInViewport(e[t]) && (e[t].src = e[t].getAttribute("data-src"))
}
function isInViewport(e) {
var t = e.getBoundingClientRect();
return t.bottom >= 0 && t.right >= 0 && t.top <= (window.innerHeight || document.documentElement.clientHeight) && t.left <= (window.innerWidth || document.documentElement.clientWidth)
}
function registerListener(e, t) {
window.addEventListener ? window.addEventListener(e, t) : window.attachEvent("on" + e, t)
}
registerListener("load", lazyLoad), registerListener("scroll", lazyLoad);

HTML:

<iframe data-src='http://some-link.com' src='' class='lazy'/>

但是当我再次滚动时,即使只是轻微滚动,iframe 也会再次重新加载。谁能帮助我创建 iframe 在我第二次滚动后不再加载?谢谢!

演示:http://design-jarwo.blogspot.co.id/我将在我的博客上使用它 www.kodejarwo.com

最佳答案

您必须在触发滚动事件监听器后将其删除:

function onScroll () {
if (lazyLoad()) {
window.removeEventListener('scroll', onScroll);
}
}
function onLoad () {
lazyLoad();
}
function lazyLoad() {
var loaded = false;
for (var e = document.getElementsByClassName("lazy"), t = 0; t < e.length; t++) {
isInViewport(e[t]) && (e[t].src = e[t].getAttribute("data-src"));
loaded = true;
}
return loaded;
}
function isInViewport(e) {
var t = e.getBoundingClientRect();
return t.bottom >= 0 && t.right >= 0 && t.top <= (window.innerHeight || document.documentElement.clientHeight) && t.left <= (window.innerWidth || document.documentElement.clientWidth)
}
function registerListener(e, t) {
window.addEventListener ? window.addEventListener(e, t) : window.attachEvent("on" + e, t)
}
registerListener("load", onLoad), registerListener("scroll", onScroll);

关于javascript - 滚动后再次停止加载 iframe 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46499237/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com