[英]Limit fixed div vertical scrolling
我有2列布局,左側有固定的側邊欄,而右側則有頁面內容。 我也有頁腳。 我希望固定的側邊欄向右滾動到頁面內容div末端的坐標。
但是,如果側邊欄的高度大於頁面內容的高度,則需要自動為頁面內容div分配新的高度。 此代碼適用於第一種情況下(內容比側邊欄更高),但如果我重新載入頁面一次(當,例如,我去這種頁從另一個網頁,腳本不分配新第二種情況僅適用高度,但是當我重新加載頁面時,確實可以)
這是代碼:
$(window).load(function(){
var windw = this;
var contentHeight = $('#content').height(); // fetch Сontent div height
var sideHeight = $('.sidebar-nav-fixed').height(); // fetch sidebar div height
$.fn.followTo = function ( pos ) {
var $this = this,
$window = $(windw);
$window.scroll(function(e){
if ($window.scrollTop() >= pos) {
$this.css({
position: 'absolute',
top: pos
});
} else {
$this.css({
position: 'fixed',
top: 100
});
}
});
};
if (sideHeight > contentHeight) {
$('#content').css ({
height: contentHeight + (sideHeight - contentHeight)
}),
contentHeight = $('#content').height() // Assign a new height to page content div
}
$('.sidebar-nav-fixed').followTo(contentHeight - sideHeight);
});
如果您有任何想法,我將非常高興,謝謝!
看來我解決了這個問題。 在我的側邊欄中,我有可動態加載的facebook小部件,因此,當我打開頁面時,腳本將獲取高度而沒有Facebook div高度,因為尚未加載。 在加載Facebook小部件后,我只是做了一些操作來加載滾動腳本。
甚至更好的方法-將此小部件放入容器中,並在CSS中手動為其指定高度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.