#

# Example: ScrollView With Pagination

This example shows how to create a ScrollView widget with pagination support, using the ScrollViewPaginator plugin.

View Example in New Window

## ScrollView with Pagination Support

In this example, we'll create a ScrollView instance which has pagination support enabled. This allows the ScrollView to scroll between discrete page boundaries in the content, as opposed to continuous scrolling. Pagination is only supported for horizontal ScrollViews currently.

### Modules Used

For this example, since we want both pagination and scrollbar indicator support enabled, we use the `scrollview` module as we did for the Horizontal ScrollView example to get the base ScrollView with scrollbar support.

Additionally we pull down the `scrollview-paginator` module, which provides the `ScrollViewPaginator` plugin:

    // Pull in the scrollview widget, and the paginator plugin

    YUI().use('scrollview', 'scrollview-paginator', function(Y) {
        ...
    });

### Instantiating The ScrollView Widget

Note: be sure to add the `yui3-skin-sam` classname to the page's `<body>` element or to a parent element of the widget in order to apply the default CSS skin. See Understanding Skinning.

    <body class="yui3-skin-sam"> <!-- You need this skin class -->

As with the Horizontal ScrollView example, we provide the markup for the ScrollView content on the page, as shown below:

    <!-- Content with a width which would require scrolling -->
    <div id="scrollview-content" style="" class="yui3-scrollview-loading">
        <ul>
            <li><img src="..."></li>
            <li><img src="..."></li>
            <li><img src="..."></li>
            <li><img src="..."></li>
        </ul>
    </div>

And we instantiate the ScrollView instance in the same way, by providing a fixed width for the widget, and constraining flicks to the "x" axis:

    // Constraining the width, instead of the height for horizontal scrolling
    var scrollView = new Y.ScrollView({
        id: '#scrollview',
        srcNode: '#scrollview-content',
        width : 320,
        flick: {
            minDistance:10,
            minVelocity:0.3,
            axis: "x"
        }
    });

As we did in the Horizontal ScrollView example, we add CSS which switches the LIs to layout horizontally:

    /* To layout horizontal LIs */
    #scrollview-content {
        white-space:nowrap;
    }

    #scrollview-content li {
        display:inline-block;
        background-color:#fff;
    }

    /* For IE 6/7 - needs inline block hack (and the background color mentioned above) */
    #scrollview-content li {
        *display:inline;
        *zoom:1;
    }

This gives us a ScrollView instance with scrollbar indicator support. However it doesn't have pagination support available yet.

### Plugging In Pagination Support

To add pagination support to the ScrollView instance, we plug in the `ScrollViewPaginator` plugin, providing the `selector` attribute configuration argument to it. The `selector` tells the paginator which list of elements inside the ScrollView content box define page boundaries at which the ScrollView should stop when scrolling. For this example, each LI defines a ScrollView page:

    scrollView.plug(Y.Plugin.ScrollViewPaginator, {
        selector: 'li'
    });

The ScrollView now has pagination support activated, and will stop at page boundaries when the user flicks the content, or drags the content past the halfway point of the current page.

### Accessing The Paginator Plugin API

Similar to the ScrollBar indicator plugin, the ScrollBarPaginator API is now available on the ScrollView instance, on the namespace defined by the plugin, which is `scrollView.pages`. The `pages` property can be used to manage the page state of the ScrollView, as shown below:

    Y.one('#scrollview-next').on('click', Y.bind(scrollView.pages.next, scrollView.pages));
    Y.one('#scrollview-prev').on('click', Y.bind(scrollView.pages.prev, scrollView.pages));

The above code calls the plugin's `next()` and `prev()` methods when the respective button is clicked. The ScrollView Paginator documentation provides additional examples of the API available through the `pages` property.

### Setting Up A Click Listener On the Content

For this example, we also set up a click listener on the images. For touch devices, the click event does not fire when dragging or flicking, so there's no special handling required when setting up a click listener. However to also support mouse based devices, we need to distinguish between a click and drag or flick. In order to do this we set up a check in our click listener, to make sure we only respond to the click if the ScrollView wasn't dragged, by checking the `lastScrolledAmt` property, which is reset whenever a drag/flick gesture ends:

    Y.one("#scrollview-content").delegate("click", function(e) {
        // For mouse based devices, we need to make sure the click isn't fired
        // and the end of a drag/scroll. We use 2 as an arbitrary threshold.
        if (Math.abs(scrollView.lastScrolledAmt) < 2) {
            alert(e.currentTarget.getAttribute("alt"));
        }
    }, "img");

### Preventing Native Behavior For Content

As with the Horizontal ScrollView example, since we have images which act as drag/flick targets, we need to stop the default image drag/drop behavior in certain browsers (for example gecko and IE), by preventing default on the underlying mousedown event. If we don't prevent default, the image will be natively draggable by default, and interfere with scrolling:

    // Prevent the image from being natively dragged
    content.delegate("mousedown", function(e) {
        e.preventDefault();
    }, "img");

## Complete Example Source

Note: be sure to add the `yui3-skin-sam` classname to the page's `<body>` element or to a parent element of the widget in order to apply the default CSS skin. See Understanding Skinning.

    <body class="yui3-skin-sam"> <!-- You need this skin class -->

    <div id="scrollview-container">
        <div id="scrollview-header">
            <h1>Paged ScrollView</h1>
        </div>

        <div id="scrollview-content" class="yui3-scrollview-loading">
            <ul>
                <li><img src="http://farm5.static.flickr.com/4136/4802088086_c621e0b501.jpg" alt="Above The City II"></li>
                <li><img src="http://farm5.static.flickr.com/4114/4801461321_1373a0ef89.jpg" alt="Walls and Canyon"></li>
                <li><img src="http://farm5.static.flickr.com/4100/4801614015_4303e8eaea.jpg" alt="Stairs Using In Situ Stone"></li>
                <li><img src="http://farm5.static.flickr.com/4076/4801368583_854e8c0ef3.jpg" alt="Tree Silhouette"></li>
            </ul>
        </div>

        <div id="scrollview-pager">
            <button id="scrollview-prev">Prev</button>
            <button id="scrollview-next">Next</button>
        </div>
    </div>

    <div id="additional-content">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras aliquam hendrerit elit id vulputate. Pellentesque pellentesque erat rutrum velit facilisis sodales convallis tellus lacinia. Curabitur gravida mi sit amet nulla suscipit sed congue dolor volutpat. Aenean sem tortor, pretium et euismod in, imperdiet sit amet urna. Ut ante nisi, auctor mattis suscipit a, ullamcorper eget leo. Phasellus sagittis ante at lectus rutrum ut sollicitudin sem malesuada. Duis ultrices sapien et nulla tincidunt malesuada. Mauris ante turpis, dignissim eu tincidunt vitae, placerat quis diam. In augue nisl, cursus at rutrum ut, scelerisque et erat. Suspendisse potenti. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris orci dui, aliquam ut convallis ut, dapibus et erat. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam erat volutpat. Mauris placerat elit id lectus rhoncus in dignissim justo mollis. Donec nec odio sapien. In iaculis euismod felis non laoreet. Mauris ornare varius neque, et congue erat porta a. Aliquam nec auctor lectus. Etiam ut ipsum a nibh iaculis fringilla.</p>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras aliquam hendrerit elit id vulputate. Pellentesque pellentesque erat rutrum velit facilisis sodales convallis tellus lacinia. Curabitur gravida mi sit amet nulla suscipit sed congue dolor volutpat. Aenean sem tortor, pretium et euismod in, imperdiet sit amet urna. Ut ante nisi, auctor mattis suscipit a, ullamcorper eget leo. Phasellus sagittis ante at lectus rutrum ut sollicitudin sem malesuada. Duis ultrices sapien et nulla tincidunt malesuada. Mauris ante turpis, dignissim eu tincidunt vitae, placerat quis diam. In augue nisl, cursus at rutrum ut, scelerisque et erat. Suspendisse potenti. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris orci dui, aliquam ut convallis ut, dapibus et erat. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam erat volutpat. Mauris placerat elit id lectus rhoncus in dignissim justo mollis. Donec nec odio sapien. In iaculis euismod felis non laoreet. Mauris ornare varius neque, et congue erat porta a. Aliquam nec auctor lectus. Etiam ut ipsum a nibh iaculis fringilla.</p>
    </div>

    <script type="text/javascript" charset="utf-8">
    YUI().use('scrollview', 'scrollview-paginator', function(Y) {

        var scrollView = new Y.ScrollView({
            id: "scrollview",
            srcNode : '#scrollview-content',
            width : 320,
            flick: {
                minDistance:10,
                minVelocity:0.3,
                axis: "x"
            }
        });

        scrollView.plug(Y.Plugin.ScrollViewPaginator, {
            selector: 'li'
        });

        scrollView.render();

        var content = scrollView.get("contentBox");

        content.delegate("click", function(e) {
            // For mouse based devices, we need to make sure the click isn't fired
            // at the end of a drag/flick. We use 2 as an arbitrary threshold.
            if (Math.abs(scrollView.lastScrolledAmt) < 2) {
                alert(e.currentTarget.getAttribute("alt"));
            }
        }, "img");

        // Prevent default image drag behavior
        content.delegate("mousedown", function(e) {
            e.preventDefault();
        }, "img");

        Y.one('#scrollview-next').on('click', Y.bind(scrollView.pages.next, scrollView.pages));
        Y.one('#scrollview-prev').on('click', Y.bind(scrollView.pages.prev, scrollView.pages));

    });
    </script>