繁体   English   中英

限制固定div垂直滚动

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM