检测滚动是否触及页面底部
Detect if scroll has touched bottom of page
这是我用来检测用户是否通过滚动到达页面底部的代码。
它在Chrome,Safari和Firefox中工作正常,但是在IE8中,当滚动到达页面底部时,showNextItems函数被调用两次。谁能确定为什么,以及如何解决它?
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == getDocHeight()) {
if ($('#digitalContent p:hidden').size() > 0) {
getAjaxLoader($('.loader'), false);
getAjaxLoader($('.loader'), true);
window.setTimeout(function() {
getAjaxLoader($('.loader'), false);
showNextItems('');
}, 1500);
}
}
});
function getDocHeight() {
var D = document;
return Math.max(
D.body.scrollHeight, D.documentElement.scrollHeight,
D.body.offsetHeight, D.documentElement.offsetHeight,
D.body.clientHeight, D.documentElement.clientHeight
);
}
你可以做:
function isBottom(m, wh) {
if (m === "init") {
if (wh === "off") $(window).off("scroll.isBottom");
else if (wh === "on") {
$(window).on("scroll.isBottom", function () {
console.log(isBottom("scroll"));
});
}
} else if (m === "scroll") {
var isIt = $(document).height() - ($(window).scrollTop() + $(window).height()) <= 0;
isBottom("init", "off");
setTimeout(function () {
isBottom("init", "on");
}, 100);
return isIt;
}
}
$(document).ready(function () {
isBottom("init", "on");
});
演示:
只有在页面底部时才应在IE8中登录true
。
function isBottom(m, wh) {
if (m === "init") {
if (wh === "off") $(window).off("scroll.isBottom");
else if (wh === "on") {
$(window).on("scroll.isBottom", function () {
console.log(isBottom("scroll"));
});
}
} else if (m === "scroll") {
var isIt = $(document).height() - ($(window).scrollTop() + $(window).height()) <= 0;
isBottom("init", "off");
setTimeout(function () {
isBottom("init", "on");
}, 100);
return isIt;
}
}
$(document).ready(function () {
isBottom("init", "on");
});
div {
height: 2000px;
background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div></div>
相关文章:
- 将视口底部滚动到元素底部
- 固定位置菜单时滚动,直到它击中一个相对容器的底部
- javascript跨浏览器确定用户是否滚动到页面底部
- javascript移动交叉浏览器确定用户是否滚动到页面底部
- 如何防止firefox在打开大型弹出窗口时滚动到页面底部
- 滚动到顶部或底部后的简单效果
- jQuery滚动到聊天框底部
- 如何仅在向上滚动时将页脚粘贴到窗口底部会导致页脚不显示
- 页面加载时自动滚动到底部
- 当滚动条到达容器底部时设置动画
- 表-滚动到底部
- 滚动至页面底部附近
- 刷新滚动到底部
- 在底部滚动
- jQuery在页面底部滚动时加载更多内容
- 自定义 DNN 皮肤无法检测到底部滚动,但它可以反向工作 (7.1.1)
- 如何从css中的UI中完全删除底部滚动条
- “看来More"按钮后每三个底部滚动使用jquery
- 无法可靠地检测 iPhone 纵向和横向模式之间的底部滚动
- AmCharts - 位置图表底部滚动条