Javascript 平滑滚动不工作jQuery

Javascript 平滑滚动不工作jQuery,javascript,jquery,html,css,Javascript,Jquery,Html,Css,使用来自和为我这样的人编写的代码无法理解为什么它不起作用 脚本: $('a[href*=#]:not([href=#])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') || location.hostname == this.hostname) { var target = $(this.hash); target = ta

使用来自和为我这样的人编写的代码无法理解为什么它不起作用

脚本:

$('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;
    }
}
});
网站:

有什么想法吗?站点跳转到div容器,但不设置动画。

查看以下答案:

我在safari和chrome(Mac)中遇到了这个问题,并发现.scrollTop可以在$(“body”)上工作,但不能在$(“html,body”)上工作

要使其在Safari中工作,请将脚本更改为:

$('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) {
      var bodyElement = $("html,body");
      if ($.browser.safari) { 
        bodyElement = $("body")
      }

      bodyElement.animate({
        scrollTop: target.offset().top
      }, 1000);

      return false;
    }
  }
});

在您的问题中也向我们展示您的HTML,这是一种更好更快的方式,让我们帮助您为我工作。它到底在哪里不起作用?请提供一个工作流来重现您的问题。@jahller似乎不在safari中工作。尝试此操作后,它在所有浏览器中都完全停止工作。