Fixed top menu on scroll doesn't allow to reach screen's bottom

笑着哭i 提交于 2019-12-11 13:52:17

问题


I have a strange behavior with my fixed top menu. The screen height is dynamic (depends on the number of registries retrived by the database). When the number of registries creates a scroll, but not high enough to cover up all the menu, the screen bounces and doesn't allow me to reach the bottom (the scroll jumps up again, no matter what).

I was able to simulate the behavior here: http://jsfiddle.net/thiagoprzy/0kkx9tsb/

I believe the issue relies on the way I created the JS part, but when I searched about alternative solutions, almost all of them were very very similar to this one.

P.S: My screen resolution is 1650x1050, so if you have a tinier resolution, maybe you'll need to change the .container height value in order to reproduce the issue.


回答1:


you need to use $('.content').offset().top instead of $('.floating-filter').offset().top or you can use

if ($(window).scrollTop() > $('.floating-filter').outerHeight(true)) {

So you can use

$(window).scroll(function () {
    if ($(window).scrollTop() > $('.content').offset().top) {
        $('.floating-filter').addClass('fixed');
    } else {
        $('.floating-filter').removeClass('fixed');
    }
});

Working Demo




回答2:


So, that's my solution: http://jsfiddle.net/thiagoprzy/0kkx9tsb/15/

Basically I gave up on position: fixed and used position: absolute. Then, I put the menu inside a wrapper with position: relative and, in $(document).ready() I set the wrapper's height the same as the menu. For last, I update the top value of the menu based on $(window).scrollTop() position inside $(window).scroll() event. Maybe it's not the most clean solution, but it solved my issue and now I'm happy. =)



来源:https://stackoverflow.com/questions/33783008/fixed-top-menu-on-scroll-doesnt-allow-to-reach-screens-bottom

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