JQuery从浏览器顶部滚动到Offset?

Jez*_*omp 21 javascript jquery

我有以下滚动脚本,滚动页面很好,工作正是我想要的.

$(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;
            }
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

但是,我需要它忽略顶部说200px,因为我在页面顶部有一个固定的标题,内容滚动后面.

这意味着,当我滚动到顶部时,它将内容滚动到固定标题后面,所以我看不到它,所以我需要它滚动到标题下方..所以将标题的底部视为浏览器的顶部我假设....

可以这样做,因为它会非常方便吗?

非常感谢任何帮助

tin*_*fni 32

会这样的吗?

var targetOffset = $target.offset().top - 200;
Run Code Online (Sandbox Code Playgroud)

或者抓住标题元素的高度以获得额外的偏移量.

var targetOffset = $target.offset().top - $("element").outerHeight(true);
Run Code Online (Sandbox Code Playgroud)