Explain why scrollTop() always returns 0 here

后端 未结 3 508
梦毁少年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: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());
      }
    });
    

提交回复
热议问题