2012-09-14 59 views
0

我有2列布局,左边是固定边栏,右边是页面内容。我也有页脚。我希望我的固定边栏向右滚动到页面内容div末尾的坐标。限制固定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); 

}); 

我会很高兴,如果您有任何意见 谢谢!

回答

0

看起来像我解决了这个问题。 在我的边栏中,我有Facebook动态加载的小部件,所以当我打开页面时,脚本获取高度而无需Facebook div高度,因为它尚未加载。在加载了Facebook小部件后,我只做了很少的操作来加载滚动脚本。

甚至更​​好的方法 - 把这个小部件放入容器中,并在css中手动指定高度。