滚动到我的div下面?

时间:2012-10-10 07:46:15

标签: javascript jquery

我得到了一个可以在点击时扩展的div,有时它位于页面底部并靠近底部。我想要的是,当它接近底部时,它应该向下滚动。

到目前为止我的解决方案如下。我得到了如何计算值和动画一个全部,但我不知道为什么我的脚本想要滚动到页面的底部,它只是将它砸到底部。有什么想法吗?

修改
Doh因为它滚动到底部。问题是我如何滚动所以它在我的div下面10px?我不想说scrollTop: total

jquery的

       var $ediv = $this.parent('div').find('.order-expand-row');
       var hDiv = $ediv.outerHeight();
       var oDiv = $ediv.offset().top;
       var wHeight = $(window).height();
       var total = hDiv + oDiv + 10;

       if (total >= wHeight) {                                   
          $('html, body').animate({ scrollTop: total }, 600);
       }

1 个答案:

答案 0 :(得分:0)

一杯茶可以做魔法;)

通过从wHeight减去total来解决它,这让我得到了我需要滚动到的值。 要清除它,我需要的是我想要滚动停止的位置而不是滚动到的位置。

   var $ediv = $this.parent('div').find('.order-expand-row');
   var hDiv = $ediv.outerHeight();
   var oDiv = $ediv.offset().top;
   var wHeight = $(window).height();
   var total = hDiv + oDiv + 10;
   var scrollTo = total - wHeight; //the scroll position

   if (total >= wHeight) {                                   
      $('html, body').animate({ scrollTop: scrollTo }, 600);
   }