简体   繁体   English

粘性侧边栏与粘性导航栏

[英]Sticky sidebar with sticky navbar

I am trying to make a layout that has the following desired behaviour: 我正在尝试制作具有以下所需行为的布局:

期望的行为

The red bar indicates where I want the sidebar to "stick". 红色栏表示我希望边栏“粘”在哪里。 Currently I have a header and when the page scrolls the nav bar below it sticks to the top of the page. 目前,我有一个页眉,并且当页面滚动时,其下方的导航栏会停留在页面顶部。 Then the header and the sidebar continue scrolling. 然后标题和侧边栏继续滚动。 When the sidebar is at the end of the length it sticks at the bottom. 当侧边栏位于长度的末尾时,它将停留在底部。 Then when the main content (consisting of individual posts) is at the end the footer comes and "pushes" the bottom of the sidebar up. 然后,当主要内容(由单个帖子组成)结束时,页脚就会出现,并将边栏的底部向上“推送”。

Then when scrolling back up, the same happens in reverse (preferably with the sidebar scrolling up until the top of it is in view and then sticks to the top below the navbar). 然后,当向后滚动时,反向操作也会发生同样的情况(最好是侧边栏向上滚动直到其顶部可见,然后粘贴到导航栏下方的顶部)。

Currently I have almost all of the desired behaviour by using the sticky-kit plugin, but I can't make it so that the sidebar sticks to just below the navbar instead of the top. 目前,通过使用sticky-kit插件,几乎可以实现所有所需的行为,但是我无法做到这一点,以致侧边栏仅停留在导航栏的下方而不是顶部。

A link can be found here if needed. 如果需要,可以在此处找到链接。

Current jQuery: 当前的jQuery:

$(document).ready(function(){
    $('nav').clone().addClass('scroll').prependTo('#wrapper');
    $("#aside").stick_in_parent();
});
var nav = $("nav");
var pos = nav.position();                    
$(window).scroll(function() {
    var windowpos = $(window).scrollTop();
    if (windowpos >= pos.top) {
        $('nav.scroll').fadeIn(0);
    } else {
        $('nav.scroll').fadeOut(0);
    }
});

Markup 标记

<div id="wrapper">
    <header></header>
    <nav></nav>
    <div id="aside"></div>
    <div id="posts"></div>
</div>

Solved the problem by using the plugin's offset_top option. 通过使用插件的offset_top选项解决了该问题。 It didn't work initially when I had $("#aside, #posts").stick_in_parent(); 当我有$("#aside, #posts").stick_in_parent();时,它最初$("#aside, #posts").stick_in_parent(); but when I changed it to $("#aside").stick_in_parent(); 但是当我将其更改为$("#aside").stick_in_parent(); it worked. 有效。

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

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