Scroll with Animate does not work

Asked

Viewed 456 times

4

The code below in jQuery runs perfectly well in Google Chrome and Opera. The problem is that it doesn’t work in IE and Firefox, it used to work.

// Jquery document...
$(document).ready(function() {
    $('a[href^="#"]').click(function (event) {
        var id = $(this).attr("href");
        var target = $(id).offset().top - 200;

        $('body').animate({scrollTop:target}, 300);
        event.preventDefault (); 
    });
});

1 answer

2


In some browsers the scrollbar is in body and in others it is in html, so you should change the selector to:

$('html,body').animate({scrollTop:target}, 300);

Should stay like this:

$(document).ready(function() {
    $('a[href^="#"]').click(function (event) {
        var id = $(this).attr("href");
        var target = $(id).offset().top - 200;

        $('html,body').animate({scrollTop:target}, 300);
        event.preventDefault(); 
    });
});
  • Thanks William, it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.