【问题标题】:Responsive and Sticky Nav Bar响应式和粘性导航栏
【发布时间】:2014-06-29 15:47:28
【问题描述】:

当宽度低于 768 像素时,我有一个固定在页面顶部的导航栏。

当它超过 768px 时,它从底部开始,当用户滚动经过它时,它会卡在顶部。

这两个实例本身都可以正常工作,但是当浏览器调整大小时,从 768 像素以下到以上时会出现一些问题。 (从 768 到以下工作正常。)

当我在 768 像素以下的浏览器中加载页面,然后调整上方窗口的大小时,我遇到了问题。

我希望导航栏在状态之间平稳过渡。 (当加载到 768 像素以上然后调整到下方并调整到上方时,它工作得很好 - 理想情况下,这就是我希望它在加载到 768 像素以下时工作的方式。)或者作为替代方案,当从下方移动时,只需将导航栏固定到顶部768px 以上。

This is the link to the site.

这是我的 CSS

.header{
width: 100%;
min-width: 300px;
height: 100px;
padding: 0px;
background: black;
position: absolute;
bottom: 0px;
z-index: 99999;
-webkit-font-smoothing: subpixel-antialiased;
}

.header.fixed{
width: 100%;
position: fixed;
top: 0px;
height: 100px;
-webkit-font-smoothing: subpixel-antialiased;
}

@media only screen and (max-width: 768px){
.header{
width: 100%;
background: black;
position: fixed;
top: 0px;
height: 100px;
-webkit-font-smoothing: subpixel-antialiased;
}
}

这是Javascript

<script>  

jQuery(document).ready(function() {
    var s = jQuery(".header");
    var pos = s.position();                    
    jQuery(window).scroll(function() {
        var windowpos = jQuery(window).scrollTop();
        if (windowpos >= pos.top) {
            s.addClass("fixed");
        } else {
            s.removeClass("fixed"); 
        }
    });
});
</script>

我也试过下面没有运气。

<script>  
if ( jQuery(window).width() > 768) {       
jQuery(document).ready(function() {
    var s = jQuery(".header");
    var pos = s.position();                    
    jQuery(window).scroll(function() {
        var windowpos = jQuery(window).scrollTop();
        if (windowpos >= pos.top) {
            s.addClass("fixed");
        } else {
            s.removeClass("fixed"); 
        }
    });
});
}</script>

<script>  
jQuery(window).resize(function(){
if ( jQuery(window).width() > 768) {       
jQuery(document).ready(function() {
    var s = jQuery(".header");
    var pos = s.position();                    
    jQuery(window).scroll(function() {
        var windowpos = jQuery(window).scrollTop();
        if (windowpos >= pos.top) {
            s.addClass("fixed");
        } else {
            s.removeClass("fixed"); 
        }
    });
});
}})</script>

【问题讨论】:

    标签: javascript jquery responsive-design nav sticky


    【解决方案1】:

    试试这个:

    function sticky_navigation() {
        var browserWidth = $(window).width();
        var scroll_top = $(window).scrollTop(); // our current vertical position from the top
    
        // if we've scrolled more than the page, change its position to fixed to stick to top,
        // otherwise change it back to relative
        if ((scroll_top > $('.header').height()) && (browserWidth > 720)) { 
            $('.header').css({ 'position': 'fixed', 'top':0, 'z-index':999999, 'opacity': 0.9, 'box-shadow': '0px 3px 5px #393939' });
        } else {
            $('.header').css({ 'position': 'relative', 'opacity': 1, 'box-shadow': 'none' }); 
        }   
    };
    
    // and run it again every time you scroll
    $(window).scroll(function() {
         sticky_navigation();
    });
    
    // and run every time you resize to boot
    $(window).resize(function() {
         sticky_navigation();
    });
    

    【讨论】:

    • 感谢您的努力,但这根本不起作用。不过,我会尝试使用该功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    相关资源
    最近更新 更多