Just developing your code:
var myScroll = function(to){ var currentScroll = window.scrollY, down = currentScroll < to; //вниз крутим или вверх var scr = setInterval(function () { currentScroll = currentScroll + 10 * (down ? 1 : -1); window.scrollTo(0, currentScroll); if((down && currentScroll > to) || (!down && currentScroll < to)){ clearInterval(scr); } }, 15); }
Use: myScroll(600) .
But I do not consider it a good idea to use the interval here, and I would generally be repelled by the duration of the animation and count the number of steps and their frequency.
Instead of setInterval, I’m used to using a more secure option.
setTimeout(function(){ //do something if(continue){ setTimeout(arguments.callee, 20); } }, 20);
although in this case, I confess, I don’t see any difference with setInterval.
I usually went about another way when creating animation: I took the argument as the duration of the animation, divided it by the duration of one frame (20ms, for example) and also divided the change in the parameter being animated. Thus, I calculated how much one parameter needs to be changed and how many iterations it takes. I set the timers to 20ms and made changes to them by the calculated value.
It's useful now to find an indicative code and found an article about creating animation that I had never met before. At first glance, it is very interesting: https://learn.javascript.ru/js-animation