Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
133
rated 0 times [  136] [ 3]  / answers: 1 / hits: 63976  / 14 Years ago, thu, october 21, 2010, 12:00:00

I have the following scroll script, which scrolls round the page fine, works exactly how i want it too.



$(function(){
$('a[href*=#]').click(function() {
if (location.pathname.replace(/^//,'') == this.pathname.replace(/^//,'')
&& location.hostname == this.hostname) {
var $target = $(this.hash);
$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
if ($target.length) {
var targetOffset = $target.offset().top;
$('html,body').animate({scrollTop: targetOffset}, 1000);
return false;
}
}
});
});


However, i need it to ignore the top say 200px as i have a fixed header at the top of the page that the content scrolls behind.



Meaning that when i scroll to top it scrolls the content to behind the fixed header so i cannot see it, so i need it to scroll to just below the header.. so to treat the bottom of the header as the top of the browser i suppose....



Can this be done as it would be very handy?



Many thanks for any help


More From » jquery

 Answers
30

Would something like this work?



var targetOffset = $target.offset().top - 200;


Or grab the height of the header element for the extra offset.



var targetOffset = $target.offset().top - $(element).outerHeight(true);

[#95222] Wednesday, October 20, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaitlynnb

Total Points: 402
Total Questions: 96
Total Answers: 109

Location: Trinidad and Tobago
Member since Fri, May 8, 2020
4 Years ago
kaitlynnb questions
;