Created
October 8, 2020 03:39
-
-
Save b-aleksei/bd715d40efd01d2bdbb8218c07593627 to your computer and use it in GitHub Desktop.
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
const ua = window.navigator.userAgent.toLowerCase(); | |
const isIe = (/trident/gi).test(ua) || (/msie/gi).test(ua); | |
const goToTarget = function (target) { // фолбэк для ie11 | |
const y = target.offsetTop; | |
const moveTo = function () { | |
if (window.pageYOffset < y) { | |
window.scrollBy(0, 60); | |
setTimeout(moveTo); | |
} | |
}; | |
moveTo(); | |
}; | |
const makeSmoothScroll = function (link) { | |
link.addEventListener('click', function (e) { | |
e.preventDefault(); | |
let targetId = link.getAttribute('href'); | |
let target = document.querySelector(targetId); | |
if (target) { | |
if (isIe) { // проверка для ie 11 | |
goToTarget(target); | |
} else { | |
target.scrollIntoView({ | |
behavior: 'smooth', | |
block: 'start', | |
}); | |
} | |
} | |
}); | |
}; | |
const links = document.querySelectorAll('.main-screen__wrap-content a'); | |
if (links.length) { | |
links.forEach((link) => makeSmoothScroll(link)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment