I am trying to get scrollspy to work when using an absolute url, not a # binding.
For example, I can easily run scrollspy using only <a href="#section1"></a>
I would like to be able to run scrollspy using <a href="http://myurl.com/#section1"></a>
The reason for this is the main navigation on the scorllspy homepage, but the blog is not part of the homepage. When you visit the blog, and then clicking the somthing link on the main page, you will get the route http://myurl.com/blog/#section1 , and not the identifier of the home page.
I found this solution ( use url and hash with scrollspy bootstrap ) but couldn't make it fully functional. After some tweaking and many combinations of regular expressions, I could make her add an active class to the first menu item, but it will not update.
Any thoughts on how to make this work? Is there an alternative js library I should use?
Solution thanks to Troy
jQuery(function($){ // local url of page (minus any hash, but including any potential query string) var url = location.href.replace(/#.*/,''); // Find all anchors $('.nav-main').find('a[href]').each(function(i,a){ var $a = $(a); var href = $a.attr('href'); // check is anchor href starts with page URI if (href.indexOf(url+'#') == 0) { // remove URI from href href = href.replace(url,''); // update anchors HREF with new one $a.attr('href',href); } // Now refresh scrollspy $('[data-spy="scroll"]').each(function (i,spy) { var $spy = $(this).scrollspy('refresh') }) }); $('body').scrollspy({ target: '.nav-main', offset: 155 }) });
I added $('body').scrollspy({ target: '.nav-main', offset: 155 }) to get the data offset after reapplying after updating scrollspy. After some trial and error, this is the only solution I could find.
href twitter-bootstrap scrollspy
Smashbrando
source share