设置滚动条位置
我在 div 中有一个表格(它溢出,因此浏览器呈现滚动条)。使用 JavaScript,如何将滚动条手柄沿着轨道移动到与表中行的位置相对应的位置?
+--------------------------------------+
|100 | |
|101 | |
|102 | |
|103 | |
|104 | |
|105 This is a table that |---|
|106 has overflowed. | - | <-- I want to move this using JavaScript,
|107 |---| so that a specific row # is visible.
|108 | |
|109 | |
|110 | |
|111 | |
+--------------------------------------+
I have a table inside of a div (it's overflow, so the browser renders a scrollbar). Using JavaScript, how do I move the scrollbar handle along the track to a position that corresponds to the location of a row in the table?
+--------------------------------------+
|100 | |
|101 | |
|102 | |
|103 | |
|104 | |
|105 This is a table that |---|
|106 has overflowed. | - | <-- I want to move this using JavaScript,
|107 |---| so that a specific row # is visible.
|108 | |
|109 | |
|110 | |
|111 | |
+--------------------------------------+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您想采用非 jQuery 方式,请使用包含表的
scrollTop
属性。假设您可以使用 ID 以及包含的
轻松识别要滚动到的行。使用
offsetTop
您想要滚动到的元素。If you want to go the non-jQuery way, use the containing table's
scrollTop
property.Lets assume you can easily identify the row you want to scroll to using an ID, along with the containing
<div />
. Use theoffsetTop
of the element you want to scroll to.你可以使用这个:
另外,你可以检查这个 jsfiddle 上的示例 。
You can use this:
Also, you could check this example on jsfiddle .
使用Jquery:
'.scroll-table' 是可滚动部分使用的类名
1.首先转到初始位置
2.下面一行计算表格顶部位置之间的滚动差异以及我们滚动到的行
Using Jquery:
'.scroll-table' is the class name used for scroll-able part
1.First go to the initial position
2.Below line calculates the scroll difference between the tables top position and the row to which we have scroll
如果你对这里的 jQuery 感兴趣,可以按如下方式进行:
If you are interested in jQuery here, you can do it as follows:
您还可以使用
.scrollTop()
jQuery 方法:https://api.scrollTop()。 jquery.com/scrollTop/
You can also use the
.scrollTop()
jQuery method:https://api.jquery.com/scrollTop/
有一个使用 AngularJs 的解决方案。
第一个参数是位置。第二个是动画的持续时间。
There is a solution using AngularJs.
The first parameter is the position. The second is the duration of animation.