Ludwig Kristoffersson Ludwig Kristoffersson - 1 year ago 69
Javascript Question

JavaScript scroll to div with animation

I have a PhoneGap application that when it opens an HTML page, I want it to scroll to a specific

element. So far I've been able to make it do that with this script using jQuery:

$(document).delegate('.ui-page', 'pageshow', function () {
var offset = $(this).find('#timeindicatordiv').offset().top;
setTimeout(function () {
}, 0);

This only gives me a jump directly to the
which looks a bit choppy.

Is there any way to give this a smooth animation?


You can do the following:

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

// specify id of element and optional scroll speed as arguments
scrollToElement('#timeindicatordiv', 600);