<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>ScrollView</title>
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic">
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css">
<link rel="stylesheet" href="../assets/css/main.css">
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
<script src="../../build/yui/yui-min.js"></script>
</head>
<body>
<!--
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>
-->
<div id="doc">
<div id="hd">
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1>
</div>
<a href="#toc" class="jump">Jump to Table of Contents</a>
<h1>ScrollView</h1>
<div class="yui3-g">
<div class="yui3-u-3-4">
<div id="main">
<div class="content"><div class="intro">
<p>ScrollView's main use case is to provide a scrollable content widget for touch devices on which overflow scrollbars are not natively supported. However, it is built on top of YUI's cross-platform/browser gesture and transition layers, so it can also be used to provide flickable scrolling on mouse-based devices across on all the A-grade browsers.</p>
<p>The <code>ScrollViewScrollbars</code> plugin can be added to the base scrollview to provide a scroll indicator. The <code>ScrollViewPaginator</code> plugin can be added to provide a cross-platform paginated scrolling implementation (a simple carousel).</p>
</div>
<h2 id="getting-started">Getting Started</h2>
<p>
To include the source files for ScrollView and its dependencies, first load
the YUI seed file if you haven't already loaded it.
</p>
<pre class="code prettyprint"><script src="http://yui.yahooapis.com/3.10.3/build/yui/yui-min.js"></script></pre>
<p>
Next, create a new YUI instance for your application and populate it with the
modules you need by specifying them as arguments to the <code>YUI().use()</code> method.
YUI will automatically load any dependencies required by the modules you
specify.
</p>
<pre class="code prettyprint"><script>
// Create a new YUI instance and populate it with the required modules.
YUI().use('scrollview', function (Y) {
// ScrollView is available and ready for use. Add implementation
// code here.
});
</script></pre>
<p>
For more information on creating YUI instances and on the
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the
documentation for the <a href="../yui/index.html">YUI Global Object</a>.
</p>
<p>
<strong>Note:</strong> be sure to add the <code>yui3-skin-sam</code> classname to the
page's <code><body></code> element or to a parent element of the widget in order to apply
the default CSS skin. See <a href="http://yuilibrary.com/yui/docs/tutorials/skins/">Understanding Skinning</a>.
</p>
<pre class="code prettyprint"><body class="yui3-skin-sam"> <!-- You need this skin class --></pre>
<h2 id="using">Using ScrollView</h2>
<h3 id="instantiating">Instantiating A ScrollView</h3>
<p><code>ScrollView</code> extends the <code>Widget</code> class, and hence follows the same pattern as any widget constructor, accepting a configuration object to set the initial configuration for the widget.</p>
<h4 id="instantiating-from-markup-progressive-enhancement">Instantiating From Markup: Progressive Enhancement</h4>
<p>A ScrollView widget is instantiated from markup by specifying the <code>srcNode</code> which contains the content to be scrolled. The content is usually in the form of a list, but doesn't necessarily have to be:</p>
<pre class="code prettyprint"><div id="scrollable" class="yui3-scrollview-loading">
<ul>
<li>AC/DC</li>
<li>Aerosmith</li>
<li>Bob Dylan</li>
<li>Bob Seger</li>
...
</ul>
</div></pre>
<p>The <code>yui3-scrollview-loading</code> class is applied by the developer to progressively enhanced markup, and can be used along with the <code>yui3-js-enabled</code> class applied to the document element, to hide the scrollview markup from view, while the JS is being loaded. The examples show how to set up a <code>yui3-scrollview-loading</code> rule to hide progressively enhanced content.</p>
<p>When instantiating from markup, a reference to the <code>srcNode</code> is provided to the constructor as part of the configuration object. This reference can be a node reference, or a selector string which can be used to identify the node. If the selector string is too broad (returns multiple nodes), the first node found will be used.
The following code references the markup shown above, while constructing the scrollview:</p>
<pre class="code prettyprint">YUI({...}).use("scrollview", function(Y) {
var scrollview = new Y.ScrollView({
srcNode:"#scrollable",
height:"20em"
});
});</pre>
<p>Generally, a ScrollView widget will be scrollable either horizontally (along the X axis) or vertically (along the Y axis). The ScrollView determines which direction it can scroll in based on whether or not its content is taller (vertically scrolled) or wider (horizontally scrolled) than its height or width respectively, as you would expect
from native overflow handling. In the above example the widget is given a fixed height, so that it will scroll vertically, when the content gets larger than the specified height. In most use cases you will provide either a height or a width to the ScrollView widget constructor.</p>
<p>Following instantiation, a call to <code>render</code> is required to have the ScrollView's state reflected in the DOM, as with all YUI 3 widgets:</p>
<pre class="code prettyprint">var scrollview = new Y.ScrollView({ ... });
scrollview.render();</pre>
<h3 id="attributes">Attributes</h3>
<p>ScrollView provides the following core attributes, in addition to the attributes provided by the base <a href="../widget/index.html#attributes">Widget</a> class:</p>
<table>
<tr><th>Attribute</th><th>Description</th></tr>
<tr><td><code>scrollX</code></td><td>The number of pixels to scroll the widget's content by, along the X axis (vertically).</td></tr>
<tr><td><code>scrollY</code></td><td>The number of pixels to scroll the widget's content by, along the Y axis (horizontally).</td></tr>
</table>
<p>Additionally, the following attributes control the sensitivity and scroll dynamics, per instance:</p>
<table>
<tr><th>Attribute</th><th>Description</th><th>Default</th></tr>
<tr><td><code>axis</code></td><td>A string which specifies the axis (or axes) should be scrolled on. Valid values are <code>x</code>, <code>y</code>, or <code>xy</code>. This is an optional attribute, so if unspecified, ScrollView will attempt to determine the desired scroll axis based off the overflow of content.</td><td><code>auto calculation</code></td></tr>
<tr><td><code>bounce</code></td><td>A drag coefficient, which defines how quickly the velocity of the scrollview content decreases after a flick, when it's past the min and max bounds for the widget. Can be set to 0 to disable bounce behavior.</td><td><code>0.1</code></td></tr>
<tr><td><code>bounceRange</code></td><td>The default bounce distance in pixels.</td><td><code>150</code></td></tr>
<tr><td><code>deceleration</code></td><td>A drag coefficient, which defines how quickly the velocity of the scrollview content decreases after a flick, when it's still with in the min and max bounds for the widget. The closer this is to 1, the less friction there is after a flick.</td><td><code>0.98</code></td></tr>
<tr><td><code>easing</code></td><td>The default easing used when scrolling to a given X or Y.</td><td><code>cubic-bezier(0, 0.1, 0, 1.0)</code></td></tr>
<tr><td><code>flick</code></td><td>An object which specifies the minimum distance and minimum velocity (in pixels/ms - generally around 0.5) which should constitute a flick gesture. Can be set to false to disable flick support, in which case the ScrollView content can only be dragged to scroll it.</td><td><code>{minDistance:10, minVelocity:0.3}</code></td></tr>
<tr><td><code>frameDuration</code></td><td>The default time interval used when animating the flick deceleration.</td><td><code>30</code></td></tr>
<tr><td><code>snapDuration</code></td><td>The default duration (in ms) used for the snap back animation when scrolled past the min or max bounds for the widget.</td><td><code>ease-out</code></td></tr>
<tr><td><code>snapEasing</code></td><td>The default easing used for the snap back animation when scrolled past the min or max bounds for the widget.</td><td><code>ease-out</code></td></tr>
</table>
<h3 id="markup">Markup Structure</h3>
<p>The final rendered ScrollView has the markup structure shown below (shown for a vertical scrollview):</p>
<pre class="code prettyprint"><!-- Bounding Box, with overflow setting and fixed dimension (in this case height) -->
<div class="yui3-widget yui3-scrollview yui3-scrollview-vert" style="height: 310px;">
<!-- Content Box, which is scrolled using either top/left, or transform:translate, where supported -->
<div class="yui3-scrollview-content" id="scrollable" style="left: 0px; top: 0px;">
... scrollable content. generally a list ...
</div>
<!-- Scrollbar indicator, if Scrollbar Plugin is added (added by default for the "scrollview" module) -->
<div class="yui3-scrollview-scrollbar yui3-scrollview-scrollbar-vert">
<span class="yui3-scrollview-child yui3-scrollview-first"></span>
<span class="yui3-scrollview-child yui3-scrollview-middle"></span>
<span class="yui3-scrollview-child yui3-scrollview-last"></span>
</div>
</div></pre>
<p>The <code>yui3-scrollview-vert</code>, or <code>yui3-scrollview-horiz</code> classes applied to the bounding box can be used to style the scrollview based on its scroll orientation.</p>
<h3 id="css">CSS</h3>
<p>The core structural css for the ScrollView is shown below, and is pretty straightforward. It simply sets up the bounding box and the content box as positioned elements, and sets overflow hidden, so that the
content beyond the bounding box area is hidden, until scrolled into view.</p>
<pre class="code prettyprint">/* Bounding Box */
.yui3-scrollview {
position: relative; /* To provide positioning context */
overflow: hidden; /* To clip scrolled content */
...
}
/* Content Box */
.yui3-scrollview-content {
position:relative; /* To allow positioning */
}</pre>
<p>Since the scrollable content is commonly set up as a list (for semantic reasons), the core css for ScrollView also ships with a rule targeting list items (<code>LI</code>s), so that list content works out of the box, for both vertical and horizontal scrollviews (inline-block is used so that we can trivially support the horizontal use case):</p>
<pre class="code prettyprint">.yui3-scrollview-content ul {
margin:0;
padding:0;
}
.yui3-scrollview-content li {
padding:0;
margin:0;
list-style:none;
/* cross browser inline block */
display: -moz-inline-stack;
display: inline-block;
*display: inline;
...
}</pre>
<p>The above rules can of course be over-ridden for cases in which you don't want lists inside ScrollView handled specially.</p>
<h3 id="scrollbars">ScrollBar Plugin</h3>
<p>The ScrollBar plugin provides a visual scroll indicator to let the user know how much scrollable content there is, and the current scroll position they're at.</p>
<p>When using the <code>scrollview</code> module, the <code>ScrollViewScrollbars</code> plugin is automatically pulled down and plugged in for all ScrollView instances (plugged in at the class level).</p>
<p>However, the user can also choose to pull down just the base scrollview module (<code>scrollview-base</code>) and add scrollbar support optionally (when and if required).</p>
<pre class="code prettyprint">YUI({...}).use("scrollview-base", "scrollview-scrollbars", function(Y) {
/* ScrollView without scrollbar indicator */
var scrollview = new Y.ScrollView({
srcNode:"#scrollable",
height:"20em"
});
/* Add scrollbar indicator support */
scrollview.plug(Y.Plugin.ScrollViewScrollbars);
/*
scrollview.scrollbars is an instance of the ScrollViewScrollbars
plugin
*/
scrollview.scrollbars.hide();
scrollview.scrollbars.show();
scrollview.scrollbars.flash();
});</pre>
<p>When the <code>ScrollViewScrollbars</code> plugin is plugged in to a ScrollView instance, a <code>scrollbars</code> property (the namespace for the plugin) is added to the ScrollView instance, which points to an instance of the plugin, as shown above. The plugin provides public <code>hide()</code>, <code>show()</code> and <code>flash()</code> methods for the scroll indicator.</p>
<h3 id="paginator">Paginator Plugin</h3>
<p>The <code>ScrollViewPaginator</code> plugin adds pagination support to ScrollView, so that it recognizes, and stops at, discrete page boundaries within its content, as opposed to scrolling continuously (it effectively makes ScrollView behave like a simple Carousel).</p>
<p>Adding the <code>scrollview-paginator</code> module to the <code>use</code> statement will pull down and add the <code>Plugin.ScrollViewPaginator</code> plugin class to the YUI instance. It can then be plugged in to ScrollView instances requiring pagination support, as shown below:</p>
<pre class="code prettyprint">YUI({...}).use("scrollview-base", "scrollview-paginator", function(Y) {
/* ScrollView without paginator */
var scrollview = new Y.ScrollView({
srcNode:"#scrollable",
height:"20em"
});
/* Plug in pagination support */
scrollview.plug(Y.Plugin.ScrollViewPaginator, {
selector: "li" // elements definining page boundaries
});
/*
scrollview.pages is an instance of the ScrollViewPaginator
plugin
*/
scrollview.pages.set("index", 3);
scrollview.pages.scrollTo(3, 0.6, "ease-in");
scrollview.pages.next();
scrollview.pages.get("total");
});</pre>
<p>The paginator plugin accepts a <code>selector</code> attribute as part of its configuration, which selects the list of elements to be used to establish page boundaries. In the example above, each <code>LI</code> within the ScrollView's content box, defines a <em>page</em> in the ScrollView.</p>
<p>When plugged in, the plugin API is available on the <code>pages</code> property of the scrollview widget instance, and can be used to set the current page, or retrieve paging information.</p>
</div>
</div>
</div>
<div class="yui3-u-1-4">
<div class="sidebar">
<div id="toc" class="sidebox">
<div class="hd">
<h2 class="no-toc">Table of Contents</h2>
</div>
<div class="bd">
<ul class="toc">
<li>
<a href="#getting-started">Getting Started</a>
</li>
<li>
<a href="#using">Using ScrollView</a>
<ul class="toc">
<li>
<a href="#instantiating">Instantiating A ScrollView</a>
<ul class="toc">
<li>
<a href="#instantiating-from-markup-progressive-enhancement">Instantiating From Markup: Progressive Enhancement</a>
</li>
</ul>
</li>
<li>
<a href="#attributes">Attributes</a>
</li>
<li>
<a href="#markup">Markup Structure</a>
</li>
<li>
<a href="#css">CSS</a>
</li>
<li>
<a href="#scrollbars">ScrollBar Plugin</a>
</li>
<li>
<a href="#paginator">Paginator Plugin</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples</h2>
</div>
<div class="bd">
<ul class="examples">
<li data-description="This example creates a basic ScrollView which doesn't include a scrollbar indicator.">
<a href="scrollview-base.html">Basic ScrollView Without a Scroll Indicator</a>
</li>
<li data-description="This example shows the classic Scrollview implementation, including scroll indicators (bars) and including code to suppress link navigation while scrolling.">
<a href="scrollview-scroll.html">ScrollView with Scroll Indicator and Link Suppression Behavior</a>
</li>
<li data-description="This example creates a horizontal ScrollView.">
<a href="scrollview-horiz.html">Horizontal ScrollView</a>
</li>
<li data-description="This example creates a horizontal ScrollView with pagination support.">
<a href="scrollview-paging.html">ScrollView With Pagination</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script>
YUI.Env.Tests = {
examples: [],
project: '../assets',
assets: '../assets/scrollview',
name: 'scrollview',
title: 'ScrollView',
newWindow: '',
auto: false
};
YUI.Env.Tests.examples.push('scrollview-base');
YUI.Env.Tests.examples.push('scrollview-scroll');
YUI.Env.Tests.examples.push('scrollview-horiz');
YUI.Env.Tests.examples.push('scrollview-paging');
</script>
<script src="../assets/yui/test-runner.js"></script>
</body>
</html>