jquery scroll to div animation

Sometime we see simple one page site with multiple menu point to different div compartment and scroll when clicked.

Here's how someone ask in stackoverflow and here's the test

here is the javascript source

   // prevent default action

   // get id of target div (placed in href attribute of anchor element)
   // and pass it to the scrollToElement function
    // also, use 2000 as an argument for the scroll speed (2 seconds, 2000 milliseconds)
   scrollToElement( $(this).attr('href'), 2000 );

var scrollToElement = function(el, ms){
    var speed = (ms) ? ms : 600;
        scrollTop: $(el).offset().top
    }, speed);