YUI 3.x Home -

YUI Library Examples: ScrollView: Basic ScrollView without a Scroll Indicator

ScrollView: Basic ScrollView without a Scroll Indicator

This example shows how to create a basic ScrollView widget. The base ScrollView widget doesn't have a scrollbar indicator or pagination support.

The Basic ScrollView Widget

In this example, we'll create a basic ScrollView instance, without any additional feature plugins applied. This is the lightest version of the ScrollView widget. In later examples, we'll see how we can pull in different modules and plugins to provide additional features.

Modules Used

Since we only need the basic scrollview for this example, we pull in the scrollview-base module, the lightest version of ScrollView:

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

The scrollview-base module provides a ScrollView without any plugins applied. We'll see in the Scrollview With Scroll Indicators example, that the scrollview module provides a base ScrollView bundled with scroll indicator support.

Instantiating The ScrollView Widget

The ScrollView provides support for scrollable content. In general this content can be anything, but most often it is in the form of a list, to be scrolled through. For this example, we'll provide the content for the scrollview in the form of a list, as shown below:

  1. <div id="scrollable" class="yui3-scrollview-loading">
  2. <ul>
  3. <li><div>AC/DC</div></li>
  4. <li><div>Aerosmith</div></li>
  5. <li><div>Billy Joel</div></li>
  6. <li><div>Bob Dylan</div></li>
  7. ...
  8. </ul>
  9. </div>
<div id="scrollable" class="yui3-scrollview-loading">
    <ul>
        <li><div>AC/DC</div></li>
        <li><div>Aerosmith</div></li>
        <li><div>Billy Joel</div></li>
        <li><div>Bob Dylan</div></li>
        ...
    </ul>
</div>

We add the yui3-scrollview-loading class as described in the Widget Progressive Enhancement section, and provide a custom rule to hide this progressively enhanced content while the scrollview is being rendered:

  1. .yui3-js-enabled .yui3-scrollview-loading {
  2. display:none;
  3. }
.yui3-js-enabled .yui3-scrollview-loading {
    display:none;
}

To instantiate the ScrollView instance, we provide it with the srcNode attribute during construction, so it uses the markup above for it's content, as shown below. We could also add the content dynamically, however providing the markup on the page, allows users without JavaScript enabled to still see the content.

  1. YUI().use('scrollview-base', function(Y) {
  2.  
  3. var scrollView = new Y.ScrollView({
  4. srcNode: '#scrollable',
  5. height: 310
  6. });
  7.  
  8. scrollView.render();
  9. });
YUI().use('scrollview-base', function(Y) {
 
    var scrollView = new Y.ScrollView({
        srcNode: '#scrollable',
        height: 310
    });
 
    scrollView.render();
});

For this example, since we want a vertically scrolling ScrollView widget, we also give it a height during construction. Without the height, the ScrollView widget would be as tall as it's content list, and there would be no need to scroll.

As the last step, to see the functional ScrollView on the page, we call scrollView.render().

Controlling Sensitivity

The scroll dynamics for the ScrollView widget can be controlled by tweaking the following attributes, either during construction or after:

flick
Defines the minimum distance and/or minimum velocity which define a flick. It can be set to 0 to disable flick support completely.
bounce
Defines how quickly the velocity of the scrollview content decreases during a bounce (when the scrollview hits the edge of it's scroll limits). It can be set to 0 to disable bounce completely.
deceleration
Defines how quickly the velocity of the scrollview content decreases in response to a flick.

Additional details about these parameters and a few other static properties which can be used to modify scroll dynamics are discussed in the ScrollView documentation.

Modifying Layout For Small Screen Devices

This example also shows how you can modify the look and feel for your page/application, based on the size of the device you're delivering it to. For this example, when the maximum width of the device is 480px or less, we provide additional CSS rules which hide additional content and make the scrollview a full screen Widget, using media queries:

  1. <link media="handheld, only screen and (max-device-width: 480px)"
  2. href="assets/examples-smallscreen.css"
  3. type="text/css"
  4. rel="stylesheet">
<link media="handheld, only screen and (max-device-width: 480px)" 
      href="assets/examples-smallscreen.css" 
      type="text/css" 
      rel="stylesheet">

The CSS in the above file, which is only served to devices matching the criteria in the media attribute, hides additional content and makes the ScrollView fill the width of the browser:

  1. #additional-content {
  2. display:none;
  3. }
  4.  
  5. .yui3-scrollview {
  6. border:0;
  7. margin:0;
  8. width:100%;
  9. float:none;
  10. }
#additional-content {
    display:none;
}
 
.yui3-scrollview {
    border:0;
    margin:0;
    width:100%;
    float:none;
}

Copyright © 2010 Yahoo! Inc. All rights reserved.

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