简体   繁体   中英

iPhone app - JQTouch & iScroll won't work - any ideas?

can anyone help me out, been banging my head against a wall for 2 days! Trying to get iScroll to work with my phonegap/jqtouch app. Have tried all the suggestions I can find on the Internet, but all I get is either no scrolling or rubber banding so I can't view the bottom of the page. My code in a nutshell is:

HTML header:

<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" src="jqtouch/jquery.js"></script>
<script type="text/javascript" src="jqtouch/jqtouch.js"></script>
<script type="text/javascript" src="iscroll-lite.js"></script>
<script type="text/javascript" src="myAppCode.js"></script>
<script type="text/javascript">

    var myScroll;

    function onBodyLoad()
    {
        document.addEventListener("deviceready",onDeviceReady,false);
    }

    function onDeviceReady()
    {
        document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
        document.addEventListener('DOMContentLoaded', loaded, false);
    }

    function loaded()
    {
        myScroll = new iScroll('wrapper');
        myScroll.refresh();
    }

</script>

HTML (I've added the 'wrapper' and 'scroller' divs):

<!-- Options Page header bar-->
 <div id="options_page">
  <div class="toolbar">
   <h1>Past Paper</h1>
   <a class="button flip" href="#reset_page">Reset Stats</a>
  </div>

<!-- why won't you scroll??? -->
 <div id="wrapper">
  <div id="scroller">

   <!-- Stats section div - dynamically generated stats by refreshStats() --> 
   <div id="Stats"></div>

    <h2>General</h2>
    <ul class="rounded">
    <li>Only new questions?<span class="toggle"><input type="checkbox" id="notSeen" /></span></li></ul>

    <h2>Categories</h2>
    <ul class="rounded">
     <li>Cardiology<span class="toggle"><input type="checkbox" id="cardiology" /></span></li>
     <li>Respiratory<span class="toggle"><input type="checkbox" id="respiratory" /></span></li>
     <li>Gastrointestinal<span class="toggle"><input type="checkbox" id="gastrointestinal" /></span></li>
     <li>Neurology<span class="toggle"><input type="checkbox" id="neurology" /></span></li>
    </ul>

    <div id="optionStartQuiz">
     <ul><li class="whiteButton">Start!</li></ul>
    </div>
   </div>
  </div>
</div>

myAppCode.js contains the refreshStats() function to display info in the Stats div above:

$(document).ready(function()
{       
    // load variables from local storage              
    loadLocalStorage();

    // load the stats div
    refreshStats();

    // refresh scores everytime the main page is loaded
    $('#main_page1').bind('pageAnimationStart', function(){
        refreshStats();
        }); 
});

function refreshStats()
{
        // an HTML ul
    var tempStats = '<h2>Current Stats</h2><ul class="rounded"><li>Questions completed<span style="float: right">' + OVERALL_questionsFirstTime + '/' + OVERALL_numberOfQuestions + '</span></li><li>Percentage correct<span style="float: right">' + percentageScore + '%</span></li></ul>';

        // display me
    $('#Stats').html(tempStats);
}

So basically I want to be able to scroll the stuff within the wrapper and scroller divs but can't get my head around it! Thanks, Nick

I was having the same problem. The div that was supposed to scroll would exhibit rubberbanding until I changed the orientation on the iphone. I tried the timeout methods from the iscroll doc page but nothing worked.

Using v4.0 beta4 I added

checkDOMChange: true

when creating my scroll wrapper. This works like a charm. Not really sure what the problem was but I am using this in a jQTouch page.

One thing to note, the checkDOMChange: is listed as being experimental...

hth

Ok finally got this working. To get jQTOuch and iScroll to play nice with each other, the scrolling areas on the page need to be reset each time JQTouch makes them disappear. In other words, once you hide the div, iScroll doesn't know what to scroll the next time it's made visible. So as a result, you get the infamous rubberband effect. To solve this, just add an event listener that resets the scrolling area right after the div is called. Make sure you give it 100 to 300ms delay. This code below assumes your variable is called myScroll :

$(".about").tap(function(){
    setTimeout(function(){myScroll.refresh()},300);
});

And on a side note, here's how to establish multiple scrollers using iScroll:

var scroll1, scroll2;
function loaded() {
    scroll1 = new iScroll('wrapper1');
    scroll2 = new iScroll('wrapper2');
}

Hope this helps someone.

I was facing the same issue. My element used just to bounce and settle back. I edited the scroll.js to make checkDOMChanges:true and it started working fine.

This is my solution.

$("a").tap(function(){ setTimeout(function(){myScroll.refresh()},300); });

The codes above assume that your iScroll variable is myScroll, and the code is force your iScroll to refresh as you tap a link.

I'm not sure about iScroll4, but the original iScroll depended on CSS classes to scroll correctly - specifically, the wrapper needed to be positioned, and have overflow set - in the demo for iScroll4 I see they are still setting similar properties on the wrapper and the scroller:

http://www.cubiq.org/dropbox/iscroll4/examples/simple/

I'm not sure if you've already done this (and just didn't include the CSS) - but your code looks fine.

I've had inordinate troubles with iScroll (pre-iScroll4, haven't tested 4 yet) getting it to work with DIVs instead of UL lists. BUT... today I found a post in google groups (see reply by manmade at 9:59) where it is suggested to add a line:

that.refresh();

into the iScroll code around line 81. In the latest iScroll (v3.7.1), this line is added at line 85, within the case for "START_EVENT" just after the line:

that.touchStart(e);

I haven't analyzed exactly why it works, but it works for getting my div to scroll perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM