Jquery 滚动至锚定在内部和外部链接上

Jquery 滚动至锚定在内部和外部链接上,jquery,Jquery,所以我发现自己陷入了困境。我正在使用附带的代码在jQuery中做一个简单的滚动到锚点的操作。当我在页面上并且它在本地滚动到锚点时,滚动到工作,但是当我需要返回滚动到另一个页面上锚点时,我的问题就会出现 var scrollWin = function (selector) { var bar = jQuery(selector).offset().top; bar = bar - 80; jQuery('html, body').animate({ scr

所以我发现自己陷入了困境。我正在使用附带的代码在jQuery中做一个简单的滚动到锚点的操作。当我在页面上并且它在本地滚动到锚点时,滚动到工作,但是当我需要返回滚动到另一个页面上锚点时,我的问题就会出现

var scrollWin = function (selector) {
    var bar = jQuery(selector).offset().top;
    bar = bar - 80;
    jQuery('html, body').animate({
        scrollTop: bar
    }, 1000);
}

jQuery('[href^=#]').click(function(e) {
    scrollWin (jQuery(this).attr('href'));
    return false;
});
我发现了一个代码,当内部页面上的链接从“#div”更改为“/#div”时,可以从外部页面运行,但更改内部页面上的导航链接不是一个选项。非常感谢所有帮助,干杯

更新

所以这就是我最终要做的

jQuery(document).ready(function(){
jQuery('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
        || location.hostname == this.hostname) {

        var target = jQuery(this.hash);
        target = target.length ? target : jQuery('[name=' + this.hash.slice(1) +']');
           if (target.length) {
             jQuery('html,body').animate({
                 scrollTop: target.offset().top - 80
            }, 1000);
            return false;
        }
    }
});
}))

然后对于外部页面,使用锚链接返回页面

jQuery(window).load(function(){
function goToByScroll(id){
   jQuery('html,body').animate({scrollTop: jQuery("#"+id).offset().top - 80},'slow');
} 
if(window.location.hash != '') {
    goToByScroll(window.location.hash.substr(1));
}
}))


现在,如果要为像我这样的粘性导航偏移空间,窗口将在锚点加载,然后设置动画以向上偏移所需的偏移。这不是我想要的理想结果,但这是我能找到的最接近的结果。

Onload检查当前URL中是否有哈希,如果有,滚动到它?