Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrolling with animation

I need to execute the javascript method scrollTo(x, y) with animation. I cant' use jQuery to do it.

like image 883
Alex Avatar asked Nov 02 '10 22:11

Alex


1 Answers

[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);
};
like image 76
25 revs, 4 users 83% Avatar answered Oct 10 '22 01:10

25 revs, 4 users 83%