设置滚动条位置

Scr*_*bar 35 javascript scrollbar

我在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                               |   |
+--------------------------------------+
Run Code Online (Sandbox Code Playgroud)

Dan*_*ert 50

如果您想使用非jQuery方式,请使用包含表的scrollTop属性.

让我们假设您可以使用ID以及包含ID轻松识别要滚动到的行<div />.使用offsetTop要滚动到的元素.

var container = document.getElementById('myContainingDiv');
var rowToScrollTo = document.getElementById('myRow');

container.scrollTop = rowToScrollTo.offsetTop;
Run Code Online (Sandbox Code Playgroud)