Skip to content

Instantly share code, notes, and snippets.

@procarrera
Created September 6, 2021 16:23
Show Gist options
  • Save procarrera/b81baf2ae0e669537bdad45f79658ada to your computer and use it in GitHub Desktop.
Save procarrera/b81baf2ae0e669537bdad45f79658ada to your computer and use it in GitHub Desktop.
Scroll to Element full compatibility with Safari
function ScrollTo(name) {
//init thread
ScrollToResolver(document.getElementById(name));
}
function ScrollToResolver(elem) {
var jump = parseInt(elem.getBoundingClientRect().top * .2) - 30;
document.body.scrollTop += jump;
document.documentElement.scrollTop += jump;
//lastjump detects anchor unreachable, also manual scrolling to cancel animation if scroll > jump
if (!elem.lastjump || elem.lastjump > Math.abs(jump)) {
elem.lastjump = Math.abs(jump);
setTimeout(function() {
ScrollToResolver(elem);
}, "100");
} else {
elem.lastjump = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment