detect distance scrolled from top jquery

2020-02-08 02:48发布

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...

I'm using 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...

标签: jquery
3条回答
萌系小妹纸
2楼-- · 2020-02-08 03:30

use $(document).scrollTop() :

$(document).scroll(function() {
    console.log($(document).scrollTop());
})
查看更多
相关推荐>>
3楼-- · 2020-02-08 03:38

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);
})
查看更多
我只想做你的唯一
4楼-- · 2020-02-08 03:47

You can use scrollTop() to find out how far down the page you've traveled.

$(window).scroll(function() {
  console.log($(window).scrollTop());
  if ($(window).scrollTop() > 200) {
    $('#div').stop().animate({
      'marginTop': $(window).scrollTop() + 'px',
      'marginLeft': $(window).scrollLeft() + 'px'
    }, 'slow');
  }
});
查看更多
登录 后发表回答