我正在研究(粘滞)滚动边栏。问题是,大多数粘性侧边栏没有考虑侧边栏可能比视口高(例如,如果很多项目被添加到篮子(边栏))。这是我想要解决的。这些都是要求:高级jQuery粘性边栏
如果侧边栏的高度越高,则视口,应该 滚动通过内容和div的底部应该坚持 视口的底部进一步向下滚动时。
如果侧边栏的高度比视口高,则只有当您位于 页面的底部时,才会显示下面的divs 。
当用户向后滚动时,侧边栏滚动回顶部并且 粘回到视口的顶部。
如果侧边栏的高度小于视口,则在向下滚动时应从顶部粘贴 。
所以总的来说相当一些功能,并不是那么简单(我认为)。我见过的最接近我想要实现的是这个例子:http://demo.techbrij.com/730/fix-sidebar-scrolling-jquery.php但代码写入的方式不适合我的。
到目前为止,这是我做了什么:DEMO
而jQuery代码我使用:
jQuery(document).ready(function($) {
var $sidebar = $('.sidebar'),
$content = $('.content');
if ($sidebar.length > 0 && $content.length > 0) {
var $window = $(window),
offset = $sidebar.offset(),
timer;
$window.scroll(function() {
clearTimeout(timer);
timer = setTimeout(function() {
if ($content.height() > $sidebar.height()) {
var new_margin = $window.scrollTop() - offset.top;
if ($window.scrollTop() > offset.top && ($sidebar.height()+new_margin) <= $content.height()) {
// Following the scroll...
$sidebar.stop().animate({ marginTop: new_margin });
} else if (($sidebar.height()+new_margin) > $content.height()) {
// Reached the bottom...
$sidebar.stop().animate({ marginTop: $content.height()-$sidebar.height() });
} else if ($window.scrollTop() <= offset.top) {
// Initial position...
$sidebar.stop().animate({ marginTop: 0 });
}
}
}, 100);
});
}
});
嗯。我没有考虑到我自己的头文件/元素库。我想我有一个新问题需要解决。 :-) http://underpull.github.com/Balloon/ – Vinay 2013-03-25 21:31:41