简体   繁体   English

脚本适用于chrome,但不适用于Firefox

[英]Script works in chrome but not in firefox

I have a script that works just fine in chrome but not in Firefox and I have no idea why this is. 我有一个脚本在Chrome中运行得很好但在Firefox中没有,我不知道为什么会这样。 The script is suppose to scroll down to an id from an anchor but does nothing in Firefox. 该脚本假设从锚点向下滚动到一个id,但在Firefox中什么都不做。

Example how I use the script below! 我如何使用下面的脚本示例!

<nav>
  <ul>
    <li><a class="scroll" target="home">Home</a></li>
  </ul>
</nav>

<div id="home">
.....
</div>

<script>
  $('.scroll').click(function() {
  $(document).animate({
  scrollTop: eval($('#' + $(this).attr('target')).offset().top - 70)
  }, 1000);
  });
</script>

Change $(document).animate to $('html,body').animate . $(document).animate更改$(document).animate $('html,body').animate

http://jsfiddle.net/mblase75/vL79H/ http://jsfiddle.net/mblase75/vL79H/


That said, I would tighten up your code a bit by using HTML-standard hash links, in case JavaScript is disabled or not working: 也就是说,如果JavaScript被禁用或不工作,我会使用HTML标准哈希链接收紧你的代码:

<li><a class="scroll" href="#home">Home</a></li>

Then modify the code to accommodate it and remove the unnecessary eval statement: 然后修改代码以适应它并删除不必要的eval语句:

$('.scroll').click(function (e) {
    e.preventDefault();
    $('html,body').animate({
        scrollTop: $($(this).attr('href')).offset().top - 70
    }, 1000);
});

http://jsfiddle.net/4FQn7/ http://jsfiddle.net/4FQn7/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery脚本在Firefox中有效,在Chrome中停止 - jQuery script works in Firefox, stops in Chrome 该脚本可在Firefox和chrome中使用,但不适用于即jquery未定义 - the script works in firefox and chrome but not in ie, jquery undefined JQuery脚本,可在Firefox中运行,而不能在IE,Chrome中运行 - JQuery Script, Works in Firefox, not in IE, Chrome PHP脚本和HTML - 这适用于Firefox和Chrome,但不适用于IE - PHP Script and HTML - This works in Firefox and Chrome, but not IE 脚本在 Chrome 和 IE 中部分有效,但在 Firefox 中无效 - Script works partially in Chrome and IE but not in Firefox Tampermonkey 的 MutationObserver 脚本适用于 Firefox,但不适用于 Chrome - MutationObserver script for Tampermonkey works in Firefox, but not in Chrome window.onload在Firefox + Greasemonkey脚本中有效但在Chrome用户脚本中无效吗? - window.onload works in Firefox+Greasemonkey script but not in a Chrome userscript? 简单的jQuery脚本在Chrome中运行良好,在Firefox中失败 - Simple jQuery script works fine in Chrome and it fails in Firefox jQuery脚本可在Firefox上运行,但在Chrome中抛出“未定义不是函数” - JQuery script works on Firefox but throws “undefined is not a function” in Chrome javascript脚本可在Firefox,Chrome,Safari,Internet Explorer &lt;9,而不是IE 9中运行 - javascript script works in Firefox, Chrome, Safari, Internet Explorer < 9, but not in IE 9
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM