简体   繁体   English

scrollTop突然不适用于粘性菜单

[英]scrollTop suddenly not working for sticky menu

I had this working with no problems for the entire build of the site. 我在整个网站构建过程中都没有遇到任何问题。 Then, the day I was supposed to launch, the sticky menu stopped working right. 然后,在我应该启动的那天,粘性菜单停止正常工作。 The menu is supposed to start at the bottom, scroll to the top, then stick (position: fixed). 菜单应该从底部开始,滚动到顶部,然后粘贴(位置:固定)。

Now, it scrolls about 10px and then jumps to the top. 现在,它滚动约10像素,然后跳到顶部。 Why is the scrollTop distance not calculating correctly? 为什么scrollTop距离计算不正确?

Live site at http://adammarshalltherapy.com 实时网站: http://adammarshalltherapy.com

Here's the code for the sticky menu. 这是粘性菜单的代码。 I'm also using JS to set min-height of divs to window height, but haven't included that code here. 我还使用JS将div的min-height设置为窗口高度,但此处未包含该代码。

$(function(){
    var stickyRibbonTop = $('#wrapper-wcf53badf7ebadf7').offset().top;

    $(window).scroll(function(){
            if( $(window).scrollTop() > stickyRibbonTop ) {
                    $('#wrapper-wcf53badf7ebadf7').css({position: 'fixed', top: '0px', 'background-image':'url(http://amarshall.360zen.com/wp-content/uploads/2014/07/menu-fade-background2.png)'});
                    $('#block-bcf53bf14093931c').css({display: 'block'});
            } else {
                    $('#wrapper-wcf53badf7ebadf7').css({position: 'static', top: '0px','background-image':'none'});
                    $('#block-bcf53bf14093931c').css({display: 'none'});
            }
    });
});

Thanks in advance for any help! 在此先感谢您的帮助! I'm not a JS or jQuery expert yet, so any suggestions for cleaning things up would be appreciated. 我还不是JS或jQuery专家,所以希望您有任何清理建议。

NOTE: The site is built on WordPress, so no-conflict mode is in effect. 注意:该站点基于WordPress构建,因此没有冲突模式有效。

I think you are initialising the sticky menu function before you set the min-height of $('big-div') . 我认为您在设置$('big-div')的最小高度之前正在初始化粘性菜单功能。

On page load, the menu starts at 54px from the top, and so when you store the offset().top value as stickyRibbonTop , it is stored at 54px. 在网页加载时,菜单开始于54px从顶部,所以当你存储offset().topstickyRibbonTop ,它被存储在54px。 Then on your scroll event you are comparing against this. 然后在滚动事件中与此进行比较。

Try setting the min-height of the divs first in your code, then run this same script afterwards. 尝试先在代码中设置div的min-height ,然后再运行相同的脚本。 The value of stickyRibbonTop should then be correct. stickyRibbonTop的值应正确。

Bear in mind that you will need to reset stickyRibbonTop every time the window.height() is updated, so you should probably make this sticky menu function a named function and call it at the end of the wrapper_height function. 请记住,每次更新window.height()时,都需要重置stickyRibbonTop ,因此您可能应该将此粘性菜单函数命名为一个函数,并在wrapper_height函数的末尾调用它。 something like this: 像这样的东西:

function stickyNav() {
    var stickyRibbonTop = $('#wrapper-wcf53badf7ebadf7').offset().top;

    $(window).unbind('scroll', scrollEvent);
    $(window).on('scroll', stickyRibbonTop, scrollEvent);
};

function scrollEvent(event) {
    var stickyRibbonTop = event.data;
    if ($(window).scrollTop() > stickyRibbonTop) {
        $('#wrapper-wcf53badf7ebadf7').css({ position: 'fixed', top: '0px', 'background-image': 'url(http://www.adammarshalltherapy.com/wp-content/uploads/2014/07/menu-fade-background2.png)' });
        $('#block-bcf53bf14093931c').css({ display: 'block' });
    }
    else {
        $('#wrapper-wcf53badf7ebadf7').css({ position: 'static', top: '0px', 'background-image': 'none' });
        $('#block-bcf53bf14093931c').css({ display: 'none' });
    }
};

function wrapper_height() {

    var height = $(window).height();
    var wrapperheight = height - 75;
    wrapperheight = parseInt(wrapperheight) + 'px';
    $(".bigDiv").css('min-height', wrapperheight);
    $("#wrapper-wcf53bad125d7d9a").css('height', wrapperheight);

    stickyNav();
}
$(function () {
    wrapper_height();
    $(window).bind('resize', wrapper_height);
});

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

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