如何创建一个“粘性”浮动固定/滚动边栏?

4
我有一个网站,左侧有一栏应该跟随用户滚动。因此,当用户滚动时,侧边栏会一起滚动,直到离页面顶部大约5像素为止。从那时起,它应该锁定在那里。
当然,视口可能比左侧栏要小,因此左侧栏不完全适合屏幕。但这并不是什么大问题。但是,如果用户滚动到底部,侧边栏的底部将“碰撞”页面页脚,然后再次随页面一起滚动,这是我想要的效果。
这是我拥有的代码:我的网站的基本设置以及对我的问题第一部分的尝试(但您会看到它不起作用):jsfiddle
我认为问题的第一部分相当清楚,但第二部分可能有点难理解,因此这里有一个模拟:when scrolled down 您可以看到没有显示文本,因为文本在视口上方。
这是我尝试第一部分的js代码:
$(document).ready(function () {
    var theLoc = 5;
    var links = $('#left');
    $(window).scroll(function () {
        console.log('scroll');
        if (theLoc >= $(window).scrollTop()) {
            if (links.hasClass('fixed')) {
                links.removeClass('fixed');
            }
        } else {
            if (!links.hasClass('fixed')) {
                links.addClass('fixed');
            }
        }
    });
});

但可能更多是CSS问题:
.fixed {
    position:fixed;
}

我尝试重新指定高度等参数(因为它显示得非常大),但没有进展。


1
请查看此插件:http://mojotech.github.io/stickymojo/ - Anil
我曾考虑自己编写它,因为通常这些插件提供的选项过多。尽管如此,我还是试了一下,但我认为它与我的表格布局不太兼容:http://jsfiddle.net/kvVHh/1/。 - jdepypere
1个回答

9

我之前做过这个,这是我创建的代码:查看JSFiddle

(您可能需要稍微更改一下您的标记,我不确定这是否适用于您的表格布局,建议使用

并将它们浮动以布置内容。)或者,您可以使用以下代码/逻辑,并使用自己的布局roll your own

基本上,
- 获取我们的元素
- 获取侧栏在页面加载时的位置
- 获取 #content.outerHeight()

一旦我们有了这些变量,在window scroll中测试是否<=(小于或等于)我们原始的sidebarTop position或检查是否超过了blogHeight,如果任何一个条件为真,则删除粘性类,或者如果我们的滚动位置>=我们原始的sidebar位置,则添加.sticky类(其具有position: fixed)。

查看JSFiddle (单击此处)


Javascript如下:

// Cache our vars for the fixed sidebar on scroll
var $sidebar = $('#sidebar-nav');

// Get & Store the original top of our #sidebar-nav so we can test against it
var sidebarTop = $sidebar.position().top;

// Edit the `- 10` to control when it should disappear when the footer is hit.
var blogHeight = $('#content').outerHeight() - 10;

// Add the function below to the scroll event
$(window).scroll(fixSidebarOnScroll);

// On window scroll, this fn is called (binded above)
function fixSidebarOnScroll(){

    // Cache our scroll top position (our current scroll position)
    var windowScrollTop = $(window).scrollTop();

    // Add or remove our sticky class on these conditions
    if (windowScrollTop >= blogHeight || windowScrollTop <= sidebarTop){
        // Remove when the scroll is greater than our #content.OuterHeight()
        // or when our sticky scroll is above the original position of the sidebar
        $sidebar.removeClass('sticky');
    }
    // Scroll is past the original position of sidebar
    else if (windowScrollTop >= sidebarTop){
        // Otherwise add the sticky if $sidebar doesnt have it already!
        if (!$sidebar.hasClass('sticky')){
            $sidebar.addClass('sticky');
        }
    }   
}

HTML 是什么:

<header>This is the header!</header>
<ul id="sidebar-nav" class="nav nav-list">
    <li><a href="#">Home</a></li>
    <li><a href="#">Blog</a></li>
</ul>
<div id="content">Content in here, scroll down to see the sticky in action!</div>
<div class="clear"></div>
<div id="footer">This is the #footer</div>

CSS

/* Sticky our navbar on window scroll */
#sidebar-nav.sticky {position:fixed;top:5px;}

/* Other styling for the html markup */
header {
    border:1px solid #aaa;
    background-color:yellow;
    margin-bottom:5px;
    height:50px;
}
#sidebar-nav {
    width:150px;
    border:1px solid #ddd;
    margin:0;
    padding:0;
    float:left;
}
#sidebar-nav li {
    list-style:none;
    border:1px solid #ddd;
    margin:10px;
    padding:2px;
}
#content {
    height:2000px;
    width:500px;
    padding:10px;
    border:1px solid #ddd;
    margin:0 0 10px 5px;
    float:right;
}
#footer {
    height:800px;
    border:1px solid green;
    background-color:#ddd;
}
.clear {
    clear:both;
}

:)


看起来你的标记代码可以工作,但我宁愿侧边栏不消失而是锁定在页脚上!然而,使用JustAnil提出的插件实现你的页面布局(与我的表格布局相反)(clear起了奇效),它看起来像我想要的(当然,比你提供的代码更复杂,但没关系)。http://jsfiddle.net/dmLQL/1/ - jdepypere
1
你本可以稍微编辑一下Javascript代码,让页脚固定在底部而不是消失掉,但我很高兴能够帮到你! - Anil
@JustAnil,您如何编辑您的小提琴以使页脚固定在底部?我正在尝试自己解决这个问题。理想情况下,当侧边栏到达其容器元素的末尾时,我想添加一个bottom类。我在这里设置了一个类似的问题:http://stackoverflow.com/questions/22588635/how-do-i-add-a-stopper-to-this-sticky-sidebar 如果您能看一下,我将非常感激。我已经遇到了这个问题有一段时间了。 - J82

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接