1. Code
  2. iOS SDK

Building an iScroll "Kitchen Sink"


This tutorial will teach you how to build a "Kitchen Sink" application for iScroll 4. Building this application will teach you to implement Pinch and Zoom functionality, Custom Scrollbars, Pull to Refresh actions, and Snap/Snap to Element functionality. Along the way, we'll also examine the configuration options for iScroll 4.

This tutorial will build heavily on the Introduction to iScroll 4 post that I published previously. Reading the introductory tutorial isn't strictly necessary before completing this one, but it will help if you are completely unfamiliar with iScroll.

Picking Up Where We Left Off

For this iScroll 4 tutorial, I've decided to restructure the application into a more practical architecture. In my root folder I have an assets directory which contains folders for CSS, Images, Includes, and Javascript. While these folders might not be necessary for the demo I'm building today, you might find the structure handy when building your own applications.

Go ahead and drop the CSS file from the previous tutorial into the assets directory.

Next, I've taken out the initialization of the iScroll function from the main HTML and put it into a main.js file. Make sure that the file is imported into index.html.

Next, rename the index.html file to index.php -just trust me on this for now and I'll explain why we're using PHP later.

Your directory layout should now mirror the following;

  • index.php
  • assets
    • css
      • styles.css
    • img
    • inc
    • js
      • iscroll.js
      • main.js

In addition to making these structural revisions, I've gone ahead and also split up the code a bit, seperating the header and the footer into the includes folder (inc), that way we don't have to copy and paste for every page we make. I'm using both PHP and SHTML for these snippits.

After being split up, your index.php (not index.html anymore) should look something like this:

iScroll Configuration Parameters

When I first discussed iScroll, I was looking at the basic scroll functionality that it offers. What wasn't discussed were the multiple parameters that can be passed into iScroll that give you some other custom options.

When we create a new iScroll object, we have the option to pass in a second parameter as a JavaScript configuration object. The configuration options available include:

  • hScroll
  • vScroll
  • hScrollbar
  • vScrollbar
  • fixedScrollbar
  • fadeScrollbar
  • hideScrollbar
  • bounce
  • momentum
  • lockDirection

We pass these through to our new iScroll configuration object as the second paremeter. We can pass in just one, or multiple.

Remember: The options must always be passed in as a JavaScript object.

Now, let's have a look at what the configuration options do and what their default settings are.

hScroll and vScroll control the horizontal and vertical scrolling (if you hadn't guessed) and by setting either one to false, you can disable them altogether. By default, these are both set to true, so if your content exceeds the wrapper, the user will have the ability to scroll to it.

hScrollbar and vScrollbar these are the options to show and hide the black scrollbar that fades-in and fades-out when scrolling.

iScroll 4 Figure

The fixedScrollbar option is the control for how the scrollbar acts when it reaches the end of your scrollable container. By default it is set to false so that when it reaches the end, it shrinks down. When set to true, it remains a fixed height.

iScroll 4 Figure

fadeScrollbar is set to true by default, but if you would prefer to have the scrollbar just dissapear rather than fade, set this to false.

hideScrollbar is set to true by default, but if you want the scrollbar to remain on the page, set this to false. The scrollbar will now not fade out when the user stops scrolling.

bounce is to set the 'elastic banding' when the user scrolls beyond the content. By default this is set to true. Personally I have found that when using iScroll within a big application, setting this to false has saved a lot of loading time.

momentum controls the inertia of the scrolling. By default it is set to true, but if you want a more controlled scrolling area, set this to false. This is another one of those options that you might want to turn off in big applications as it seems to eat up resources.

lockDirection locks down the scrolling to one axis only. So you can either scroll up and down or left and right at any one time. This is set to true by default, but if you want to allow the user to scroll in either direction at anytime, set it to false.

Those are all the current parameters that can be used with iScroll, so let's go ahead and put the references to them in our kitchen sink app

Adding Pages to the App

Before we can go ahead and start adding pages to our reference app, we need to build on top of what we already had to allow paging. In order to do this quickly, and to explain relatively quickly, I am going to go ahead and include jQuery into the project. I'm not going to use jQuery Mobile for this app, simply because it's a little bit overkill. All we are going to need is the core library.

I've gone ahead and included it in the footer of our project and have used the minified version hosted by Google.

To get our paging to work I have also made some modifications to the index.php. I have changed the wrapper ID to id="home-wrapper" and added a class of page and current to the home wrapper. I have then added another DIV underneath with a new ID and just the page class. We also need to change scroll-content to a class rather than an ID as there could now be multiple on the page.

Your index.php should look something like the below now:

There are also adjustments needed for the CSS. The wrapper ID rule should now be assigned to the page class, a new rule for the current class sould be added, and the scroll-content should be a class and not an ID. Your CSS should resemble the below now.

Note that the page is hidden by default and any page with the class of current is shown, this is the beginning of our paging technique.

In our main.js file I have also written a small amount of functionality that basically takes the target of our href and adds the current class to the element. It also removes the current class from the clicked anchor.

This is pretty basic paging for the app. Animating paging, general navigation through a web app, and building up a history object (without using a prebuilt mobile framework) is beyond the scope of this tutorial. If you would like to see a tutorial covering such things, please leave a comment below.

Just for a bit of consistency, I am also going to initialize iScroll inside the jQuery document.ready() function.

Now that we have some navigation through our app, let's go ahead and add some details to our Parameters section. Im going to make a page for each section, that way you can include any notes you may want to keep on that parameter.

I've just added some backlinks to be able to go backwards through the app and wrapped the page content in a DIV with the page-content class. Add some CSS to accomodate the new elements.

Now that we have taken care of paging and adding our first pages to our iScroll Sink, we can continue looking at iScroll.

Adding Pinch and Zoom

The Pinch and Zoom allows you to have scalable content within our app, but while keeping the fixed headers and footers. It is passed thorugh like any of the other parameters (as an object) and you will need to create a new instance of iScroll for the zoomable area.

Step 1: Destroy and Rebuild

To keep from constantly creating new instances of iScroll for everything, we can .destroy() the current scroll and make a new one each time we page. If you are going to be using various scrolling implementations in your application, you might find .destroy() helpful to free up memory.

To be able to customize my options I am going to first get the current page's ID.

Then I am going to destroy() the current iScroll and set the newScroll to null.

Step 2: Initialize Zoom

Using an if statement, I will next create a new iScroll.

You can control the maximum zoom by passing through an additional option of zoomMax. By Default this is set to 4. You also have the ability to double-tap in order to zoom in and out.

If you are going to be using images in your zoomable areas, you might want to go ahead and add webkit-transform:translate3d(0,0,0) in order to take advantage of hardware compositing.

Go ahead and create a new page for zoom.

Adding Custom Scrollbars

When working with iScroll, you have the option of doing a bit of customization on the scrollbar itself if you pass in the scrollbarClass option.

Step 1: Make the Page

Let's make a Custom Scroll page and see how it works.

Step 2: Initialize iScroll

Then we need to create a new iScroll when we navigate to this page with our options.

Step 3: Scrollbar CSS

In order to customize our scrollbar we need to add some CSS. The myScrollbarV and myScrollbarH classes are added to the vertical and horizontal respectivly. We are going to style myScrollbarV.

You should now have a custom scrollbar when navigating to the Custom Scrollbar page.

iScroll 4 Figure

Have a play around with the styles and see what you can come up with.

Adding Pull to Refresh

Pull to refresh is probably one of the more popular features of iScroll. Any application that has some sort of live feed or stream seems to have an implementation that enables the users to 'Pull' to refresh that content.

Step 1: Refreshing HTML

To get this working in iScroll takes a little more effort than the other options, so let's dive right in and create a page for it.

Note that we have added an extra DIV for our pull down label. Let's take a look at the JS.

Step 2: Refreshing JavaScript

It might be a bit daunting to look at, but it is pretty self explanatory. I'll break it up a bit. I also had some trouble getting this to work with jQuery, so I have reverted to pure JavaScript.

The pullDownAction() is fired when we stop scrolling and our pullDown DIV has the class flip. This could be an Ajax request to get some data from the server, but for the demo we are just going to generate some li elements and generate the text inside of them.

The .refresh() method is a public method available to the iScroll object, like .destroy(). This refreshes the object to accompany any new content that is added to the scrolling area. So, when we are adding rows to our list, we refresh iScroll and it takes them into account so our scrolling area doesn't cut off.

Next, we create a new iScroll object and pass in our options. We need to set the transition, topOffset, onRefresh, onScrollMove and onScrollEnd

If useTransition is set to true, then the scrollbar does not shrink when the content is pulled down to generate new rows. Set it to false to get the shrinking scrollbar.

The topOffset option is the amount of offest needed at the top of the document to hide the refresh DIV. We calculated this above with JavaScript.

The other three options are functions we are defining within the iScroll object. You are most likely to change the labels and the wording within them.

In the onScrollMove function, there is a reference to this.y. This is the distance that the user will have to pull down before the "Release to refresh..." replaces the "Pull to refresh" message.

There are a couple of things I didn't add to my demo that are available in the official iScroll demo. The loading timer and the images. These are pretty easy for you to add if needed, but be carefull not to add the setTimeout into your production code as the Ajax call will take time to fill in.

You can also have pull-up to refresh, which is the same as pull down, except you reverse some of the properties to offset at the bottom.

You should be able to pull down your scrolling area and get some generated rows.

Adding Snap and Snap to Element

The last feature we are going to look at within iScroll is the Snap and Snap to element capabilities. This option allows us to have our scrollable area 'snap' to either the elements within the scrolling container using the default snap or other defined areas.

Step 1: Setting Up the Carousel

For the demo we are just going to build a mini-carousel that will use the swiping to snap to the next element. Here is the HTML code.

Step 2: Snap Options for iScroll

We need to make a new iScroll object and pass in the snap options.

Setting snap to true allows the content to snap into place. This is false by default. The momentum option is the momentum carried through when swiping the content. If you swipe too hard the element are skipped as the momentum carries through. Set this to false if you want to snap to the very next element, regardless of the user swipe speed. The hScrollbar and vScrollbar are two we've seen before. Set these to true if you'd like to see the scrollbar when the content is moving.

Step 3: Carousel CSS

Let's add some CSS to our carousel to finish it off. This is standard CSS for a carousel.

You should now have a scrollable carousel.

Step 4: Further Reading

In the demo on the official site, the carousel has additional links that allow you to scroll by clicking. Check out the live demo and you will see the code being used. If you like, see if you can get something similar working in the sink.

Kitchen Sink Code Review

Let's take a look at what the finished code should look like. First, the index.php file:

Then the styles.css file:

Finally, the main.js file:

I have published this copy of iScroll Sink to GitHub. You are more than welcome to fork this code, and if you modify it feel free to submit a pull request. I will keep this GitHub repo updated if and when more things get added to iScroll.

Next Steps

What you have here is the foundation of iScroll, and now you have it in an app you can keep on your local machine or personal server and refer to any time you might need to use something. You also have a place where you can test out new things with iScroll and keep updating the sink to be your personal reference. While we have not covered every inch of iScroll, we have covered a good majority of it, and I encourge you to check out the main website and the google groups page for more information.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.