Skip to content

Instantly share code, notes, and snippets.

@b-aleksei
Last active August 21, 2020 09:08
Show Gist options
  • Save b-aleksei/cbb74423823ce48e499f63c56bd96187 to your computer and use it in GitHub Desktop.
Save b-aleksei/cbb74423823ce48e499f63c56bd96187 to your computer and use it in GitHub Desktop.
smooth-scroll
const ua = window.navigator.userAgent.toLowerCase();
const isIe = (/trident/gi).test(ua) || (/msie/gi).test(ua);
if (isIe) {
const makeSmoothScroll = function (link) {
link.addEventListener('click', function (e) {
e.preventDefault();
let targetId = link.getAttribute('href');
let target = document.querySelector(targetId);
if (target) {
const moveTo = function () {
if (window.pageYOffset < target.offsetTop) {
window.scrollBy(0, 60);
setTimeout(moveTo);
}
};
moveTo();
}
});
};
const links = document.querySelectorAll('.nav__link');
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