javascript - body.scrollTop 在严格模式下已弃用。

标签 javascript jquery google-chrome

我想要平滑滚动到内部链接。在 Firefox 中工作正常,但在 Chrome 中我收到错误“body.scrollTop 在严格模式下已弃用”

我的代码是

$(function() {
   $('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;
         }
      }
   });
});

然而,问题是在我改变这个之后

$('html,body').animate({scrollTop: target.offset().top}, 1000);

$('html').animate({scrollTop: target.offset().top}, 1000);

链接根本不起作用,并且控制台中没有错误/警告。

最佳答案

保留'html,body'以定位更多浏览器。

只要 Chrome 显示“已弃用”警告(不是错误),它仍然会响应 body.scrollTop 并且应该像您期望的那样滚动。

我认为造成这种困惑的原因是 Chrome 32 会警告已弃用的功能,但 Chrome 33 使用已弃用的功能且没有任何警告。

关于javascript - body.scrollTop 在严格模式下已弃用。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21886131/

相关文章:

php - 在 $.ajax success 函数中传递两个参数

jquery - jquery 中的 .contains 在 chrome 中不起作用

JQuery 动画 div "like"a 缩放

javascript - 使用 jquery 在 chrome 中使用 scrollto 和 scrolltop 的问题

google-chrome - Chrome 扩展程序统计信息未更新

php - 为什么我的表单向我发送的数据添加选项卡?

javascript - 无法从 CakePHP 3 中的 AJAX 请求获取数据

javascript - 有没有办法将变量从程序内存存储到硬盘保留功能

javascript - 使用 AngularJS 合并来自 WP-API 的结果

javascript - 查找并替换 'src' 属性的部分文件路径