Created
September 6, 2021 16:23
-
-
Save procarrera/b81baf2ae0e669537bdad45f79658ada to your computer and use it in GitHub Desktop.
Scroll to Element full compatibility with Safari
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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