Javascript JQuery是否滚动到浏览器顶部的偏移?

Javascript JQuery是否滚动到浏览器顶部的偏移?,javascript,jquery,Javascript,Jquery,我有下面的滚动脚本,它可以很好地在页面上滚动,它的工作方式也正是我想要的 $(function(){ $('a[href*=#]').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var $target

我有下面的滚动脚本,它可以很好地在页面上滚动,它的工作方式也正是我想要的

$(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;
            }
        }
    });
});
但是,我需要它忽略顶部,比如200px,因为我在页面顶部有一个固定的标题,内容在后面滚动

这意味着当我滚动到顶部时,它会将内容滚动到固定标题的后面,这样我就看不到它,所以我需要它滚动到标题的正下方。。因此,我想,将标题的底部视为浏览器的顶部

这样做会很方便吗


非常感谢您的帮助

如果您的代码中有这样的条件,您可以使用类似的方法来实现这一点

//check if the absolute position is below header
if ($('#IdOfTheScrollElement').position().top >= 200 ){
//scroll
}
else {
//do nothing
}

你喜欢这个工作吗

var targetOffset = $target.offset().top - 200;
或者获取额外偏移量的标题元素的高度

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

代码很好,您只需要在这里删除固定标题的高度,例如,如果它是200px。它将完美地工作

 $('html,body').animate({scrollTop: (targetOffset().top)-200}, 1000);
您也可以在单击按钮时进行检查

$(function() {

$('a[href*=#]:not([href=#])').click(function() {

    // Check height of the header and padding
    var header_height = $('.header').outerHeight();
从偏移量中删除它

$('html,body').animate({scrollTop: (targetOffset().top) - header_height }, 1000);

谢谢你们的帮助。我不确定它们是否有效,因为我不知道应该把它们放在代码中的什么地方才能起作用,在迷雾中,我只是打破了卷轴:(谢谢你问这个问题,为了完全相同的原因,我整晚都在努力理解如何做到这一点!