每当用户滚动页面时,我希望在页面底部放置一个元素。它类似于“固定位置”,但我不能使用“位置:固定”css,因为我的许多客户的浏览器都不支持它。
我注意到jquery可以获取当前视区的顶部位置,但是如何获取滚动视区的底部呢?
所以我在问如何知道:$(window).scrollBottom()
发布于 2011-01-11 15:47:11
var scrollBottom = $(window).scrollTop() + $(window).height();
发布于 2011-05-03 22:03:42
我想说,作为scrollTop的直接对立面,scrollBottom应该是:
var scrollBottom = $(document).height() - $(window).height() - $(window).scrollTop();
这是一个对我有效的小而难看的测试:
// SCROLLTESTER START //
$('<h1 id="st" style="position: fixed; right: 25px; bottom: 25px;"></h1>').insertAfter('body');
$(window).scroll(function () {
var st = $(window).scrollTop();
var scrollBottom = $(document).height() - $(window).height() - $(window).scrollTop();
$('#st').replaceWith('<h1 id="st" style="position: fixed; right: 25px; bottom: 25px;">scrollTop: ' + st + '<br>scrollBottom: ' + scrollBottom + '</h1>');
});
// SCROLLTESTER END //
发布于 2015-05-08 23:04:08
将来,我已经将scrollBottom变成了一个jquery插件,它的使用方式与scrollTop相同(即,您可以设置一个数字,它将从页面底部滚动该数量,并从页面底部返回像素数,或者,如果没有提供数字,则从页面底部返回像素数)。
$.fn.scrollBottom = function(scroll){
if(typeof scroll === 'number'){
window.scrollTo(0,$(document).height() - $(window).height() - scroll);
return $(document).height() - $(window).height() - scroll;
} else {
return $(document).height() - $(window).height() - $(window).scrollTop();
}
}
//Basic Usage
$(window).scrollBottom(500);
https://stackoverflow.com/questions/4655273
复制相似问题