YUI 3.x Home -

YUI Library Examples: ScrollView: Horizontal ScrollView

ScrollView: Horizontal ScrollView

This example shows how to create a ScrollView widget which scrolls horizontally.

A Horizontal ScrollView

In this example, we'll create a ScrollView instance, which scrolls horizontally, as opposed to the vertically scrolling ScrollView we created in the ScrollView With Scroll Indicator example.

The code for the example is pretty much the same as the code for the ScrollView With Scroll Indicator example. The only difference is that instead of having a ScrollView with a fixed height (and content which overflows that height), we set up a ScrollView with a fixed width (and content which overflows that width).

Modules Used

Since we want to use the base ScrollView, along with the ScrollViewScrollbars plugin, we use the scrollview module as we did for the vertical ScrollView example:

  1. YUI().use('scrollview', function(Y) {
  2. ...
  3. });
  4.  
YUI().use('scrollview', function(Y) {
    ...
});
 

Instantiating The ScrollView Widget

As with the vertical 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="scrollable" style="" class="yui3-scrollview-loading horiz">
  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="scrollable" style="" class="yui3-scrollview-loading horiz">
    <ul>
        <li><img src="..."></li>
        <li><img src="..."></li>
        <li><img src="..."></li>
        <li><img src="..."></li>
    </ul>
</div>

But this time, when we instantiate the ScrollView instance, we provide a fixed width, as opposed to a fixed height, for the widget.

Also, since we have images which act as drag/flick targets, we need to stop the default image drag/drop behavior for gecko, by preventing default on the underlying mousedown event used by flick:

  1. // Constraining the width, instead of the height for horizontal scrolling
  2. var scrollView = new Y.ScrollView({
  3. srcNode: '#scrollable',
  4. width: 320,
  5.  
  6. flick : {
  7. preventDefault:function(e) {
  8. // Prevent image drag in gecko (assuming non-touch for this example).
  9. return (Y.UA.gecko);
  10. },
  11. minDistance:10,
  12. minVelocity:0.3
  13. }
  14.  
  15. });
  16.  
  17. scrollView.render();
// Constraining the width, instead of the height for horizontal scrolling
var scrollView = new Y.ScrollView({
    srcNode: '#scrollable',
    width: 320,
 
    flick : {
        preventDefault:function(e) {
            // Prevent image drag in gecko (assuming non-touch for this example).
            return (Y.UA.gecko);
        },
        minDistance:10,
        minVelocity:0.3
    }
 
});
 
scrollView.render();

This causes the list content (which has inline-block applied to each LI by the scrollview CSS) to be wider than the ScrollView, forcing horizontal scrolling. The height of the ScrollView in this case is driven by the height of it's content.

Copyright © 2010 Yahoo! Inc. All rights reserved.

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