设置滚动条位置

发布于 2024-09-15 17:03:00 字数 768 浏览 9 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

狼性发作 2024-09-22 17:03:00

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

假设您可以使用 ID 以及包含的

轻松识别要滚动到的行。使用 offsetTop您想要滚动到的元素。

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

container.scrollTop = rowToScrollTo.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 the offsetTop of the element you want to scroll to.

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

container.scrollTop = rowToScrollTo.offsetTop;
狂之美人 2024-09-22 17:03:00

你可以使用这个:

$('a').on('click', function(e){
  var t = this.id.substring(1);
  e.preventDefault();
  var target= $(".section")[t] ;
  var offset = $( target ).offset();
  $('html, body').stop().animate({
    scrollTop: offset.top
  }, 1000);
});

另外,你可以检查这个 jsfiddle 上的示例

You can use this:

$('a').on('click', function(e){
  var t = this.id.substring(1);
  e.preventDefault();
  var target= $(".section")[t] ;
  var offset = $( target ).offset();
  $('html, body').stop().animate({
    scrollTop: offset.top
  }, 1000);
});

Also, you could check this example on jsfiddle .

樱&纷飞 2024-09-22 17:03:00

使用Jquery:

'.scroll-table' 是可滚动部分使用的类名

1.首先转到初始位置

$('.scroll-table').scrollTop(0);

2.下面一行计算表格顶部位置之间的滚动差异以及我们滚动到的行

$('.scroll-table').scrollTop($('#myrowid').position().top-$('.scroll-table').position().top);

Using Jquery:

'.scroll-table' is the class name used for scroll-able part

1.First go to the initial position

$('.scroll-table').scrollTop(0);

2.Below line calculates the scroll difference between the tables top position and the row to which we have scroll

$('.scroll-table').scrollTop($('#myrowid').position().top-$('.scroll-table').position().top);
空城旧梦 2024-09-22 17:03:00

如果你对这里的 jQuery 感兴趣,可以按如下方式进行:

$(".tableClass tbody").scrollTop(0);

If you are interested in jQuery here, you can do it as follows:

$(".tableClass tbody").scrollTop(0);
浅语花开 2024-09-22 17:03:00

您还可以使用 .scrollTop() jQuery 方法:

https://api.scrollTop()。 jquery.com/scrollTop/

You can also use the .scrollTop() jQuery method:

https://api.jquery.com/scrollTop/

表情可笑 2024-09-22 17:03:00

有一个使用 AngularJs 的解决方案。

  $document.scrollTopAnimated(50, 1000).then(function () {});

第一个参数是位置。第二个是动画的持续时间。

There is a solution using AngularJs.

  $document.scrollTopAnimated(50, 1000).then(function () {});

The first parameter is the position. The second is the duration of animation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文