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

how to scroll to set the bottom of a DIV at the bottom of the screen (monitor)

本文关键字:底部 设置 监视器 屏幕 DIV 何滚动 滚动      更新时间:2023-09-26

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

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

  1. 计算screenHeight
  2. 计算divHeight
  3. 计算divDistanceFromTopOfScreen

如果screenHeight <divHeight divDistanceFromTopOfScreen=">

    scroll down to bottom of `DIV`

如果你认为这个算法是正确的,我该如何实现这样的事情?

这应该可以做到。

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

引用:

  • j查询偏移量:http://api.jquery.com/offset/

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