小编use*_*601的帖子

JS scrollTop在Safari浏览器上不起作用

我使用以下代码在菜单链接和内容行容器之间进行平滑滚动。除Safari之外,这在所有浏览器上似乎都可以正常工作。

我知道我需要在偏移线内定义“ html”和“ body”,但是,我不确定是否正确执行了此操作。希望对此有所帮助!谢谢。

$(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;
      }
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

javascript safari smooth-scrolling scrolltop

5
推荐指数
0
解决办法
582
查看次数

标签 统计

javascript ×1

safari ×1

scrolltop ×1

smooth-scrolling ×1