Here are some hacks that I’ve discovered to get the most out of iScroll. The basic customization parameters are documented at www.cubiq.org’s website but you have to look at the cleanly laid out source to find the gems.

The current list of tips:

  • Improved Lock Direction
  • Faster Snap
  • Snap to a new element and only that element
  • Controlling Scroll Bar Position
  • Enabling SELECT, INPUT and TEXTAREA focus
  • Enabling hidden iScroll divs (where the wrapper div has display:none)
  • The bounce of death – non-scrolling content
  • Disappearing Lists
  • iScroll scrolls up when a scrolled element is clicked

Improved Lock Direction

There will come a time when you have two iScrolls on a page – one vertical and one horizontal. You will find that even if you enable “lockDirection” that iScroll will still be touchy. Try to scroll up and the slightest movement left or right will be caught by the horizontal iScroll. Try to swipe left or right and the slightest up or down movement will be caught by the vertical iScroll. Lock direction provides some improvement so be sure to enable it:

...
var myScroll = myScroll = new iScroll('yourWrapper', {lockDirection:true });
..

But to get even better “lockDirection performance” try the following hacks. Locate lines 470 and 473 within iscroll.js. We’ll want to change a number from 5 to 0.

Line 470 before:

...
if (that.absDistX > that.absDistY - 5) {
...

And line 470 after:

...
if (that.absDistX > that.absDistY - 0) { // yes, you can just remove the math too
...

Do the same with line 473. You should now notice that lockDirection with overlapping iScrolls works better than before.

Faster Snap

To enjoy faster element snapping open iscroll.js and scroll to line 437 where you will find this:

...
that.lastScale = scale / this.scale;
..

Subtract 10 from the lastScale value as seen below:

...
that.lastScale = scale / this.scale - 10;
..

Next, go to line 780 and find this line:

...
var deceleration = 0.0006,
...

Change the deceleration value to 0.002 per the next example:

...
var deceleration = 0.002, // 0.0006
...

Done, your snapping should be much “snappier”.

Snap to a new element and only that element

This tip is pretty cool – and simple. You can prevent “over swiping” in a snap-enabled iScroll by disabling momentum in one of two ways. The first approach is the easiest and most simple – and that is to disable momentum when you initialize iScroll, for example:

...
var myScroll = myScroll = new iScroll('introSliderWrapper', {snapThreshold:.5, momentum:false });
..

Note the “momentum:false” in the above configuration object. Momentum will be turned off.

Ok, so this next way of doing it is a **complete** hack and is really only of geeky interest – but if you look at the iScroll source you will see that there is some old code present that is very similar to the code that precedes it.

The new and old “duplicate” blocks are at lines 589 (new, momentum) and 608 (old, no momentum). Each block of code is surrounded by “if” statements that contain the exact same conditional. The “return” statement at the end of the first “if” block prevents the second “if” from running. It is necessary to comment the entire “if” block that starts at line 590 all the way to and including the return statement at line 605. Be aware that if you do this you are in effect creating a “global” change that will impact all iScrolls that you may employ. As a result, its best to stick with the perfectly functional momentum:false flag.

Anyway, to continue, locate the following code at line 589:

...
    // Do we need to snap?
    if (that.options.snap) {
      distX = newPosX - that.absStartX;
      distY = newPosY - that.absStartY;
      if (m.abs(distX) < that.options.snapThreshold && m.abs(distY) < that.options.snapThreshold) { that.scrollTo(that.absStartX, that.absStartY, 200); }
      else {
            snap = that._snap(newPosX, newPosY);
            newPosX = snap.x;
            newPosY = snap.y;
            newDuration = m.max(snap.time, newDuration);
      }
    }

    that.scrollTo(m.round(newPosX), m.round(newPosY), newDuration);

    if (that.options.onTouchEnd) that.options.onTouchEnd.call(that, e);
    return;
  }
...

And with it commented out:

...
    // Do we need to snap?
    /*if (that.options.snap) {
      distX = newPosX - that.absStartX;
      distY = newPosY - that.absStartY;
      if (m.abs(distX) < that.options.snapThreshold && m.abs(distY) < that.options.snapThreshold) { that.scrollTo(that.absStartX, that.absStartY, 200); }
      else {
            snap = that._snap(newPosX, newPosY);
            newPosX = snap.x;
            newPosY = snap.y;
            newDuration = m.max(snap.time, newDuration);
      }
    }

    that.scrollTo(m.round(newPosX), m.round(newPosY), newDuration);

    if (that.options.onTouchEnd) that.options.onTouchEnd.call(that, e);
    return; */
  }
...

Done.... when swiping you will no longer swipe past the the adjacent snap-to element. As mentioned, this is done easier by adding the momentum attribute to the iScroll configuration object.

Controlling Scroll Bar Position

Enabling scrollbars in iScroll is easy - making sure they are positioned where you want - easy as well as long as you position your div's appropriately. Even if you run into problems, you can hack the scroll bar into position.

To make scrollbars appear just pass a properly formatted configuration object to the iScroll, like so:

...
new iScroll('scrollWrapper',{
   vScrollbar: true, // the vertical scroll bar has been enabled
   momentum:true
});
...

This will make the vertical scrollbar appear - but in some cases the scrollbar may not be aligned with your scrolling content but aligned along the right-edge of the window. Sure, it may work but if you have a multi-column layout then chances are that you may not want the scroll bar "removed" from the scrolling area. The fix here is simple and achieved via CSS - make sure that your "scrollWrapper" div has its position style set to one of the following:

  1. absolute
  2. relative
  3. fixed
  4. inherit (as long as the inherited value is one of the preceeding three values)

Once the above is done then iScroll will properly place the scrollbar along side your content.

What if you can't change your css or don't want to? Then you can attack it with a little jQuery.

iScroll will place the scrollbar divs within the scroll wrapper but without an id. Since the wrapper does have an id we can use jQuery to get the wrapper's children of which the scrollbar is a member. The last child will always be the scroll bar. For example:

...
window.addEventListener('load',function(){
   new iScroll('scrollWrapper',{ // the scrollbar will be the last child within the wrapper
      vScrollbar: true,
      momentum:true
   });

   // lets get the wrapper's children
   var children = $('#scrollWrapper').children();
   // the children array has a length of two, the last one is always the scrollbar
   // position the scroll bar as desired
   children[1].style.height = 350 + 'px';
   children[1].style.top = 0 + 'px';
   children[1].style.left = 190 + 'px';
},false);
...

As you can see from the above its a simple matter to attack the iScroll scroll bar. If you like you could obviously change this:

children[1].style.height = 350 + 'px';

To this:

children[children.length-1].style.height = 350 + 'px';

So that you **always** get the last child as who knows how you're structuring your content.

In addition to this you may not always have a scroll bar present depending on the length of your content. Therefore, we have to check for its existence in some way. In a typical setup the wrapper will only have a single child if the scrollbar is not there. The following check is based on this assumption:

...
window.addEventListener('load',function(){
   new iScroll('scrollWrapper',{ // the scrollbar will be the last child within the wrapper
      vScrollbar: true,
      momentum:true
   });

   // lets get the wrapper's children
   var children = $('#scrollWrapper').children();
   if (children.length > 1){ // if there is more than one child, the scrollbar must be present
      children[1].style.height = 350 + 'px';
      children[1].style.top = 0 + 'px';
      children[1].style.left = 190 + 'px';
   }
},false);
...

Enabling SELECT, INPUT and TEXTAREA focus

iScroll will disable the ability to give focus to certain form elements preferring instead to treat all interactions with the screen as something that must go through iScroll itself. You can get around this by adding the following function to the onBeforeScrollStart event - shown here as a configuration object while instantiating iScroll. You could also just add it to the iScroll source - view source, scroll down until you see the onBeforeStartEvent and add the function there.

...
var scroll = new iScroll('content',
   {scrollbarClass: 'myScrollbar',
    handleClick: false,
    useTransform:true,
    onBeforeScrollStart: function (e) {
        var target = e.target;
        while (target.nodeType != 1) target = target.parentNode;
        if (target.tagName != 'SELECT' && target.tagName != 'INPUT' && target.tagName != 'TEXTAREA')
            e.preventDefault();
	}
    });
...

Enabling hidden iScroll divs

This isn't a hack per se as iScroll has a method to deal with the issue. In cases where you instantiate an iscroll on a hidden wrapper div and then reveal the wrapper by changing its display style to something other than "none" you will find that the iScroll-ed div will just bounce in its wrapper, refusing to scroll.

The fix is to do the following **after** you have revealed the wrapper - the example uses the refresh and scrollTo methods to enable scrolling:

...
// instantiate a new iScroll
var myScroll = new iScroll('myDiv');

function reveal_iScroll_div(){
   document.getElementById('myDiv').style.display = 'block';
   myScroll.refresh();
   myScroll.scrollTo(0,0,0);
}
...

...

The bounce of death - non-scrolling content

Sometimes your content will just bounce in its container and not scroll at all. There are a couple things that cause this - 1 of them is the section preceding this one, the other is likely to be a simple line of CSS. Make sure that the div that is wrapping your content has a css display value of "block".

Disappearing Lists

Sometimes you'll have an iScroll whose contents are updated according to whatever the user has selected. If you have a long list and quickly scroll through them - and then try to update the contents of the scroll wrapper while your list items are flying by you may find that the list may update with new content but not be visible. That is, if you inspect the DOM you plainly see that your elements are there correctly positioned but they do not display (observed in Android 4.1.2).

It appears that we may need to stop the scrolling before updating the list. Something like this:

...
if (_myScroll){
    _myScroll.scrollTo(0,0,0);
}
// now update the list
...
// _myScroll.refresh() might not work here, the "scrollTo" before the update seems to help
...

iScroll scrolls up when a scrolled element is clicked

(7/15/2016) I just had this particular issue that took me quite a while to figure out - I had inited an iScroll with content that appeared to function correctly, but no matter what buttons I clicked in my scrolled content the result was a) what I programmed the button to do, and b) the iScroll would scroll back to the top (interestingly the scroll bar would NOT scroll to the top).

Turned out that I had instantiated the iScroll twice in quick succession on the same variable - maybe creating some sort of race condition? You would think that one would overwrite the other... but fixing the code so that I created an iScroll instance only once resolved the issue.