0

My smoothscroll script works fine with other browsers. But I can't seem to make it work on Internet Explorer. Any thoughts?

let anchorLinks = document.querySelectorAll('a[href^="#"]')
let headerHeight = $('header').outerHeight();
for (let item of anchorLinks) {
  item.addEventListener('click', (e) => {
    let hashVal = item.getAttribute('href')
    let topOfElement = document.querySelector(hashVal).offsetTop - headerHeight

    window.scroll({ top: topOfElement, behavior: "smooth" })
    history.pushState(null, null, hashVal)
    e.preventDefault()
  })
}
Devi
  • 51
  • 1
  • 9

1 Answers1

0

You can apply jQuery.animate on the scrollTop of the HTML and body elements.

let anchorLinks = document.querySelectorAll('a[href^="#"]')
let headerHeight = $('header').outerHeight();
for (let item of anchorLinks) {
  item.addEventListener('click', (e) => {
    let hashVal = item.getAttribute('href')
    let topOfElement = document.querySelector(hashVal).offsetTop - headerHeight
    $('html, body').animate({
       scrollTop: topOfElement
    }, 500);
    history.pushState(null, null, hashVal)
    e.preventDefault()
  })
}
Unmitigated
  • 76,500
  • 11
  • 62
  • 80