[英]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);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.