如何滚动以在屏幕底部设置DIV的底部(显示器)

时间:2013-10-06 16:48:24

标签: javascript html

我的网站中有DIV,其高度不固定。当用户在图像上移动鼠标时,将出现DIV,并显示有关图像的一些信息。页面上有几个网格格式的图像,每个图像都有自己的信息。很明显,一些图像位于屏幕底部,因此mouseover信息的某些部分DIV将不在屏幕上。我想自动控制这种情况,当其DIV的任何部分离开屏幕时,滚动条会一直向下,直到它到达DIV的底部。

这是我的伪代码但我无法在jquery中实现它。

  1. 计算screenHeight
  2. 计算divHeight
  3. 计算divDistanceFromTopOfScreen
  4. 如果screenHeight< divHeight + divDistanceFromTopOfScreen那么

        scroll down to bottom of `DIV`
    

    如果你认为这个算法是对的,我怎么能实现这个呢?

1 个答案:

答案 0 :(得分:-1)

这应该这样做。

<强>使用Javascript:

var top = $('.theDiv').offset().top;     // the top of your div relative to the document
var div_height = $('.theDiv').height();  // the height of your div
window.scrollTo(0, top + div_height);    // scroll to the bottom of your div

<强>参考文献:

小提琴: http://jsfiddle.net/manishie/NGDBL/

相关问题