gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:52:29 26 4
gpt4 key购买 nike

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

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

  1. 计算屏幕高度
  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

引用资料:

fiddle : http://jsfiddle.net/manishie/NGDBL/

关于javascript - 如何滚动以将 DIV 的底部设置在屏幕底部(监视器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19211332/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com