简体   繁体   English

纯Javascript scrollTo与动画

[英]Pure Javascript scrollTo with animation

I want to covert my code to scroll with animation. 我想隐瞒我的代码以滚动动画。 A lot of solutions I looked up were functions that had to provide element, etc. I already know exactly the amount I need to scroll down. 我查找的许多解决方案都是必须提供元素的函数等。我已经确切知道需要向下滚动的数量。 I just need to animate it. 我只需要设置动画。

reactScrollTo(nextPos){
    let elem = document.getElementById('ref-'+nextPos);
    let amount = elem.offsetTop+530
    window.scrollTo(0, amount);
}

If you're already using React, I would suggest doing your scrolling in a declarative way, instead of manipulating the DOM yourself. 如果您已经在使用React,则建议您以声明性方式进行滚动,而不要自己操作DOM。

Check out something like react-scroll . 签出诸如react-scroll之类的东西。

It allows you to do stuff like: 它允许您执行以下操作:

scroll.scrollMore(100)

Just pop that in whatever lifecycle event you want. 只要在您想要的任何生命周期事件中弹出它即可。

if you want to do window scroll to some specific position then maybe consider use url anchor. 如果您想将窗口滚动到某个特定位置,则可以考虑使用url锚。
check here how to use url anchor 在这里检查如何使用网址锚

if you try to have "transition" effect while scrolling, then consider use "transform" instead of scrolling.(this is more suitable for scroll inside an element, not window itself) 如果尝试在滚动时具有“过渡”效果,请考虑使用“变换”而不是滚动。(这更适合在元素内部滚动,而不是窗口本身)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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