I am trying to add the following functions: when I type a URL with an anchor, I would like to be directly anchored. My problem is that I have a fixed header, and my current jQuery code does not take into account the offset that I set ( 55, ie the height of header).
Here's the code snippet:
if ($(location.href.split("#")[1])) {
var target = $('#'+location.href.split("#")[1]);
if (target.length) {
var hash = this.hash;
$('html,body').animate({ scrollTop: target.offset().top - 55 }, 300, function() {
location.hash = hash;
});
}
}
This piece of code is in the [anchor.js] [1] script.
I am well redirected to the anchor, but no matter what offset value I take, this is not taken into account for the final result, and therefore the anchor name is hidden by a fixed header after the page loads.
If someone can help me fix this problem,
Thanks in advance.
ps: I just want it to work on Firefox and Chrome.
UPDATE
, .
<a name="..."></a> <span> class='direct_anchor' ( .before JQuery), CSS :
.direct_anchor {
display: block;
height: 55px; // Height of fixed header
margin-top: -55px;
visibility: hidden;
}
, , URL :
var target = $('[name="'+location.href.split("#")[1]+'"]');
var hash = location.href.split("#")[1];
if (target.length) {
location.hash = hash;
$('html,body').animate({ scrollTop: target.offset().top - 55 }, 300);
target.before( "<span class='direct_anchor'></span>" );
}
, , class='direct_anchor' CSS, , span.
Jquery, - , ?
.