简体   繁体   中英

How to set a timed transition to the .scrollIntoView behavior: smooth?

This is my code:

    document.querySelector(#theId).scrollIntoView({
      behavior: "smooth",
    });

I have gone to the documentation of scrollIntoView and I don't seem to find a way to set the speed of the smooth transition. I would ideally like to set a time in milliseconds to tell the browser the duration the scroll must last (just like it can be done with the transition property in CSS). Do you know if there is a workaround to achieve this? Thank you so much.

There is no way to change the scrolling speed with scrollIntoView() . See the smooth scroll specification :

When a user agent is to perform a smooth scroll of a scrolling box box to position, it must update the scroll position of box in a user-agent-defined fashion over a user-agent-defined amount of time .

But I also don't see any reason why you would want to do that. But if you insist on changing it, you have to do it manually .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM