简体   繁体   English

滚动到标题后,将显示DIV导航

[英]DIV nav appear after scrolling past header

I am designing a site with a sticky nav appearing after scrolling past the header. 我正在设计一个滚动了标题后出现粘性导航的网站。

I got this to work using this script: 我可以使用以下脚本来工作:

$(window).load(function(){
// Get the headers position from the top of the page, plus its own height
var startY = $('#header').position().top + $('#header').outerHeight();

$(window).scroll(function(){
    checkY();
 });

function checkY(){
    if( $(window).scrollTop() > startY ){
        $('#navbar').slideDown();
    }else{
        $('#navbar').slideUp();
    }
}

 // Do this on load just in case the user starts half way down the page
 checkY();
 });//]]>  

The problem is the scrip reads the height of my header on load, but because my header height is 100% of the viewport, when one resizes the window, the nav appears either too late or too early. 问题在于,脚本会读取加载时我标头的高度,但是由于我标头的高度是视口的100%,因此当调整窗口大小时,导航显示的时间太晚或太早。

For example loading the page with a 670px high viewport, sized down to a 400px viewport. 例如,以670像素高的视口加载页面,并缩小到400像素视口。 My header shrinks along to 400px high, even though te nav only appears after 670px 我的标题缩小到400px高,即使te nav仅在670px之后出现

any way to fix this? 有任何解决这个问题的方法吗? thanks 谢谢

Put your javascript function trigger in a document.ready() instead of a window.load() ? 将您的javascript函数触发器放入document.ready()而不是window.load()吗?

Try moving this part OUT of the window.load(). 尝试将这一部分移出window.load()。

$(window).scroll(function(){
    checkY();
 });

Try this: 尝试这个:

$(window).on("load resize",function(e){
    // Get the headers position from the top of the page, plus its own height
    var startY = $('#header').position().top + $('#header').outerHeight();

    $(window).scroll(function(){
        checkY();
    });

    function checkY(){
        if( $(window).scrollTop() > startY ){
            $('#navbar').slideDown();
        }else{
            $('#navbar').slideUp();
        }
    }

    // Do this on load just in case the user starts half way down the page
    checkY();
});//]]>

It's only the first line (on load resize) that is modified. 仅修改了第一行(在调整负载大小时)。

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

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