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?
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?
Share Improve this question edited May 10, 2016 at 17:16 Kirk Beard 9,84312 gold badges45 silver badges48 bronze badges asked Oct 21, 2013 at 15:22 LuddigLuddig 2,8193 gold badges21 silver badges23 bronze badges 1- have you tried this plugin: plugins.jquery.com/Scrollorama? – Tomer Commented Oct 21, 2013 at 15:26
3 Answers
Reset to default 28You 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/
Do it like this:
$("html,body").animate({scrollTop: offset}, 600);
$('.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;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#link1" class="click">link 1</a><br />
<a href="#link2" class="click">link 2</a><br />
<a href="#link3" class="click">link 3</a><br />
<div id="link1">Link 1</div>
<div id="link2">Link 2</div>
<div id="link3">Link 3</div>