Calculate how much user has scrolled (incl margins & paddings)

泄露秘密 提交于 2019-12-25 03:04:12

问题


I'm stuck with a very annoying problem. I'm trying to create a script that changes the menu bar (CSS) when the user has X pixels scrolled (height of header element). However, any jQuery script I've tried doesn't calculates the scrolled distance correctly. After some pixels it stops counting, and continues.. and then resumes, stops, resumes, etc.

I think it may have something to do with padding and/or margins.. Anyway, I need some support.

Check out the current site, with a 'distance scrolled' script appended.

Script:

$(window).scroll(function() {
  var scrollTop = $(window).scrollTop();
  console.log(scrollTop);
});

回答1:


You've got two scrollbars going for a start. Add overflow: inherit; to your body's css to solve this.

DEMO

This should do roughly what you want. At 120px scrolled down, a header slides down overtop the content.

If this doesn't help, please describe exactly what you're trying to achieve a little better.



来源:https://stackoverflow.com/questions/23628617/calculate-how-much-user-has-scrolled-incl-margins-paddings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!