将侧边栏保留在视口中,滚动问题
我有一个解决方案,可以在您上下滚动页面时在视口中保留侧边栏。当侧边栏比内容区域长时,问题就会出现,并且您继续滚动,因为侧边栏不断向下推页脚,您会得到这种抖动效果。
我在 jsFiddle 中有一个此设置的示例: http://jsfiddle.net/U9F7w/2/< /a> (全屏:http://jsfiddle.net/U9F7w/2/embedded/result/ )
我的问题是,有没有办法让侧边栏在触及底部/页脚区域后停止?
我已阅读关于将侧边栏设置为绝对的一些解决方案,不幸的是它是一个现有网站并且更改位置不起作用并且会扰乱许多现有的页面元素。
这是我正在使用的 jQuery/js:
// set the offset
var sidebarOffset = $(".sidebar").offset();
var sidebarPadding = 15;
// when the window scrolls, keep sidebar in view
$(window).scroll(function() {
if ($(window).scrollTop() > sidebarOffset.top) {
$(".sidebar").stop().animate({marginTop: $(window).scrollTop() - sidebarOffset.top + sidebarPadding });
}
else {
$(".sidebar").stop().animate({marginTop: 0});
};
});
编辑
我想到的一件事是(不确定这是否可能)检测一个 div 的底部是否低于另一个 div 的底部,停止滚动。有没有办法检测一个div的底部是否低于另一个div的底部?
I've got a solution for keeping a sidebar in the viewport as you scroll up and down the page. Problem comes in when the sidebar is longer than the content area, and you keep scrolling you get this jittering effect as the sidebar keeps pushing the footer down.
I've got an example of this setup in jsFiddle: http://jsfiddle.net/U9F7w/2/ (full screen: http://jsfiddle.net/U9F7w/2/embedded/result/ )
My question is, is there a way to make the sidebar stop once it touches the bottom/footer area?
I've read some solutions about setting the sidebar to absolute, unfortunately it's an existing site and changing the position didn't work and messed with a lot of the existing page elements.
Here's the jQuery/js I'm working with:
// set the offset
var sidebarOffset = $(".sidebar").offset();
var sidebarPadding = 15;
// when the window scrolls, keep sidebar in view
$(window).scroll(function() {
if ($(window).scrollTop() > sidebarOffset.top) {
$(".sidebar").stop().animate({marginTop: $(window).scrollTop() - sidebarOffset.top + sidebarPadding });
}
else {
$(".sidebar").stop().animate({marginTop: 0});
};
});
edit
One thing I thought about was (not sure if this is possible) to detect if the bottom of one div was lower than the bottom of another, stop the scrolling. Is there a way to detect if the bottom of one div is lower than the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查侧边栏的高度是否大于内容的高度:
请参阅大内容的演示小提琴和带有大侧边栏的演示小提琴。
我不知道到底为什么,我会将 top 与
position:relative
结合使用,但 marginTop 也可以正常工作。Check if the sidebar's height is greater then that of the content:
See demo fiddle with large content and demo fiddle with large sidebar.
And I don't know why exactly, I would use top in conjunction with
position: relative
, but marginTop works also fine.