粘性导航隐藏在页面顶部



我正在寻找一个脚本,该脚本将我的粘性导航隐藏在页面顶部。因此,最后,当您向下滚动进入网站时,应该开始可见。

这是我正在建立的网站:http://kmnew.kadushimarketing.com/index.php

这是我目前正在使用的脚本:

$(function() {
    // grab the initial top offset of the navigation 
    var sticky_navigation_offset_top = $('#sticky_navigation').offset().top;
    // our function that decides weather the navigation bar should have "fixed" css position or not.
    var sticky_navigation = function(){
        var scroll_top = $(window).scrollTop(); // our current vertical position from the top
        // if we've scrolled more than the navigation, change its position to fixed to stick to top, otherwise change it back to relative
        if (scroll_top > sticky_navigation_offset_top) { 
            $('#sticky_navigation').css({ 'position': 'fixed', 'top':0, 'left':0 });
        } else {
            $('#sticky_navigation').css({ 'position': 'relative' }); 
        }   
    };
    // run our function on load
    sticky_navigation();
    // and run it again every time you scroll
    $(window).scroll(function() {
         sticky_navigation();
    });
});

我不确定这是否是您想要的。如果Scrolltop大于您的WindowSheight,请显示并将其放在TOP

$(function() {
    // grab the initial top offset of the navigation 
    var sticky_navigation_offset_top = $('#sticky_navigation').offset().top;
    // our function that decides weather the navigation bar should have "fixed" css position or not.
    var sticky_navigation = function(){
        var scroll_top = $(window).scrollTop(); // our current vertical position from the top
        var windowHeight = $(window).height();
        // if we've scrolled more than the navigation, change its position to fixed to stick to top, otherwise change it back to relative
        if (scroll_top > windowHeight) { 
            $('#sticky_navigation').css({ 'position': 'fixed', 'top':0, 'left':0, 'display': 'block' });
        } else {
            $('#sticky_navigation').css({ 'position': 'relative', 'display': 'none' }); 
        }   
    };
    // run our function on load
    sticky_navigation();
    // and run it again every time you scroll
    $(window).scroll(function() {
         sticky_navigation();
    });
});

我得到了它。我更改了脚本:

$(document).ready(function(){
    // hide #sticky_navigation first
    $("#sticky_navigation").hide();
    // fade in #sticky_navigation
    $(function () {
        $(window).scroll(function () {
            if ($(this).scrollTop() > 100) {
                $('#sticky_navigation').fadeIn();
            } else {
                $('#sticky_navigation').fadeOut();
            }
        });

    });
});

然后我还添加了位置:固定在我的CSS中。

相关内容

  • 没有找到相关文章

最新更新