JQuery Scroll To Top.....not the very top?

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 :slight_smile:

Can’t you just subtract 100 from the targetOffset variable?

Like so…?

$(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(-100).top;
                $('html,body').animate({scrollTop: targetOffset}, 1000);
                return false;
            }
        }
    });
});

That stops it working altogether :frowning:

Beautiful, thank you so much :slight_smile:

Nowhere on the web has a tutorial about that and scroll to top!

Like this:


{scrollTop: targetOffset - 100}

Can this not be done?

Surely there must be a way to tell the js to only scroll to 100px from the top of the browser, rather than the very top?