YUI 3.x Home -

YUI Library Examples: ScrollView: ScrollView Paginator Plugin

ScrollView: ScrollView Paginator Plugin

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

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:

  1.  
  2. // Pull in the scrollview widget, and the paginator plugin
  3.  
  4. YUI().use('scrollview', 'scrollview-paginator', function(Y) {
  5. ...
  6. });
  7.  
 
// Pull in the scrollview widget, and the paginator plugin
 
YUI().use('scrollview', 'scrollview-paginator', function(Y) {
    ...
});
 

Instantiating The ScrollView Widget

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

  1. <!-- Content with a width which would require scrolling -->
  2. <div id="scrollview-content" style="" class="yui3-scrollview-loading">
  3. <ul>
  4. <li><img src="..."></li>
  5. <li><img src="..."></li>
  6. <li><img src="..."></li>
  7. <li><img src="..."></li>
  8. </ul>
  9. </div>
<!-- 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:

  1. // Constraining the width, instead of the height for horizontal scrolling
  2. var scrollView = new Y.ScrollView({
  3. id: '#scrollview',
  4. srcNode: '#scrollview-content',
  5. width : 320,
  6. flick: {
  7. minDistance:10,
  8. minVelocity:0.3,
  9. axis: "x"
  10. }
  11. });
// 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:

  1. /* To layout horizontal LIs */
  2. #scrollview-content {
  3. white-space:nowrap;
  4. }
  5.  
  6. #scrollview-content li {
  7. display:inline-block;
  8. background-color:#fff;
  9. }
  10.  
  11. /* For IE 6/7 - needs inline block hack (and the background color mentioned above) */
  12. #scrollview-content li {
  13. *display:inline;
  14. *zoom:1;
  15. }
/* 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:

  1. scrollView.plug(Y.Plugin.ScrollViewPaginator, {
  2. selector: 'li'
  3. });
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:

  1. Y.one('#scrollview-next').on('click', Y.bind(scrollView.pages.next, scrollView.pages));
  2. Y.one('#scrollview-prev').on('click', Y.bind(scrollView.pages.prev, scrollView.pages));
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:

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

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

Copyright © 2011 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings