[英]Stop load iframe again after scroll?
I have a page with an iframe... Currently i used this code to load iframe onscroll.... 我有一个包含iframe的页面...目前,我使用此代码加载iframe滚动广告...。
Javascript: 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: HTML:
<iframe data-src='http://some-link.com' src='' class='lazy'/>
But when I scroll again even if only slightly, the iframe is re-loaded again. 但是,即使我只是稍微滚动一次,iframe也会重新加载。 Can anyone help me to create iframe is not loaded again after i scroll for the second time? 谁能帮我创建第二次滚动后不再加载iframe? Thank you! 谢谢!
DEMO: http://design-jarwo.blogspot.co.id/ and i'll use it on my blog www.kodejarwo.com 演示: http : //design-jarwo.blogspot.co.id/ ,我将在我的博客www.kodejarwo.com上使用它。
You must remove the scroll event listener after firing it: 触发后,必须删除滚动事件侦听器:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.