0

I am using the ScrollTo and LocalScroll plugins to apply nice scrolling to anchor navigation on this site.

What I would like is to always have correct menu item highlighted as the user scrolls with the scroll bar.

I have seen this question and answer but I'm afraid I can't figure out how to modify it successfully! (My navigation uses #about, #contact etc rather than #section, #section2 etc, but I have given each section a name of section1, section2 etc like in the example)

I have also tried the One Page Navigation plugin which comes with this functionality be default, but it conflicts with my dropdown menu :(

Thanks for taking the time to read this. Hope you can help!

Community
  • 1
  • 1
Caroline Elisa
  • 1,246
  • 6
  • 18
  • 35

1 Answers1

0

If you want to generalize your code to do the same thing both when you are visiting #section1 through an external link, and internally throught <a href="#section1">Section 1</a>, you can listen to the hashchange event. It won't update while scrolling though, but if you generalize the code and make it reusable, I'm sure it's easy to combine the techniques.

Try Ben Alman's jQuery hashchange event plugin and add a callback calling the same function. You might want to use it together with jQuery urlInternal: Easily test URL internal-, external-, or fragment-ness and perhaps jQuery BBQ: Back Button & Query Library, also by Ben Alman.

Here's some code, quickly adapted from a project where I use it in the same way to activate different tabs in a tab row. (I use all three plugins mentioned above, but I don't remember if all of them were necessary for this snippet.)

(function() {
    var menuLinksSelector = "#menu-main-menu a",
        menuLinkHighlightClass = "current";

    $.fn.extend({
        filterLinksToFragment: function(fragment) {
            var $this = $(this),
                $filtered = $this.filter("a:urlFragment").filter(function() {
                    var $this = $(this),
                        thisFragment = $.param.fragment($this.attr('href'));

                    return (thisFragment === fragment);
                });

            return $filtered;
        }
    });

    function highlightMenuItem(fragment) {
        var $menuLinks = $(menuLinksSelector),
            $selectedMenuLink = $menuLinks.filterLinksToFragment(fragment);

        $menuLinks.removeClass(menuLinkHighlightClass);
        $menuItem.addClass(menuLinkHighlightClass);
    }

    function highlightCurrentMenuItem() {
        var currentFragment = $.param.fragment();

        if (currentFragment !== undefined && currentFragment !== "") {
            highlightMenuItem(currentFragment);
        }
    }

    $window.on('hashchange', function() {
        // Activate the current URL's #part-anything link
        highlightCurrentMenuItem();
    });

    // Trigger the event (useful on page load).
    $window.hashchange();
}());

if you use this code, you won't need to use the .click(...) handler you have in your question, except perhaps to cancel the click event, since you have plugins for scrolling.

Joel Purra
  • 24,294
  • 8
  • 60
  • 60
  • Thanks Joel! However my #anchor navigation is working well, so ideally I would just like to use a script that highlights a menu item based on the scroll position, not hash in the URL. I have modified my question to hopefully make this clearer. – Caroline Elisa Sep 06 '12 at 10:24