【问题标题】:sticky navigation hides at top of the page粘性导航隐藏在页面顶部
【发布时间】:2012-12-26 18:24:49
【问题描述】:

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

这是我正在构建的网站: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();
    });

});

【问题讨论】:

  • 不使用position: relative,你为什么不把它隐藏起来?
  • 不应该一直隐藏。仅在页面的最顶部。
  • 隐藏它并使用 jquery 创建一个滚动事件监听器。在那里您可以分析滚动位置并显示/隐藏导航。
  • @Marcel 设置为position: relative时不是在页面顶部吗?
  • 我想添加这样的内容:$(document).ready(function(){ // hide #back-top first $("#sticky_navigation").hide(); // fade in #back-top $(function () { $(window).scroll(function () { if ($(this).scrollTop() > 100) { $('#sticky_navigation').fadeIn(); } else { $('#sticky_navigation').fadeOut(); } });

标签: jquery navigation hide sticky


【解决方案1】:

我不确定这是否是您要查找的内容。如果 scrolltop 比你的 windowheight 大,显示它并将它放在顶部

$(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();
    });

});

【讨论】:

  • 不幸的是,这与我拥有的代码没有什么不同。
  • 那么你想做什么?
【解决方案2】:

我明白了。我将脚本更改为:

$(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。

【讨论】:

  • 但是它可能无法 100% 工作......它在页面加载时显示得非常快,然后消失。有谁知道如何解决这个问题?
猜你喜欢
  • 1970-01-01
  • 2018-10-13
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 2018-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多