dramixx
dramixx

Reputation: 274

jQueryMobile - Scroll to a position onLoad

In jQueryMobile, on the page load, I would like to scroll to a given position. I know how to do it in classic jQuery, but in jQueryMobile there is an auto scroll top on the page load.

I tried to do :

$( document ).ready(function() {
    $.mobile.silentScroll(1000);
});

That doesn't work. My page stay blocked to the top of the page.

While if i click on a link with onclick="$.mobile.silentScroll(1000);" that works perfectly !

I just would like to scroll to a yPosition on the page load :) !

=======EDIT============

After suggestions of White Raven and Omar I've tried to do this :

    $( document ).delegate("#pagePkmn", "pageinit", function() {
        $.mobile.silentScroll(1000);
    });

OR this :

    $(document).one("pagecontainershow", function () {
        $.mobile.silentScroll(1000);
    });

But still no effect ...

Thanks for your attention.

Upvotes: 1

Views: 823

Answers (2)

user1650394
user1650394

Reputation:

Using $(document).ready() is a bad idea:

http://view.jquerymobile.com/1.3.1/dist/demos/faq/dom-ready-not-working.html

It is recommended to use pageinit

=== EDIT ===

You can always use the ghetto way:

setTimeout(function(){$.mobile.silentScroll(1000);}, 1000);  // scroll after 1 second

Upvotes: 1

Omar
Omar

Reputation: 31732

Use pagecontainershow as it triggers after page is shown and JQM performes a default scroll to page's top.

$(document).one("pagecontainershow", function () {
   /* scroll */
});

Upvotes: 1

Related Questions