Skip to content

Instantly share code, notes, and snippets.

@MatthieuScarset
Forked from moeamaya/getOffset.js
Created April 7, 2017 17:00
Show Gist options
  • Save MatthieuScarset/bc0321bad1d5ee89890270b94db7cf90 to your computer and use it in GitHub Desktop.
Save MatthieuScarset/bc0321bad1d5ee89890270b94db7cf90 to your computer and use it in GitHub Desktop.
// Get element offset from top of page
function getOffset(el) {
el = el.getBoundingClientRect();
return {
left: el.left + window.scrollX,
top: el.top + window.scrollY
};
};
// Scroll element to a integer position
function scrollTo(element, to, duration) {
if (duration <= 0) return;
var difference = to - element.scrollTop;
var perTick = difference / duration * 10;
setTimeout(function() {
element.scrollTop = element.scrollTop + perTick;
if (element.scrollTop === to) return;
scrollTo(element, to, duration - 10);
}, 10);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment