Hey!
The site we have is just one page and all of the nav links point to #anchor
elements on the page. That was working a-okay. Then we needed to add a contact form as a separate page but with the same nav items. So, I just added the /
so the anchors are /#anchor
. That way, the same nav can be used on the contact form page to point to site root #anchor
.
Example: <a href="/#features">Features and Pricing</a>
refers to either “go to an anchor called features on this page” or “go to an anchor called features on the home page” if you’re on the \contact
page. Does that make sense?
Here’s what I did to the code:
Right at the top of the ('a[href*=#]').click()
function, I check to see what the first character in the href
is:
var intSubstrIndex; // To set substr index.
// If the anchor begins with a /...
if (jQuery(this).attr("href").charAt(0) == "/") {
intSubstrIndex = 2;
}
else {
intSubstrIndex = 1;
}
Then set the index for substr
for offset
and top
. Like:
var offset = jQuery('[name="' + jQuery.attr(this, 'href').substr(intSubstrIndex) + '"]').offset();
That way I’m altering it so I get “features” whether the href
is “/#features” or “#features”.
I don’t know if it’s something that would be useful in the official version. As it is, it’s really specific for us.
Jeremy