Javascript 为什么滚动不';工作不正常吗?

Javascript 为什么滚动不';工作不正常吗?,javascript,jquery,html,dom,scroll,Javascript,Jquery,Html,Dom,Scroll,链接到jsfiddle- 我想做一个平滑的卷轴。但当我用锚点计算链接的坐标时,它会返回不同的结果。它看起来像是从视口开始计算坐标,而不是从文档开始。它在codepen上工作,但在local和jsfiddle上不工作。css被禁用。也许您知道它有什么问题?使用以下代码: $('a[href*="#"]:not([href="#"])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.r

链接到jsfiddle-


我想做一个平滑的卷轴。但当我用锚点计算链接的坐标时,它会返回不同的结果。它看起来像是从视口开始计算坐标,而不是从文档开始。它在codepen上工作,但在local和jsfiddle上不工作。css被禁用。也许您知道它有什么问题?

使用以下代码:

$('a[href*="#"]:not([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) {
            $('html, body').animate({
                scrollTop: target.offset().top
            }, 1000);
            return false;
        }
    }
});

使用以下代码:

$('a[href*="#"]:not([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) {
            $('html, body').animate({
                scrollTop: target.offset().top
            }, 1000);
            return false;
        }
    }
});

var hash=this.hash
?可能你的意思是
var hash=this.href
我不明白怎么回事?!问题出在哪里?$('html,body').animate({scrollTop:top},5000);增加滚动时间。当你点击两个链接(服务点击此链接进行滚动)时,它会更轻松。底部有两个链接。这些链接工作不正确。因此,您不能使用不在页面顶部的链接。
var hash=this.hash
?可能你的意思是
var hash=this.href
我不明白怎么回事?!问题出在哪里?$('html,body').animate({scrollTop:top},5000);增加滚动时间。当你点击两个链接(服务点击此链接进行滚动)时,它会更轻松。底部有两个链接。这些链接工作不正确。因此,您不能使用不在页面顶部的链接。这是因为页面的剩余高度小于滚动区域。这是它可以滚动的最大值。:)你找到解决办法了吗?是的。有人告诉我,这是jQuery的bug。解决方案是在高度不等于0的块上创建#链接。感谢您的帮助这是因为页面的剩余高度小于滚动区域。这是它可以滚动的最大值。:)你找到解决办法了吗?是的。有人告诉我,这是jQuery的bug。解决方案是在高度不等于0的块上创建#链接。谢谢你的帮助