Explain why scrollTop() always returns 0 here

后端 未结 3 507
梦毁少年i
梦毁少年i 2021-02-10 03:13

See http://codepen.io/anon/pen/NGqPNz

CSS:

html {
  height: 100%;
  overflow-y: hidden;
}
body {
  height: 100%;
  overflow-y: auto;
}

相关标签:
3条回答
  • 2021-02-10 03:18

    Try to change "height: 100%;" by "height: 100vh;"

    0 讨论(0)
  • 2021-02-10 03:21

    Thanks to Shikkediel's comment, it appears to be a Webkit bug. If you put a div immediately inside the body, and bind the scroll event to the div, it works.

    http://codepen.io/anon/pen/bVderq

    CSS:

    html {
      height: 100%;
      overflow-y: hidden;
    }
    body {
      height: 100%;
      overflow-y: hidden;
    }
    .scroll-wrapper {
      height: 100%;
      overflow-y: auto;
    }
    

    JS:

    $('.scroll-wrapper').bind("scroll", function () {
      if ($('.scroll-wrapper').scrollTop()) {
        console.log('triggered!');
        console.log($('.scroll-wrapper').scrollTop());
      } else {
        console.log($('.scroll-wrapper').scrollTop());
      }
    });
    
    0 讨论(0)
  • 2021-02-10 03:44

    It's because you have overflow-y: hidden;. Remove this and then scrollTop() will work

    0 讨论(0)
提交回复
热议问题