简体   繁体   中英

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. The script is suppose to scroll down to an id from an anchor but does nothing in 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 .

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:

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

Then modify the code to accommodate it and remove the unnecessary eval statement:

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

http://jsfiddle.net/4FQn7/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM