Skip to content

Instantly share code, notes, and snippets.

@b-aleksei
Created October 8, 2020 03:39
Show Gist options
  • Save b-aleksei/bd715d40efd01d2bdbb8218c07593627 to your computer and use it in GitHub Desktop.
Save b-aleksei/bd715d40efd01d2bdbb8218c07593627 to your computer and use it in GitHub Desktop.
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