Search code examples
javascriptjqueryscrolloffsetscrolltop

A jQuery scrollTop(); with Transition


I can't figure out of to make scroll, (when a div is clicked), and make it smooth. (like not going straight to the scroll position)

Here's my code:

$('.about-center').click(function() {
    var div = document.getElementById('ABOUT');
    var pos = div.offsetTop;
    
    $(window).scrollTop(pos);
});

Solution

  • try this one:

    $('.about-center').click(function() {
        var div = $('#ABOUT');
        var pos = div.offset().top;
    
        $('html, body').animate({scrollTop:pos},2000); // will take two seconds to scroll to the element
    });