Avatar of Mark Bran
Mark Bran

asked on 

Highlight menu item on scroll

Need help here ....
I am having difficulty here .... onClick the below code highlights my menu item as it should but
this is with the click event
I need to do the same thing but onScroll I need to highlight the menu item as I am doing below ... I guess using the active class

1. When user scrolls to next section  the menu item should be highlighted - using the active class

  let actLink = document.querySelector("#homeLink");
actLink.classList.add("active"); // Add .active class to Home main Landing Page
// onclick remove and or add .active
function activeFunction(e) {
   let els = document.querySelector(".active");
   if (els !== null) {
      els.classList.remove("active");
   }
   e.target.className = "active";
}

Open in new window

JavaScript

Avatar of undefined
Last Comment
leakim971
Avatar of Mark Bran
Mark Bran

ASKER

Thought I would add the HTML sections
<section id='home'></section>
<section id='about'></section>
<section id='team'></section>
<section id='contact'></section>

Open in new window


Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

please provide a link to your page
Avatar of leakim971
leakim971
Flag of Guadeloupe image

test page : http://leak.im/29178037/


        let homePosY = document.querySelector("#home").offsetTop;
        let aboutPosY = document.querySelector("#about").offsetTop;
        let teamPosY = document.querySelector("#team").offsetTop;
        let contactPosY = document.querySelector("#contact").offsetTop;
        let curActive = document.querySelector(".active");
        if(curActive) {
            curActive.classList.remove("active");
        }
        if(window.scrollY>=(homePosY-8)&&window.scrollY<aboutPosY) {
            document.getElementById("homeLink").className = "active";
        } else if(window.scrollY>=aboutPosY&&window.scrollY<teamPosY) {
            document.getElementById("aboutLink").className = "active";
        } else if(window.scrollY>=teamPosY&&window.scrollY<contactPosY) {
            document.getElementById("teamLink").className = "active";
        } else if(window.scrollY>=contactPosY) {
            document.getElementById("contactLink").className = "active";
        }
    });
    window.dispatchEvent(new Event("scroll"));

Open in new window


With :
<div class="stick" id="menus">
    <span><a href="#home" id="homeLink">Home</a></span>
    <span><a href="#about" id="aboutLink">About</a></span>
    <span><a href="#team" id="teamLink">Team</a></span>
    <span><a href="#contact" id="contactLink">Contact</a></span>
</div>
<section id='home'>
    <div style="height:1300px;background-color: #0c60b3;"></div>
</section>
<section id='about'>
    <div style="height:1300px;background-color: rosybrown;"></div>
</section>
<section id='team'>
    <div style="height:1300px;background-color: yellowgreen"></div>
</section>
<section id='contact'>
    <div style="height:1300px;background-color: darkgoldenrod"></div>
</section>

Open in new window

Avatar of Mark Bran
Mark Bran

ASKER

at bo9ttom of JS code getting an error ... red squigly here --->  });    at line 18:
Avatar of Mark Bran
Mark Bran

ASKER

Here is my codepen I am working on
https://codepen.io/jsMarko/pen/eYNXyoZ
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo