I need to execute the javascript method scrollTo(x, y) with animation. I cant' use jQuery to do it.
[Working demo]
function interpolate( source,target,pos ) {
return ( source + (target - source) * pos );
}
function easing( pos ) {
return ( -Math.cos( pos * Math.PI ) / 2 ) + 0.5;
}
function scrollTop( duration ) {
duration = duration || 1000;
var startY = window.pageYOffset,
start = Number(new Date()),
finish = start + duration;
var interval = setInterval(function() {
var now = Number(new Date()),
pos = (now > finish) ? 1 : (now - start) / duration;
scrollTo(0, interpolate( startY, 0, easing(pos) ));
if ( now > finish )
clearInterval( interval );
}, 15);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With