DIV自动滚动 [英] DIV auto scroll

查看:76
本文介绍了DIV自动滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个div里有大量的项目。我通过点击一个按钮动态选择这些项目,在我的情况下,溢出是自动的。我想要的是当选择不可见的项目滚动div,以便可见。我该怎么做?

解决方案

您可以使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆