DIV自动滚动 [英] DIV auto scroll
问题描述
您可以使用 scrollTop 属性设置HTMLElement的内容滚动量。
而你可以通过 offsetTop 获取您需要滚动的数量您要滚动。
例如,使用此HTML:
< div id =container>
< p id =item-1> foo< / p>
< p id =item-2> bar< / p>
< p id =item-3> baz< / p>
< / div>
您可以使用此JavaScript将容器div滚动到第三段:
document.getElementById(container)。scrollTop = document.getElementById(item-3)。
I have a div that has a large number of items in it. I select these items dynamically by clicking a button, overflow is auto in my case. What I want is that when an item that is not visible is selected to scroll the div so that can be visible. How can I do this?
You can use the scrollTop property of HTMLElement to set the amount its content is scrolled by.
And you can get the amount you need to scroll by from offsetTop of the element to which you want to scroll.
For example with this HTML:
<div id="container">
<p id="item-1">foo</p>
<p id="item-2">bar</p>
<p id="item-3">baz</p>
</div>
You could use this JavaScript to scroll the container div to third paragraph:
document.getElementById("container").scrollTop = document.getElementById("item-3").offsetTop;
这篇关于DIV自动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!