div中的Javascript无限滚动
作者:互联网
我正在尝试使用javascript创建无限加载页面.我发现了:How to do infinite scrolling with javascript only without jquery
我一直在玩链接到此jsfiddle页面的最后一个答案:http://jsfiddle.net/8LpFR/
document.addEventListener("scroll", function (event) {
checkForNewDiv();
});
var checkForNewDiv = function () {
var lastDiv = document.querySelector("#scroll-content > div:last-child");
var lastDivOffset = lastDiv.offsetTop + lastDiv.clientHeight;
var pageOffset = window.pageYOffset + window.innerHeight;
if (pageOffset > lastDivOffset - 10) {
var newDiv = document.createElement("div");
newDiv.innerHTML = "my awesome new div";
document.getElementById("scroll-content").appendChild(newDiv);
checkForNewDiv();
}
};
checkForNewDiv();
我将如何修改它以使滚动在div而不是整个页面中进行?如上,lastDivOffset和pageoffset会变成什么?
解决方法:
这是不创建任何新包装器分区的解决方案.
document.getElementById("scroll-content").addEventListener("scroll", function(event) {
var newDiv = document.createElement("div");
newDiv.innerHTML = "my awesome new div";
document.getElementById("scroll-content").appendChild(newDiv);
});
var checkForNewDiv = function() {
var lastDiv = document.querySelector("#scroll-content > div:last-child");
var maindiv = document.querySelector("#scroll-content");
var lastDivOffset = lastDiv.offsetTop + lastDiv.clientHeight;
var pageOffset = maindiv.offsetTop + maindiv.clientHeight;
if (pageOffset > lastDivOffset - 10) {
var newDiv = document.createElement("div");
newDiv.innerHTML = "my awesome new div";
document.getElementById("scroll-content").appendChild(newDiv);
checkForNewDiv();
}
};
checkForNewDiv();
标签:infinite-scroll,css,html,javascript,jquery 来源: https://codeday.me/bug/20191029/1960459.html