Javascript滚动到div底部,如果当前滚动位置接近底部



我正在编写一个脚本,该脚本将文本(大小不同)附加到具有溢出滚动的div块(高度不同)。在这个文本被附加之后,我需要div块滚动到底部,以便看到新的附加文本,但是我只需要它滚动到底部,如果当前滚动位置在接近底部的10%左右,否则如果它在查看div的顶部部分时向下滚动会很烦人。

这就是我想出来的,它是在文本被追加后运行的:

var divblock = document.getElementById('div'); //the block content is appended to
var divheight = divblock.offsetHeight;//get the height of the divblock
//now check if the scroller is near the bottom and if it is then scroll the div to the abslute bottom
if (***HELP HERE***) divblock.scrollTop=divblock.scrollHeight; ///scroll to bottom

非常感谢!

if( divblock.scrollTop < divblock.scrollHeight )

scrollTop + lineheight> scrollHeight

呢?

您只需要检查scrollTop是否大于scrollHeight的90%。

if (divblock.scrollTop > divblock.scrollHeight*0.9) {
  divblock.scrollTop=divblock.scrollHeight; 
}

scrollHeight = scrollTop + clientHeight +[忽悠因子]

对我来说,"蒙混因子"似乎在30-40(像素)左右。所以试试这个:

if ((divblock.scrollTop + divblock.clientHeight + 50) > divblock.scrollHeight) {
    ...
}

最新更新