Skip to main content
Participant
May 1, 2018
Answered

JavaScript code for Scroll Down anchor ties not working

  • May 1, 2018
  • 1 reply
  • 449 views

Hello!

I have anchor ties that cut to different sections of my webpage. I was given this code in a tutorial so instead of cutting to my anchor ties, the website would scroll down to them. For some reason, it only scrolls down for one of my anchor ties, and the rest of them it still only cuts to them. I would like the Javascript to work so that all my anchor ties will be scrolled to when clicked on. Any help is appreciated! Thank you!

let anchorlinks = document.querySelectorAll('a[href^="#"]')

for (let item of anchorlinks) { // relitere

    item.addEventListener('click', (e)=> {

        let hashval = item.getAttribute('href')

        let target = document.querySelector(hashval)

        target.scrollIntoView({

            behavior: 'smooth',

            block: 'start'

        })

        history.pushState(null, null, hashval)

        e.preventDefault()

    })

}

This topic has been closed for replies.
Correct answer Nancy OShea

Which tutorial and how old is it?

The demo I posted yesterday contains smooth scrolling from a  bit of jQuery code. 

Bootstrap Parallax Template - https://alt-web.com/

1 reply

Nancy OShea
Community Expert
Nancy OSheaCommunity ExpertCorrect answer
Community Expert
May 1, 2018

Which tutorial and how old is it?

The demo I posted yesterday contains smooth scrolling from a  bit of jQuery code. 

Bootstrap Parallax Template - https://alt-web.com/

Nancy O'Shea— Product User & Community Expert