简体   繁体   English

将元素粘贴到滚动上固定元素的底部?

[英]Sticking an element to the bottom of a fixed element on scroll?

Basically what I'm trying to achieve is to get a secondary navigation to stick to the bottom of the main navigation as soon as it meets the bottom of the main navigation on scroll. 基本上,我要实现的目的是使辅助导航在滚动时遇到主导航的底部时,立即停留在主导航的底部。 I'm still learning with jQuery and I've started to tear my hair out! 我仍在学习jQuery,并且已经开始扯头发了!

EDIT: Realised I didn't explain where I'd got to so far; 编辑:意识到我到目前为止还没有解释我要去的地方; the class is being added to the element however instead of fixing below the main header it's going off up out of the viewport. 该类已添加到元素中,但是不是固定在主标头下方,而是从视口移出。

EDIT 2: JSFiddle 编辑2: JSFiddle

If anyone could offer some help/advice I would appreciate it so much! 如果有人可以提供帮助/建议,我将非常感谢!

Outer-Header height: 160px 外部标题高度:160px

Markup is as follows: 标记如下:

HTML HTML

<div class="outer-header" id="header">
    <header>
        ....    
    </header>        
</div>
<div id="page-wrap">
    <div class="upper-content">
        .....
    </div>
    <nav id="secondary_nav">
        ....
    </nav>

CSS CSS

.sticky{
    position: fixed;
    top: 206px; /*top of viewport + height of secondary nav and outer-header*/
}

JS JS

var stickyNavTop = $('#secondary_nav').offset().top - 160;  

var stickyNav = function(){  
    var scrollTop = $(window).scrollTop();  

    if (scrollTop > stickyNavTop) {   
        $('#secondary_nav').addClass('sticky');  
    } else {  
        $('#secondary_nav').removeClass('sticky');   
    }  
};  

stickyNav();  

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

This should work: 这应该工作:

.sticky{
    position: fixed !important;
    top: 160px;
}

Your code is 99% there. 您的代码在那里是99%。 You just need to specify the class better to stop it being overwritten. 您只需要更好地指定该类即可阻止其被覆盖。

DEMO http://jsfiddle.net/H9Bz3/27/ 演示http://jsfiddle.net/H9Bz3/27/

#secondary_nav.sticky{
    position: fixed;
    top: 160px;
}

Updated: 更新:

To ensure the height from the top is correct I would assign it CSS based on the element height of outter-header 为了确保从顶部开始的高度正确,我将根据outter-header的元素高度为其分配CSS

DEMO http://jsfiddle.net/H9Bz3/28/ 演示http://jsfiddle.net/H9Bz3/28/

jQuery(document).ready(function($){
    //Sticky
    var stickyNavTop = $('#secondary_nav').offset().top - 160; 
    var topHeight = $('.outer-header').outerHeight();

    var stickyNav = function(){  
        var scrollTop = $(window).scrollTop();  

        if (scrollTop >= stickyNavTop) {   
            $('#secondary_nav').css({
                'position': 'fixed',
                'top': topHeight+'px'
            });  
        } else {  
            $('#secondary_nav').css({
                'position': 'relative',
                'top': '0'
            });  
        }  
    };  

    stickyNav();  

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

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

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