Avatar of Refael
Refael
Flag for United States of America asked on

jquery.scrollTo active anchor link on scroll

I am using the jquery.scrollTo plugin from jquery.scrollTo.
I am trying to figure out how to activate the anchor link on scroll. Can someone help or suggest?
JavaScriptjQuery

Avatar of undefined
Last Comment
Refael

8/22/2022 - Mon
Refael

ASKER
Here is my current code. Again I am trying to find out how to set "active state" on the anchor link while scrolling.

$('ul#submenu-page li a').click(function(e) {		
		e.preventDefault();
		
		var subMenuId = $(this).attr("href");				
		$('ul#submenu-page li').removeClass("active");
		$(this).parent("li").addClass("active");
		
		if (subMenuId==='#section1') {
			$('body, html').scrollTo(0, 1000, {easing:"easeInOutExpo"});			
		} else {		
			$('body, html').scrollTo("#page " + subMenuId, 1000, {easing: "easeInOutExpo", offset:{top:-180}});
		}
	});

Open in new window

skij

Try this:
$('ul#submenu-page li a').click(function(e) {		
		e.preventDefault();
		
		var subMenuId = $(this).attr("href");				
		$('ul#submenu-page li').removeClass("active");
		$(this).parent("li").addClass("active");
		
		if (subMenuId==='#section1') {
			$('body, html').scrollTo(0, 1000, {easing:"easeInOutExpo"});			
		} else {		
			$('body, html').scrollTo("#page " + subMenuId, 1000, {easing: "easeInOutExpo", offset:{top:-180}});
			$("#page " + subMenuId).addClass("active");

		}
	});

Open in new window

Refael

ASKER
skij thank you but....

This is on click function. I talking about the scroll. So when the user scrolls the page and enter e.g. section2 then in the menu with the class "section2" will set to active. Do you know what i mean?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
skij

Refael

ASKER
skij :-) I actually bookmarked this page while I was searching for the solution.
Yet my current code works so nicely so I do not want to refine everything.
If you will tell me that that's the easy way to go then I will have no choice but to delete my codes and take this one.
ASKER CERTIFIED SOLUTION
skij

THIS SOLUTION 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
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Refael

ASKER
skij you are one of the brilliants :-)  Thank you so much this was very helpful.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.