Created
October 30, 2020 21:50
-
-
Save LuisPaGarcia/2db2d5d6b017276df46042cd67be63ad to your computer and use it in GitHub Desktop.
A scrollto function enhanced for IE and
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
/** | |
* 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