简体   繁体   English

删除固定导航菜单弹跳-jQuery?

[英]Remove Fixed Navigation Menu Bounce - jQuery?

A working demo of a template I've modified can be found at the following url: 我修改过的模板的有效演示可以在以下网址找到:

https://newbloggerthemes.com/base-wp-blogger-template/demo/ https://newbloggerthemes.com/base-wp-blogger-template/demo/

I've been unable to determine what is causing a annoying bounce of the fixed navigation menu. 我一直无法确定是什么导致固定导航菜单的烦人反弹。 If you scroll the page downward the menu should move upward and stop when its top border reaches the top of the page; 如果向下滚动页面,则菜单应向上移动并在其顶部边框到达页面顶部时停止; however, it actually moves just past the top of the page and then snaps back, causing a annoying bounce. 但是,它实际上仅移到页面顶部上方,然后回弹,从而导致恼人的反弹。

How do I get rid of this bounce? 我如何摆脱这种反弹? I am assuming that this might be caused by the jQuery code used to query the menu's position. 我假设这可能是由用于查询菜单位置的jQuery代码引起的。 Its as if the code is fixing the issue afterward as opposed to preventing it from happening in the first place. 好像代码是在事后解决问题,而不是从一开始就阻止它发生。

I've removed the header from the original template so that the menu initially appears at the top; 我已经从原始模板中删除了标题,以便菜单最初显示在顶部。 however, when the user first scrolls the page downward the menu bounces in a similar fashion so the defect is in the original template and not due to any changes I've made. 但是,当用户第一次向下滚动页面时,菜单会以类似的方式反弹,因此缺陷在原始模板中,而不是由于我所做的任何更改所致。

I've posted the modified template at the url below in case the code below is not enough. 如果下面的代码不够用,我已经在下面的URL上发布了修改后的模板。

https://1drv.ms/t/s!AnHSMVz7JJ2Ocf9KoYS67t_6ZqI https://1drv.ms/t/s!AnHSMVz7JJ2Ocf9KoYS67t_6ZqI

jQuery(document).ready(function($) {
    var $filter = $('.main-navigationbwrap');
    var $filterSpacer = $('<div />', {
        "class": "filter-drop-spacer",
        "height": $filter.outerHeight()
    });


    if ($filter.size())
    {
        $(window).scroll(function ()
        {
            if (!$filter.hasClass('fix') && $(window).scrollTop() > $filter.offset().top)
            {
                $filter.before($filterSpacer);
                $filter.addClass("fix");
            }
            else if ($filter.hasClass('fix')  && $(window).scrollTop() < $filterSpacer.offset().top)
            {
                $filter.removeClass("fix");
                $filterSpacer.remove();
            }
        });
    }

});

Okay, you're done removed the header HTML. 好的,您已经删除了标题HTML。

Now you can also removed that jQuery. 现在您还可以删除该jQuery。 Becuase to make sure your menu navigation position always fixed at the top, you can do that just by CSS only. 因为要确保菜单导航位置始终固定在顶部,所以只能通过CSS做到这一点。 Example CSS is below. CSS示例如下。

.site-headerbwrap {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 9999;
}

in you case: paste CSS code above before ]]></b:skin> line and then Save your Template 如果您遇到这种情况:请将CSS代码粘贴在[ ]]></b:skin>行之前,然后保存您的模板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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