Take the 2-minute tour ×

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

        && 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

share|improve this question
add comment

2 Answers

up vote 10 down vote accepted

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);
share|improve this answer
add comment

You could use something like this if condition on ur code to do that

//check if the absolute position is below header
if ($('#IdOfTheScrollElement').position().top >= 200 ){
else {
//do nothing
share|improve this answer
Thanks guys for your help. I'm not sure if they work though as i couldn't figure out where to put them in the code to have an effect, in mist cases i just broke the scroll :( –  Jezthomp Oct 22 '10 at 10:12
add comment

Your Answer


