6

Im currently pulling my hair out trying to get iscroll 4 working with jQuery Mobile.

It all works fine it i disable JQM ajax default navigation but I would like to retain this.

My issue is I cant work out how to successfully call/bind iscroll so it works with the pages that need them. I have tried pageinit() and pagecreate() to no avail.

Any pointers much appreciated.

A.

Jasper
  • 75,717
  • 14
  • 151
  • 146
Adi
  • 4,034
  • 13
  • 56
  • 78

1 Answers1

8

I initialize/refresh the iScroll instances on the pageshow and orientationchange events. I set a class on data-role="content" divs that I want to be scrollable (in this instance I used the .content class).

var myScroll = [];
$(document).delegate('[data-role="page"]', 'pageshow', function () {
    if ($.mobile.activePage.find('.content').length > 0) {
        if (this.id in myScroll) {
            myScroll[this.id].refresh();
        } else {
            myScroll[this.id] = new iScroll($.mobile.activePage.find('.content')[0].id, {
                hScroll        : false,
                vScroll        : true,
                hScrollbar     : false,
                vScrollbar     : true,
                fixedScrollbar : true,
                fadeScrollbar  : false,
                hideScrollbar  : false,
                bounce         : true,
                momentum       : true,
                lockDirection  : true
            });
        }
    }
});

$(window).bind('orientationchange', function () {
    if ($.mobile.activePage[0].id in myScroll) {
        myScroll[$.mobile.activePage[0].id].refresh();
    }
});
Jasper
  • 75,717
  • 14
  • 151
  • 146
  • do you load iscroll js before ? – Adi Oct 07 '11 at 17:16
  • No it's the last script I load. If you are having a hard time initializing the iScroll instances, make sure you are following the example on the iScroll site. iScoll only scrolls the immediate child of the element you call it on. – Jasper Oct 07 '11 at 17:19
  • i can get it working, just not with JQM ajax navigation. i.e. if i get to the page iscroll isnt working. if i refresh that page it works fine (presumable becasue it reads all the js again) at present iscroll js is in head of html still and not in any page event in js – Adi Oct 07 '11 at 17:36
  • Hi, here is a very rough example of the issue (there are thousands more pages in the real site) http://bit.ly/ngXkNR – Adi Oct 11 '11 at 11:48
  • You are binding to the `DOMContentLoaded` event which is only fired when the initial page is loaded (and not on pages pulled-in through the ajax navigation) and you want to bind to one of the following: `pageshow`, `pagecreate`, `pageinit`. Check-out my example above that binds to the `pageshow` event and then inside that event handler I check if the iScroll instance exists (I then refresh it if it exists) or if the instance does not exist I create it. Here's a link to explain each event: http://jquerymobile.com/demos/1.0rc1/docs/api/events.html. – Jasper Oct 11 '11 at 16:09