Reputation: 3505
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>
...
Upvotes: 2
Views: 3431
Reputation: 64657
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);
});
Upvotes: 4