JavaScript animated scrollTo

JavaScript easing Scroll to Element, ohne großes Framework.

animatedScrollTo.js JavaScript (981 bytes) 17.04.2014 11:03
window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       ||
          window.webkitRequestAnimationFrame ||
          window.mozRequestAnimationFrame    ||
          function(callback){
            window.setTimeout(callback, 1000 / 60);
          };
})();

function animateScrollTo(id) {
  var node = document.getElementById(id);
  if (node) {
    var to = 0;
    var from = window.pageYOffset;
    do {
      to += node.offsetTop;
//      from += node.offsetParent ? node.offsetParent.scrollTop : 0;
      node = node.offsetParent;
    } while (node)
    if (from == to) return;
    var start = Date.now();
    var tick = function() {
      var t = Math.min(1, ((Date.now() - start) / 1000));
      var eased =  t<.5 ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1;
      var val = (eased * (to - from)) + from;
      window.scrollTo(0, val);
      if (t<1) { 
        window.requestAnimFrame(tick);
      }
    };
    tick();
  }
}

Author: , published: , last modified:

Kontakt

Udo Schmal

Udo Schmal
Softwareentwickler
Olvengraben 41
47608 Geldern
Nordrhein-Westfalen
Germany





+49 2831 9776557
+49 1575 0663676
+49 2831 1328709
SMS
WhatsApp

Instagram Profile
vCard 3.0

Service Infos

CMS Info Product Name:
UDOs Webserver
Version:
0.5.0.101
Description:
All in one Webserver
Copyright:
Udo Schmal
Compilation:
Sun, 18. Jul 2021 17:42:24
Development Info Compiler:
Free Pascal FPC 3.3.1
compiled for:
OS:Linux, CPU:x86_64
System Info OS:
Ubuntu 20.04.2 LTS (Focal Fossa)
Hardware Info Model:
Hewlett-Packard HP Pavilion dv7 Notebook PC
CPU Name:
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz
CPU Type:
x86_64, 1 physical CPU(s), 2 Core(s), 4 logical CPU(s), 1121.995 MHz