简体   繁体   中英

how to add Smooth scroll animation in pure javascript?

I am using pure javascript for vertical scroll.I need this scroll to have smooth behaviour.

 function move_up(scroll_nav) { var container = document.getElementById(scroll_nav); upScroll(container,'up',40,50,10); } function move_down(scroll_nav) { var container = document.getElementById(scroll_nav); upScroll(container,'down',40,50,10); } function upScroll(element,direction,speed,distance,step) { scrollAmount = 0; var slideTimer = setInterval(function(){ if(direction == 'up'){ element.scrollTop -= step; } else { element.scrollTop += step; } scrollAmount += step; if(scrollAmount >= distance){ window.clearInterval(slideTimer); } }, speed); }

I need an animate function similar to the one in this link but without jquery. https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_smooth_scroll_jquery

I need an animate function to be included Please provide an solution.

element.scrollTo({ top: ..., behavior: 'smooth' });

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