Skip to content Skip to sidebar Skip to footer

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 usin

Solution 1:

You can do the following:

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

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

jsfiddle/example: http://jsfiddle.net/dtR34/4/

Solution 2:

Do it like this:

$("html,body").animate({scrollTop: offset}, 600);

Solution 3:

$('.click').click(function(l){
   // prevent default action

   l.preventDefault();

   scrollToElement( $(this).attr('href'), 2000 );
});

var scrollToElement = function(el, ms){
    var speed = (ms) ? ms : 2000;
    $('html,body').animate({
        scrollTop: $(el).offset().top
    }, speed);
}
div {
    margin-top:1000px;
}
div:last-child {
    padding-bottom:1000px;
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ahref="#link1"class="click">link 1</a><br /><ahref="#link2"class="click">link 2</a><br /><ahref="#link3"class="click">link 3</a><br /><divid="link1">Link 1</div><divid="link2">Link 2</div><divid="link3">Link 3</div>

Post a Comment for "Javascript Scroll To Div With Animation"