检测从顶部 jquery 滚动的距离
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2021440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
detect distance scrolled from top jquery
提问by Jorre
How can I detect the number of pixels scrolled in a browser window? I need this to dynamically adjust the height of a 100% height div...
如何检测浏览器窗口中滚动的像素数?我需要这个来动态调整 100% 高度 div 的高度...
I'm using jQuery.
我正在使用 jQuery。
EDIT: I cannot just use scrollTop() because I'm working with a 100% height div with overflow set to auto. Firefox does not detect browser scrolling due to this, the only thing scrolling is a 100%x100% div...
编辑:我不能只使用 scrollTop(),因为我正在使用 100% 高度 div,溢出设置为自动。由于这个原因,Firefox 没有检测到浏览器滚动,唯一滚动的是 100%x100% div...
采纳答案by Jorre
Allright guys, I found it:
好吧伙计们,我找到了:
$("div#container").scroll(function() {
var screenheight = parseInt($(document).height());
var scrolledpx = parseInt($("div#container").scrollTop());
var sum = screenheight+scrolledpx;
console.log($("div#container").scrollTop());
console.log("screen: " + screenheight);
console.log("sum=" + sum);
$("div.content").height(sum);
})
回答by David Hellsing
use $(document).scrollTop()
:
使用$(document).scrollTop()
:
$(document).scroll(function() {
console.log($(document).scrollTop());
})
回答by thecraighammond
You can use scrollTop() to find out how far down the page you've traveled.
您可以使用 scrollTop() 找出您在页面下方移动了多远。
$(window).scroll(function() {
console.log($(window).scrollTop());
if ($(window).scrollTop() > 200) {
$('#div').stop().animate({
'marginTop': $(window).scrollTop() + 'px',
'marginLeft': $(window).scrollLeft() + 'px'
}, 'slow');
}
});