简体   繁体   中英

Scroll down then fixed navigation glitch

I have this glitch that is bothering me. The problem is that my navigation is not working properly as I wanted it to be. It jumps even though I have not reached the top of my nav or after passing the height of my header with scrollTop value. I recreate the problem in jsfiddle .

var header_height = $('header').height();
//var main_nav = $('nav');
$(document).scroll(function () {
    if ($(this).scrollTop() >= header_height) {
        $('nav').addClass("fixed");
    } else {
        $('nav').removeClass("fixed");
    }
});

在此处输入图片说明

Instead of taking the height of header, you should be taking the top of .main_nav to compare with ScrollTop.
Change the first line in the code you posted above to:

var header_height = $('.main_nav').position().top;

That should work. Here is the working fiddle.

Hope this helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM