我正在研究(粘滯)滾動邊欄。問題是,大多數粘性側邊欄沒有考慮側邊欄可能比視口高(例如,如果很多項目被添加到籃子(邊欄))。這是我想要解決的。這些都是要求:高級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