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

时间:2013-06-26 14:35:25

标签: javascript jquery css scroll

我的网站左侧有一个应该留在用户的栏。 因此,当用户滚动时,侧边栏会一直滚动,直到它从页面顶部开始说5px。从那以后,它应该锁定在那里。

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

以下是我获得的代码:我网站的基本设置以及我问题第一部分的尝试(但您会发现它不起作用):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 个答案:

答案 0 :(得分:5)

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

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

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

我们拥有这些变量后,在window scroll测试中查看我们是<=(小于或等于)原始sidebarTop position还是检查我们是否超过{{1}如果2中的任何一个为真,则删除粘性类,blogHeight我们的滚动是elseif我们原来的>=位置,然后添加sidebar类(其中包含.sticky)。

Check the JSFiddle (Click here)


Javascript 就像这样:

position: fixed

HTML

// 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');
        }
    }   
}

CSS

<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>

/* 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; }

相关问题