export function scrollTop(el: any, from = 0, to: number, duration = 500, endCallback?: Function) { if (!window.requestAnimationFrame) { window.requestAnimationFrame = ( function (callback) { return window.setTimeout(callback, 1000/60); } ); } const difference = Math.abs(from - to); const step = Math.ceil(difference / duration * 50); function scroll(start: number, end: number, step: number) { if (start === end) { endCallback && endCallback(); return; } let d = (start + step > end) ? end : start + step; if (start > end) { d = (start - step < end) ? end : start - step; } if (el === window) { window.scrollTo(d, d); } else { el.scrollTop = d; } window.requestAnimationFrame(() => scroll(d, end, step)); } scroll(from, to, step); }