Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Mobile: .animate({scrollTop}) doesn't work after fixing page transitions

jsFiddle links are at the bottom.

I have a Phonegap app that I have created with jQuery Mobile. The page transitions were really choppy and inconsistent in the native iOS app until I found this solution. It made my scrolling not so great, so I made a few changes per this follow-up article.

After the first solution and still after I implemented the second solution, the following code stopped working for me in my app:

$('html, body').animate({scrollTop: $('#specificID').offset().top}, 2500);

The above code scrolls the user down the page over 2.5 seconds to the DIV with the ID of specificID.

I have tried multiple things, but nothing seems to work:

$('#container').animate({scrollTop: $('#specificID').offset().top}, 2500);
$('html, body, #container').animate({scrollTop: $('#specificID').offset().top}, 2500);
$('.scrollable').animate({scrollTop: $('#specificID').offset().top}, 2500);
$(".scrollable").animate({ scrollTop: $("#specificID").scrollTop() }, 2500);

So, here is how I adjusted my jquery mobile code to fix the page transitions:

1. I wrapped [data-role="page"] DIV with container DIV

<body>
  <div id="container">
    <div data-role="page">

2. I added the following Javascript

$(document).one('mobileinit', function () {  
  // Setting #container div as a jqm pageContainer
  $.mobile.pageContainer = $('#container');

  // Setting default page transition to slide
  $.mobile.defaultPageTransition = 'slide';
});

3. I added the following CSS

body {
    margin: 0;
}
div#container {
    position: absolute;
    width: 100%;
    top: 0;
    bottom: 0;
}
div[data-role="header"] {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
}
div[data-role="content"] {
    position: absolute;
    top: 41px;
    bottom: 0;
    left: 0;
    right: 0;
}
.scrollable {
    overflow-y: scroll;
    -webkit-overflow-scrolling: touch;
}
/* iOS specific fix, don't use it on Android devices */
 .scrollable > * {
    -webkit-transform: translateZ(0px);
}

I setup three jsFiddles to show this:

  1. Plain jQuery - scrollTop working: http://jsfiddle.net/pxJcD/1/

  2. Transition Fix - scrollTop NOT working: http://jsfiddle.net/ytqke/3/

  3. Transition Fix w/ Native Scrolling - scrollTop NOT working: http://jsfiddle.net/nrxMj/2/

The last jsFiddle is the solution that I am using and the one that I need to work. I provided the second one to show that the scrollTop functionality stopped before any of the native scrolling changes I made. Any thoughts on what I can do to be able to scroll down the page using javascript?

I’m using jQuery 1.8.2, jQuery Mobile 1.2.0, and Phonegap 2.2.0 (via Build).

Thank you for any help you can offer.

like image 573
Mark Rummel Avatar asked Dec 18 '25 18:12

Mark Rummel


1 Answers

In your CSS, you have set your container's position property to Absolute. Remove your div#container It should work.

http://jsfiddle.net/nrxMj/16/

like image 185
DPA Avatar answered Dec 20 '25 13:12

DPA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!