【问题标题】:How can I create a `sticky` floating fixed/scrolling sidebar?如何创建“粘性”浮动固定/滚动侧边栏?
【发布时间】:2013-06-26 14:35:25
【问题描述】:

我有一个网站,左边有一个栏,应该留给用户。 因此,当用户滚动时,侧边栏会一直滚动,直到距离页面顶部 5px。从那时起,它应该被锁定在那里。

当然,视口可能小于左侧栏,因此左侧栏无法完全适应屏幕。虽然不是很戏剧化。但是,如果用户滚动到底部,我希望它在侧边栏的底部“点击”页面的页脚,然后再次与页面一起滚动。

这是我得到的代码:我的网站的基本设置和我问题第一部分的尝试(但你会发现它不起作用):jsfiddle

我认为问题的第一部分相当清楚,但第二部分可能有点难以理解,所以这里是一个模型: 您可以看到没有显示任何文本,因为文本位于视口上方。

这是我尝试第一部分的 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;
}

我尝试再次指定高度等(因为它看起来很大),但没有提前。

【问题讨论】:

  • 签出这个插件:mojotech.github.io/stickymojo
  • 我正在考虑自己编写它,因为这些插件通常会提供比需要更多的选项。尽管如此,我还是试了一下,但我认为它不适用于我的表格布局:jsfiddle.net/kvVHh/1

标签: javascript jquery css scroll


【解决方案1】:

我不久前做过这个,这是我为此创建的代码:View the JSFiddle

(您可能需要稍微更改您的标记,我不确定这与您拥有的表格布局的效果如何,我建议使用divs 并浮动它们来布局您的内容。),或者,您可以使用下面的代码/逻辑和roll your own 与您自己的布局。

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

一旦我们有了这些变量,在window scroll 上测试看看我们是否是<=(小于或等于)我们原来的sidebarTop position,或者检查我们是否超过了blogHeight,如果有的话2是真的,去掉粘性类,elseif我们的滚动是>=我们原来的sidebar位置,然后添加.sticky类(它有position: fixed)。

Check the JSFiddle (Click here)


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 做了魔术),它似乎可以按我的意愿工作(当然,代码比您提供的要多,但是很好)。 jsfiddle.net/dmLQL/1
  • 您可以稍微编辑 Javascript 以使页脚粘在底部而不是消失,但很高兴我能提供帮助!
  • @JustAnil 如何编辑您的小提琴以使页脚粘在底部?我正在尝试自己解决这个问题。理想情况下,当侧边栏到达其容器元素的末尾时,我想添加一个 bottom 类。我在这里设置了一个类似的问题:stackoverflow.com/questions/22588635/… 如果您可以看一下,我将不胜感激。这个问题我已经有一段时间了。
猜你喜欢
  • 2015-09-23
  • 2012-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多