/*
 * duration: scroll duration in milliseconds
 * y: the y coordinate to scroll, 0 = top
 */
function scrollToY(duration, y) {
  var cosParameter = (window.scrollY - y) / 2,
  		scrollCount = 0,
			oldTimestamp = performance.now();
  function step (newTimestamp) {
    scrollCount += Math.PI / (duration / (newTimestamp - oldTimestamp));
    if (scrollCount >= Math.PI) window.scrollTo(0, y);
    if (window.scrollY === y) return;
    window.scrollTo(0, Math.round((cosParameter + y) + cosParameter * Math.cos(scrollCount)));
		oldTimestamp = newTimestamp;
    window.requestAnimationFrame(step);
  }
	window.requestAnimationFrame(step);
}

/*
 * duration: scroll duration in milliseconds
 * this function is using the scrollToY function
 */
function scrollToTop(duration) {
  scrollToY(duration, 0);
}

/*
 * duration: scroll duration in milliseconds
 * id: the id of the element as a string
 * this function is using the scrollToY function
 */
function scrollToId(duration, id) {
	var offset = document.getElementById(id).getBoundingClientRect().top;
	scrollToY(duration, window.scrollY + offset);
}

/*
 * duration: scroll duration in milliseconds
 * element: a node object
 * this function is using the scrollToY function
 */
function scrollToElement(duration, element) {
	var offset = element.getBoundingClientRect().top;
	scrollToY(duration, window.scrollY + offset);
}

@@ -4,92 +4,216 @@
 %0A * 
-duration: scroll duration in milliseconds%0D%0A * y: the y coordinate to scroll, 0 = top
+y: the y coordinate to scroll, 0 = top%0D%0A * duration: scroll duration in milliseconds; default is 0 (no transition)%0D%0A * element: the html element that should be scrolled ; default is the main scrolling element
 %0D%0A *
@@ -233,25 +233,29 @@
 crollToY
-(
+ (y, 
 duration
 , y) %7B%0D%0A
@@ -250,21 +250,146 @@
 tion
-, y) %7B%0D%0A  var
+ = 0, element = document.scrollingElement) %7B%0D%0A  // cancel if already on target position%0D%0A  if (element.scrollTop === y) return;%0D%0A%0D%0A  const
  cos
@@ -401,30 +401,33 @@
 eter = (
-window
+element
 .scroll
-Y
+Top
  - y) / 
@@ -431,15 +431,17 @@
  / 2
-,
+;
 %0D%0A  
-%09%09
+let 
 scro
@@ -456,13 +456,9 @@
 = 0,
-%0D%0A%09%09%09
+ 
 oldT
@@ -472,26 +472,15 @@
 p = 
-performance.now();
+null;%0D%0A
 %0D%0A  
@@ -511,16 +511,108 @@
 amp) %7B%0D%0A
+    if (oldTimestamp !== null) %7B%0D%0A      // if duration is 0 scrollCount will be Infinity%0D%0A  
     scro
@@ -634,21 +634,9 @@
 .PI 
-/ (duration /
+*
  (ne
@@ -661,20 +661,32 @@
 mestamp)
-)
+ / duration
 ;%0D%0A
+  
     if (
@@ -709,22 +709,30 @@
 ath.PI) 
-window
+return element
 .scrollT
@@ -736,91 +736,42 @@
 llTo
-(0,
+p =
  y
-)
 ;%0D%0A    
-if (window.scrollY === y) return;%0D%0A    window.scrollTo(0, Math.round((
+  element.scrollTop = 
 cosP
@@ -782,17 +782,16 @@
 eter + y
-)
  + cosPa
@@ -825,11 +825,16 @@
 unt)
-));
+;%0D%0A    %7D
 %0D%0A%09%09

update scroll to y function to work for every element; fixed timestamp bug