Scroll with animate does not work

3

The code below in jQuery runs perfectly fine in Google Chrome and Opera. The problem is that it does not work in IE and Firefox, before it worked.

// 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 (); 
    });
});
    
asked by anonymous 21.04.2015 / 02:35

1 answer

1

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

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

It should look 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(); 
    });
});
    
21.04.2015 / 03:01