Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
195
rated 0 times [  196] [ 1]  / answers: 1 / hits: 30618  / 11 Years ago, mon, october 21, 2013, 12:00:00

I have a PhoneGap application that when it opens an HTML page, I want it to scroll to a specific <div> element. So far I've been able to make it do that with this script using jQuery:



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


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



Is there any way to give this a smooth animation?


More From » jquery

 Answers
21

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 arguments
scrollToElement('#timeindicatordiv', 600);


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


[#74833] Sunday, October 20, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arthur

Total Points: 729
Total Questions: 107
Total Answers: 109

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;