Javascript 如何创建“粘性”浮动固定/滚动侧边栏?
我有一个网站,在左边有一个酒吧,应该和用户呆在一起。 因此,当用户滚动时,边栏会一直滚动,直到距离页面顶部5px为止。从那时起,它应该被锁定在那里 当然,视图端口可能比左栏小,因此左栏不能完全适应屏幕。不过也不是很戏剧性。然而,我希望如果用户滚动到底部,侧边栏的底部“点击”页面的页脚,然后再次沿着页面滚动 下面是我得到的代码:我的站点的基本设置和我问题第一部分的尝试(但你会看到它不起作用): 我认为问题的第一部分相当清楚,但第二部分可能有点难以理解,因此这里有一个模型: 您可以看到没有显示任何文本,因为文本位于视口上方 以下是我在第一部分尝试的js:Javascript 如何创建“粘性”浮动固定/滚动侧边栏?,javascript,jquery,css,scroll,Javascript,Jquery,Css,Scroll,我有一个网站,在左边有一个酒吧,应该和用户呆在一起。 因此,当用户滚动时,边栏会一直滚动,直到距离页面顶部5px为止。从那时起,它应该被锁定在那里 当然,视图端口可能比左栏小,因此左栏不能完全适应屏幕。不过也不是很戏剧性。然而,我希望如果用户滚动到底部,侧边栏的底部“点击”页面的页脚,然后再次沿着页面滚动 下面是我得到的代码:我的站点的基本设置和我问题第一部分的尝试(但你会看到它不起作用): 我认为问题的第一部分相当清楚,但第二部分可能有点难以理解,因此这里有一个模型: 您可以看到没有显示任何文
$(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;
}
我再次尝试指定高度等(因为它显示得非常大),但没有任何进展。我刚才做了这件事,下面是我创建的代码: (您可能需要稍微更改标记,我不确定这是否适用于您的表格布局,我建议使用
divs
并浮动它们来布局您的内容。)或者,您可以使用下面的代码/逻辑和滚动您自己的
,使用您自己的布局
基本上,-获取我们的元素
-获取页面加载时侧边栏的位置
-获取
#content.outerHeight()
一旦我们有了这些变量,在窗口上滚动
测试,看看我们是否是=
我们原来的边栏
位置,然后添加.sticky
类(它有位置:fixed
)
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');
}
}
}
:)
签出这个插件:我正在考虑自己写它,因为这些插件通常提供比需要更多的选项。尽管如此,我还是尝试了一下,但我认为它与我的表布局不太匹配:对于您的标记,它似乎可以工作,但我宁愿侧边栏不消失,而是锁定页脚!然而,使用JustAnil提出的插件实现页面布局,而不是我的表格布局(clear
Dod the magic),它似乎可以按照我的要求工作(当然,代码比您提供的要多,但很好)。您可以稍微编辑Javascript,使页脚粘到底部而不是消失,但很高兴我能提供帮助@JustAnil如何编辑你的小提琴,使其页脚粘到底部?我自己也在想办法。理想情况下,我希望在边栏到达其容器元素的末尾时添加一个bottom
类。我在这里提出了一个类似的问题:如果你能看一看,我将非常感激。我有一段时间对这个有意见了。
<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;
}