preventDefault,滚动Firefox

时间:2016-07-24 13:56:40

标签: javascript jquery firefox scrolltop

我在jQuery中有这个脚本,它滚动到想要的部分但是间隔到顶部。它适用于所有经过测试的浏览器......除了firefox。

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE 'have_%ssl';
+---------------+----------+
| Variable_name | Value    |
+---------------+----------+
| have_openssl  | YES      |
| have_ssl      | DISABLED |
+---------------+----------+

https://jsfiddle.net/5L3xyuuv/1/

2 个答案:

答案 0 :(得分:0)

在此尝试此操作,将HTMLBODY添加到scrollTop()方法,这在所有浏览器上都能正常运行。



$('nav > *').click(function(event){
    event.preventDefault();
    var navClicked = $(this).index();
    var elem = $(this).attr("href");
    $('html,body').scrollTop($(elem).offset().top - 48);
});

nav {
  width: 100%;
  height: 48px;
  background-color: red;
  position: fixed;
  top:0;
}

#about{
  width:400px;
  height:400px;
  background:#111;
}

#benefits{
  width:400px;
  height:400px;
  background:#ccc;
}

#team{
  width:400px;
  height:400px;
  background:#f22;
}

#path{
  width:400px;
  height:400px;
  background:#f2f;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
  <a href="#about">about</a>
  <a href="#benefits">benefits</a>
  <a href="#team">team</a>
  <a href="#path">path</a>
</nav>

<div id="about"></div>
<div id="benefits"></div>
<div id="team"></div>
<div id="path"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

(文档)$ .scrollTop(...); 解决了这个问题。