5
<title>Example: Horizontal ScrollView</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: Horizontal ScrollView</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><div class="intro">
21
<p>This example shows how to create a ScrollView widget which scrolls horizontally.</p>
24
<div class="example newwindow">
25
<a href="scrollview-horiz-example.html" target="_blank" class="button">
26
View Example in New Window
30
<h2>A Horizontal ScrollView</h2>
32
<p>In this example, we'll create a ScrollView instance, which scrolls horizontally, as opposed to the vertically scrolling ScrollView we created in the <a href="scrollview.html">ScrollView With Scroll Indicator</a> example.</p>
34
<p>The code for the example is pretty much the same as the code for the <a href="scrollview.html">ScrollView With Scroll Indicator</a> 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).</p>
38
<p>Since we want to use the base ScrollView, along with the ScrollViewScrollbars plugin, we use the <code>scrollview</code> module as we did for the vertical ScrollView example:</p>
40
<pre class="code prettyprint">YUI().use('scrollview', function(Y) {
45
<h3>Instantiating The ScrollView Widget</h3>
47
<p>As with the vertical <a href="scrollview.html">ScrollView</a> example, we provide the markup for the ScrollView content on the page, as shown below:</p>
49
<pre class="code prettyprint"><!-- Content with a width which would require scrolling -->
50
<div id="scrollview-content" class="yui3-scrollview-loading">
52
<li><img src="..."></li>
53
<li><img src="..."></li>
54
<li><img src="..."></li>
55
<li><img src="..."></li>
57
</div></pre>
60
<p>But this time, when we instantiate the ScrollView instance, we provide a fixed width, as opposed to a fixed height, for the widget.</p>
62
<pre class="code prettyprint">// Constraining the width, instead of the height for horizontal scrolling
63
var scrollView = new Y.ScrollView({
64
id: 'scrollview',
65
srcNode: '#scrollview-content',
74
scrollView.render();</pre>
77
<p>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. As with the ScrollView Base example, we constrain flick handling to a specific axis, in this case the "x" axis.</p>
79
<p>The important CSS for the example, which switches the LIs to layout horizontally, is shown below, along with some tweaks requires for IE 6 and 7 support:</p>
81
<pre class="code prettyprint">/* To layout horizontal LIs */
86
#scrollview-content li {
88
background-color:#fff;
91
/* For IE 6/7 - needs inline block hack (and the background color mentioned above) */
92
#scrollview-content li {
98
<p><strong>NOTE:</strong> In the initial ScrollView release (3.2.0), the above CSS to layout LIs horizontally was bundled with the ScrollView CSS out-of-the-box. It has been removed as of the 3.3.0 release, since it makes it harder for developers to nest lists inside the ScrollView content, and in general, ScrollView is content agnostic.</p>
100
<h3>Preventing Native Behavior For Content</h3>
102
<p>In this 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:</p>
104
<pre class="code prettyprint">// Prevent the image from being natively dragged
105
content.delegate("mousedown", function(e) {
107
}, "img");</pre>
110
<h2>Complete Example Source</h2>
111
<pre class="code prettyprint"><div id="scrollview-container">
112
<div id="scrollview-header">
113
<h1>Horizontal ScrollView</h1>
116
<div id="scrollview-content" class="yui3-scrollview-loading">
118
<li><img src="http://farm5.static.flickr.com/4136/4802088086_c621e0b501.jpg" alt="Above The City II"></li>
119
<li><img src="http://farm5.static.flickr.com/4114/4801461321_1373a0ef89.jpg" alt="Walls and Canyon"></li>
120
<li><img src="http://farm5.static.flickr.com/4100/4801614015_4303e8eaea.jpg" alt="Stairs Using In Situ Stone"></li>
121
<li><img src="http://farm5.static.flickr.com/4076/4801368583_854e8c0ef3.jpg" alt="Tree Silhouette"></li>
126
<div id="additional-content">
127
<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>
128
<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>
131
<script type="text/javascript" charset="utf-8">
132
YUI().use('scrollview', function(Y) {
134
var scrollView = new Y.ScrollView({
135
id: 'scrollview',
136
srcNode: '#scrollview-content',
147
// Prevent default image drag behavior
148
scrollView.get("contentBox").delegate("mousedown", function(e) {
153
</script></pre>
158
<div id="sidebar" class="yui3-u">
162
<div class="sidebox">
164
<h2 class="no-toc">Examples</h2>
168
<ul class="examples">
171
<li data-description="This example creates a basic ScrollView which doesn't include a scrollbar indicator.">
172
<a href="scrollview-base.html">Basic ScrollView Without a Scroll Indicator</a>
177
<li data-description="This example shows the classic Scrollview implementation, including scroll indicators (bars) and including code to suppress link navigation while scrolling.">
178
<a href="scrollview.html">ScrollView with Scroll Indicator and Link Suppression Behavior</a>
183
<li data-description="This example creates a horizontal ScrollView.">
184
<a href="scrollview-horiz.html">Horizontal ScrollView</a>
189
<li data-description="This example creates a horizontal ScrollView with pagination support.">
190
<a href="scrollview-paging.html">ScrollView With Pagination</a>
204
<script src="../assets/vendor/prettify/prettify-min.js"></script>
205
<script>prettyPrint();</script>