如何使javascript scrollIntoView顺利? [英] how to make javascript scrollIntoView smooth?

查看:68
本文介绍了如何使javascript scrollIntoView顺利?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个反应​​应用程序中,我有一个方法被调用以将特定节点带入视图,如下所示。

In a react app, I have a method being called to bring a particular node into view as follows.

scrollToQuestionNode(id) {
        const element = document.getElementById(id);
        element.scrollIntoView(false);
}

滚动很好,但滚动动作有点不稳定。我怎样才能让它顺利?我没有看到任何可以为scrollIntoView提供的选项。

The scroll happens fine, but the scroll action is a little jerky. How can I make it smooth? I don't see any options which I can give to scrollIntoView for the same.

推荐答案

这可能有所帮助。

来自scrollIntoView的MDN文档
您可以传入选项而不是布尔值。

From MDN documentation of scrollIntoView You can pass in option instead of boolean.

scrollIntoViewOptions Optional
A Boolean or an object with the following options:
{
  behavior: "auto"  | "instant" | "smooth",
  block:    "start" | "end",
}

所以你可以简单地传递这样的参数。

So you can simply pass parameter like this.

scrollToQuestionNode(id) {
  const element = document.getElementById(id);
  element.scrollIntoView({ block: 'end',  behavior: 'smooth' });
}

参考: https://developer.mozilla.org/en/docs/Web/API/Element/scrollIntoView

这篇关于如何使javascript scrollIntoView顺利?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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