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?
RefaelAsked:
Who is Participating?
 
skijCommented:
The concept there should work with your page, however without seeing all of you code, I don't know for sure.

If you look at it piece by piece you should be able to figure out how to implement the concept.  Here is something to get you started:
$(document).ready(function () {
    $(document).on("scroll", onScroll);
});

function onScroll(event){
    var scrollPos = $(document).scrollTop();
    $('ul#submenu-page li a').each(function () {
        var currLink = $(this);
        var refElement = $(currLink.attr("href"));
        if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
            $('ul#submenu-page li a').removeClass("active");
            currLink.addClass("active");
        }
        else{
            currLink.removeClass("active");
        }
    });
}

Open in new window

0
 
RefaelAuthor Commented:
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

0
 
skijCommented:
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

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
RefaelAuthor Commented:
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?
0
 
skijCommented:
0
 
RefaelAuthor Commented:
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.
0
 
RefaelAuthor Commented:
skij you are one of the brilliants :-)  Thank you so much this was very helpful.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.