Reputation: 23
The header position is fixed and is 100px tall.
There are anchor tags within the header that goto content. The problem is because of the header when its clicked it doesnt scroll to the correct place. i.e. it scrolls to far.
How can I add vertical space (padding) to that anchor position so it shows correctly?
my code
link:
<li>
<a href="#whoisresponsible">text</a>
</li>
anchor:
<h3>
<a name="whoisresponsible"></a>Q: Who is responsible for Lifeline’s Suburban Garage Sales?
</h3>
here is a js fiddle
http://jsfiddle.net/lmeyer/XqLRg/3/
Upvotes: 2
Views: 1047
Reputation: 1295
jQuery(document).ready(function() {
jQuery(".link").on("click", function(e) {
e.preventDefault();
var name= jQuery(this).attr("href");
var n=name.split("#");
var element = jQuery('[name="'+n[1]+'"]')
var container = jQuery("#mainContainer");
var number = element.offset().top - container.offset().top + container.scrollTop() -190;
jQuery(document).scrollTop(number);
});
});
Upvotes: 1
Reputation: 2809
Try This instead http://jsfiddle.net/TDPP4/
Add CSS classes to h3 and a
<h3 class="Head3">
<a class="InLink" name="whoisresponsible"></a>Q: Who is responsible for Lifeline’s Suburban Garage Sales?
</h3>
Then add this CSS
.Head3{
position:relative;
overflow:hidden;
}
a.InLink{
position:absolute;
margin-top:-150px;
}
Upvotes: 1
Reputation: 386
I'm not sure my answer is correct, because i'm really not understand what your problem.
Set the position of the heading target to be :
relative
or absolute
then when the anchor link clicked you set the position of the target where it is must to stop
Upvotes: 0