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:
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
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 -->
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:
<div id="scrollview-content" class="yui3-scrollview-loading"> <ul> <li>AC/DC</li> <li>Aerosmith</li> <li>Billy Joel</li> <li>Bob Dylan</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:
.yui3-js-enabled .yui3-scrollview-loading { visibility:hidden; }
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.
YUI().use('scrollview-base', function(Y) { var scrollView = new Y.ScrollView({ id:"scrollview", srcNode: '#scrollview-content', height: 310, flick: { minDistance:10, minVelocity:0.3, axis: "y" } }); 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. We also give the ScrollView widget bounding box an id ("scrollview") which we can target in the example CSS. Finally, we constrain flicks so that only flicks along the "y" axis are picked up.
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:
<link media="handheld, only screen and (max-device-width: 480px)" href="../assets/scrollview/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:
#additional-content { display:none; } .yui3-scrollview { border:0; margin:0; width:100%; float:none; }
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>Basic ScrollView</h1> </div> <div id="scrollview-content" class="yui3-scrollview-loading"> <ul> <li>AC/DC</li> <li>Aerosmith</li> <li>Billy Joel</li> <li>Bob Dylan</li> <li>Bob Seger</li> <li>Bon Jovi</li> <li>Bruce Springsteen</li> <li>Creed</li> <li>Creedence Clearwater Revival</li> <li>Dave Matthews Band</li> <li>Def Leppard</li> <li>Eagles</li> <li>Eminem</li> <li>Fleetwood Mac</li> <li>Green Day</li> <li>Guns N' Roses</li> <li>James Taylor</li> <li>Jay-Z</li> <li>Jimi Hendrix</li> <li>Led Zeppelin</li> <li>Lynyrd Skynyrd</li> <li>Metallica</li> <li>Motley Crue</li> <li>Neil Diamond</li> <li>Nirvana</li> <li>Ozzy Osbourne</li> <li>Pearl Jam</li> <li>Pink Floyd</li> <li>Queen</li> <li>Rod Stewart</li> <li>Rush</li> <li>Santana</li> <li>Simon and Garfunkel</li> <li>Steve Miller Band</li> <li>The Beatles</li> <li>The Doors</li> <li>The Police</li> <li>The Rolling Stones</li> <li>Tom Petty</li> <li>U2</li> <li>Van Halen</li> <li>Willie Nelson</li> <li>ZZ Top</li> </ul> </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-base', function(Y) { var scrollView = new Y.ScrollView({ id:"scrollview", srcNode: '#scrollview-content', height: 310, flick: { minDistance:10, minVelocity:0.3, axis: "y" } }); scrollView.render(); }); </script>