Frage

I'm using Twitter Bootstrap 3.0, using the ScrollSpy plugin to activate my "nav" links as I scroll down. Now I want to automatically update the URL in the address bar (using window.history.pushstate) in response to the ScrollSpy event "activate.bs.scrollspy".

How can I identify the "nav" element that ScrollSpy has activated for me?

My code looks something like this.

<body data-spy="scroll" data-target="#primarynavbar">
    <div id="primarynavbar" class="navbar navbar-inverse navbar-fixed-top" role="navigation">
...
...
        <li><a href="#contact">Contact Us</a></li>
...
...
    <div id="contact" />
...
...
    <script>
        $('#primarynavbar').on('activate.bs.scrollspy', function () {
            window.history.pushstate('http://abcd.com#contact')
        });
    </script>
...
War es hilfreich?

Lösung

I would assume you could look for the active class, and get the child anchors href attribute:

$('#primarynavbar').on('activate.bs.scrollspy', function () {
   var hash = $(this).find("li.active a").attr("href");
   window.history.pushstate('http://abcd.com' + hash);
});
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top