我想在滚动到页面底部时弹出提示,类似于这样:
$(function(){
$(document).scroll(function() {
if($(document).scrollTop() == 0) alert("top");
})
})
但是不使用 jQuery,在滚动到底部时弹出提示框。
我想在滚动到页面底部时弹出提示,类似于这样:
$(function(){
$(document).scroll(function() {
if($(document).scrollTop() == 0) alert("top");
})
})
但是不使用 jQuery,在滚动到底部时弹出提示框。
document.addEventListener('scroll', function (event) {
if (document.body.scrollHeight ==
document.body.scrollTop +
window.innerHeight) {
alert("Bottom!");
}
});
JSFiddle在这里:http://jsfiddle.net/cSer6/
if(window.addEventListener){
window.addEventListener('scroll',scroll)
}else if(window.attachEvent){
window.attachEvent('onscroll',scroll);
}
function scroll(ev){
var st = Math.max(document.documentElement.scrollTop,document.body.scrollTop);
if(!st){
console.log('top');
}else if((st+document.documentElement.clientHeight)>=document.documentElement.scrollHeight ){
console.log('bottom');
}
}
document.addEventListener=document.addEventListener || document.attachEvent; document.addEventListener('scroll',function(ev){ if ( document.body.scrollTop === document.body.height ) { console.log('Bottom'); } });
但是我无法检测到滚动条是否到达文档底部。 - user1305760Vivaldi
中进行了测试,有时候这个方法可以很好地工作,但是有时候st+document.documentElement.clientHeight
会比document.documentElement.scrollHeight
略小(1-小数部分)
,并且控制台不会打印底部,所以我将其改为Math.ceil(st + document.documentElement.clientHeight)
,目前看来效果不错。 - towithdocument.onscroll = function() {
if(document.documentElement.scrollTop + window.innerHeight == document.documentElement.scrollHeight)
{
alert('bottom');
}
}
document.onscroll = function() {
if(!document.body.scrollTop){
alert('top');
}
}
JSFiddle 演示
.addEventListener('scroll', ...)
而不是.onscroll = ...
,因为它更灵活,避免与其他事件处理程序发生冲突。不幸的是,在IE≤8中无法使用,但是可以很容易地使用polyfill来实现。 - ephemientscrollTop
起源于 MSIE,所以应该可以工作。 - Dennisif (document.body.scrollHeight <= Math.ceil(window.pageYOffset + window.innerHeight)) {
function addEvent(node, type, callback) {
if('addEventListener' in node) {
node.addEventListener(type, callback, false);
} else {
node.attachEvent('on' + type, callback);
}
}
addEvent(window, 'scroll', (function() {
// https://developer.mozilla.org/en/DOM/window.scrollY#Notes
var stObj, stProp;
if('scrollY' in window) { // CSSOM:
// http://www.w3.org/TR/cssom-view/#extensions-to-the-window-interface
stObj = window;
stProp = 'scrollY';
} else if('pageYOffset' in window) { // CSSOM too
stObj = window;
stProp = 'pageYOffset';
} else {
stObj = document.documentElement.clientHeight ?
document.documentElement : document.body;
stProp = 'scrollTop';
}
var node = document.documentElement.clientHeight ?
document.documentElement :
document.body; // let's assume it is IE in quirks mode
var lastSt = -1;
return function(e) {
if(lastSt !== stObj[ stProp ] && // IE <= 8 fires twice
node.scrollHeight === stObj[ stProp ] + node.clientHeight) {
alert('bottom');
}
lastSt = stObj[ stProp ];
};
})());
它已经成功地在Firefox 11、Chrome 17、IE 9(X-UA-Compatible: 8, 7, 5)和Opera 11.60中进行了测试。
document.documentElement.scrollTop
始终为零。 - Saxoierif($(document).scrollTop() == 0) alert("top");
。我可以看到的是上限而不是下限。请编辑您的问题。 - Saxoier