Skip to content

Instantly share code, notes, and snippets.

@LuisPaGarcia
Created October 30, 2020 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuisPaGarcia/2db2d5d6b017276df46042cd67be63ad to your computer and use it in GitHub Desktop.
Save LuisPaGarcia/2db2d5d6b017276df46042cd67be63ad to your computer and use it in GitHub Desktop.
A scrollto function enhanced for IE and
/**
* A scrollto function enhanced
* @param {string} element The query selector of the destiny element
* @param {number} space Space between top and element on scroll (optional)
* @param {bool} notSmooth Define if the scroll speed must be smooth or auto (Default: Smooth)
*/
function scrollToNative(element, space = 0, behaviorScroll = 'smooth') {
const tabsCta = document.querySelector(element).offsetTop
const top = tabsCta + space
const behavior = ['auto', 'smooth'].includes(behaviorScroll)
? behaviorScroll
: 'smooth'
if (
navigator.userAgent.indexOf('MSIE') !== -1 ||
navigator.appVersion.indexOf('Trident/') > -1
) {
window.scrollTo(0, tabsCta + space)
} else {
window.scrollTo({
top,
behavior
})
}
}
export default scrollToNative
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment