平滑滚动Javascript在一台Mac(Chrome)上失败但在另一台Mac(Chrome)上运行

时间:2017-09-20 16:49:54

标签: javascript

我们有一个平滑的滚动脚本在某些Mac机器上失败(最新的chrome)。

我们稍微调整了一下脚本,现在它似乎正在所有Mac上运行。

但我们不知道为什么。

有人可以帮忙吗?

之前(无法为某些Mac / Chrome测试机正确滚动):

var topID = (ua("safari")) ? "body" : "html";

$(".SmoothScroll").unbind().click(function(){
    var link = $(this).attr("href");
    if(link.charAt(0)=="#" && link.charAt(1)!="") {
        var offset = $(link).offset();
        var tid = setTimeout(function() {
            $(topID).stop().animate({scrollTop: offset.top}, 800, "easeInOutCubic", function() {
                location.href = link;
            });
        }, 10);
        return false;
    }
});

修复后(适用于所有机器)

//var topID = (ua("safari")) ? "body" : "html";
var $root = $('html, body');

$(".SmoothScroll").unbind().click(function(){
    var link = $(this).attr("href");
    if(link.charAt(0)=="#" && link.charAt(1)!="") {
        var offset = $(link).offset();
        var tid = setTimeout(function() {
            $root.stop().animate({scrollTop: offset.top}, 800, "easeInOutCubic", function() {
                location.href = link;
            });
        }, 10);
        return false;
    }
});

基本上将$(topID)更改为$('html,body')似乎正在运行。但为什么?发生了什么以及上一个脚本在某些Mac + Chrome上失败的原因是什么?

1 个答案:

答案 0 :(得分:0)

你可能在两台mac上都没有相同版本的safari