Example: ScrollView with Scroll Indicator and Link Suppression Behavior

This example shows how to create a ScrollView widget with a scrollbar indicator. It also illustrates a technique for suppressing link behavior during a scroll — a technique you may require if you have a ScrollView instance heavily populated by links, as in this example.

The ScrollView Widget With A Scroll Indicator

In this example, we'll create a ScrollView instance, which also has a scrollbar indicator.

Modules Used

Since we want to use the base ScrollView, along with the ScrollViewScrollbars plugin, which provides the scrollbar indicator we use the scrollview module, instead of the scrollview-base module we used for the basic ScrollView example:

// Pulls in scrollview-base and scrollview-scrollbars plugin 
// and plugs it in (at the class level)

YUI().use('scrollview', function(Y) {
    ...
});

The scrollview module pulls in the basic ScrollView and also the ScrollViewScrollbars plugin. It has code which plugs the scrollbar plugin into the ScrollView base class:

Y.Base.plug(Y.ScrollView, Y.Plugin.ScrollViewScrollbars);

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 -->

As with the Base ScrollView example, we provide the markup for the ScrollView content on the page, 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>

And we instantiate the ScrollView instance in the same way, providing the srcNode attribute during construction, so it uses the markup above for it's content:

YUI().use('scrollview', function(Y) {

    var scrollView = new Y.ScrollView({
        id: "scrollview",
        srcNode: '#scrollview-content',
        height: 310,
        flick: {
            minDistance:10,
            minVelocity:0.3,
            axis: "y"
        }
    });

    scrollView.render();
});

Again, for this example, since we want a vertically scrolling ScrollView widget, we also give it a height during construction and constrain flicks to the "y" axis.

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

The only difference, compared to the Base ScrollView example, is that the ScrollViewScrollbars plugin has been pulled down and plugged in by the scrollview module code shown above, so the ScrollView now has a scroll indicator. The scroll indicator is styled with rounded corners in browsers which support CSS rounded corners natively.

Accessing The Scrollbars Plugin API

As discussed in the ScrollBar Plugin documentation, the API to control scrollbars is available on the scrollview instance, through the scrollView.scrollbars property. The ScrollBar plugin doesn't have too complex of an api, just a few methods to hide and show the scrollbars:

/* 
  scrollView.scrollbars is an instance of the ScrollViewScrollbars plugin 
*/
scrollView.scrollbars.hide();
scrollView.scrollbars.show(); 
scrollView.scrollbars.flash();
});

Suppressing Default Link Behavior

In this example, the scrolling surface is populated with links. To prevent links' default action (page navigation) from taking place after a scroll, we look at the lastScrolledAmt property of our ScrollView instance; on a simple click, that number will be very close to zero, whereas after scroll that number will be much higher. In this case, we're using a 2px threshold.

var content = scrollView.get("contentBox"); 

content.delegate("click", function(e) {
    // Prevent links from navigating as part of a scroll gesture
    if (Math.abs(scrollView.lastScrolledAmt) > 2) {
        e.preventDefault();
        Y.log("Link behavior suppressed.")
    }
}, "a");

We also prevent default on mousedown, to prevent the native "drag link to desktop" behavior on certain browsers.

content.delegate("mousedown", function(e) {
    // Prevent default anchor drag behavior, on browsers 
    // which let you drag anchors to the desktop
    e.preventDefault();
}, "a");

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>ScrollView</h1>
    </div>
    <div id="scrollview-content" class="yui3-scrollview-loading">
        <ul> 
            <li><a href="http://www.acdc.com/" title="AC/DC | The Official AC/DC Site">AC/DC</a></li> 
            <li><a href="http://www.aerosmith.com/" title="Aerosmith | Aerosmith -">Aerosmith</a></li> 
            <li><a href="http://www.billyjoel.com/" title="Billy Joel | The Official Billy Joel Site">Billy Joel</a></li> 
            <li><a href="http://www.bobdylan.com/" title="Home Page | Bob Dylan">Bob Dylan</a></li> 
            <li><a href="http://www.bobseger.com/" title="Bob Seger | Official Site | Home">Bob Seger</a></li> 
            <li><a href="http://www.bonjovi.com/" title="Bon Jovi &#134; The Official Website">Bon Jovi</a></li> 
            <li><a href="http://www.brucespringsteen.net/" title="BRUCE SPRINGSTEEN IS AWESOME">Bruce Springsteen</a></li> 
            <li><a href="http://www.creed.com/" title="Creed.com – The Official Website of Creed">Creed</a></li> 
            <li><a href="http://en.wikipedia.org/wiki/Creedence_Clearwater_Revival" title="Creedence Clearwater Revival - Wikipedia, the free encyclopedia">Creedence Clearwater Revival</a></li> 
            <li><a href="http://www.davematthewsband.com/" title="DAVE MATTHEWS BAND | Home Page | Dave Matthews Band">Dave Matthews Band</a></li> 
            <li><a href="http://www.defleppard.com/news/" title="DefLeppard.com | the official Def Leppard web site">Def Leppard</a></li> 
            <li><a href="http://www.eaglesband.com/" title="Eagles | eaglesband.com">The Eagles</a></li>
					<li><a href="http://www.bunnymen.com/" title="Echo &amp; The Bunnymen">Echo and the Bunnymen</a></li>
            <li><a href="http://www.eminem.com/" title="Eminem : Official Site">Eminem</a></li> 
            <li><a href="http://www.fleetwoodmac.com/" title="Fleetwood Mac">Fleetwood Mac</a></li> 
            <li>Green Day</li> 
            <li><a href="http://www.gunsnroses.com/" title="Guns N' Roses: Home: Chinese Democracy">Guns N' Roses</a></li> 
            <li><a href="http://www.jamestaylor.com/" title="James Taylor: The Official Site">James Taylor</a></li> 
            <li><a href="http://www.jay-z.com/index.php" title="Jay-Z.com: The Official Jay-Z Website">Jay-Z</a></li> 
            <li><a href="http://en.wikipedia.org/wiki/Jimi_Hendrix" title="Jimi Hendrix - Wikipedia, the free encyclopedia">Jimi Hendrix</a></li> 
            <li><a href="http://www.ledzeppelin.com/" title="Led Zeppelin - Official Website">Led Zeppelin</a></li> 
            <li><a href="http://www.lynyrdskynyrd.com/" title="Lynyrd Skynyrd">Lynyrd Skynyrd</a></li> 
            <li><a href="http://metallica.com/" title="Metallica.com">Metallica</a></li> 
            <li>Motley Crue</li> 
            <li><a href="http://www.neildiamond.com/" title="Official site from Neil Diamond">Neil Diamond</a></li> 
            <li><a href="http://en.wikipedia.org/wiki/Nirvana_(band)" title="Nirvana (band) - Wikipedia, the free encyclopedia">Nirvana</a></li> 
            <li><a href="http://www.ozzy.com/" title="Ozzy Osbourne | The Official Ozzy Osbourne Site">Ozzy Osbourne</a></li> 
            <li><a href="http://pearljam.com/" title="Home | Pearl Jam - Ten Club">Pearl Jam</a></li> 
            <li><a href="http://www.pinkfloyd.com/" title="Pink Floyd | The Official Site">Pink Floyd</a></li> 
            <li><a href="http://www.queenonline.com/" title="Queen Online.com">Queen</a></li> 
            <li><a href="http://www.rodstewart.com/" title="Rod Stewart | The Official Rod Stewart Site">Rod Stewart</a></li> 
            <li><a href="http://www.rush.com/" title="RUSH - Official Website">Rush</a></li> 
            <li><a href="http://www.santana.com/" title="SANTANA - The Official Carlos Santana Web Site">Santana</a></li> 
            <li><a href="http://www.simonandgarfunkel.com/" title="Simon &amp; Garfunkel official site">Simon and Garfunkel</a></li> 
            <li><a href="http://www.stevemillerband.com/" title="Steve Miller Band - Official Web Site">Steve Miller Band</a></li> 
            <li><a href="http://www.thebeatles.com/" title="The Beatles">The Beatles</a></li> 
            <li><a href="http://www.thedoors.com/" title="The Doors">The Doors</a></li> 
            <li><a href="http://www.thepolicefile.com/" title="ThePoliceFile.com: The Police - Concert Tickets - World Tour - The Police">The Police</a></li> 
            <li><a href="http://www.rollingstones.com/" title="The Official site of the Rolling Stones | Rollingstones.com">The Rolling Stones</a></li> 
            <li><a href="http://www.tompetty.com/" title="TomPetty.com &gt; Home">Tom Petty</a></li> 
            <li><a href="http://www.u2.com/" title="U2 &gt; Welcome">U2</a></li> 
            <li><a href="http://www.van-halen.com/" title="Van-Halen.com | the official Van Halen web site">Van Halen</a></li> 
            <li><a href="http://www.willienelson.com/" title="Home &raquo; 
            			Willie Nelson">Willie Nelson</a></li> 
            <li><a href="http://www.zztop.com/" title="ZZ Top | Home">ZZ Top</a></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', function(Y) {

        var scrollView = new Y.ScrollView({
            id: "scrollview",
            srcNode: '#scrollview-content',
            height: 310,
            flick: {
                minDistance:10,
                minVelocity:0.3,
                axis: "y"
            }
        });

        scrollView.render();
        
        var content = scrollView.get("contentBox"); 

				content.delegate("click", function(e) {
				    // Prevent links from navigating as part of a scroll gesture
				    if (Math.abs(scrollView.lastScrolledAmt) > 2) {
				        e.preventDefault();
						Y.log("Link behavior suppressed.")
				    }
				}, "a");

        content.delegate("mousedown", function(e) {
            // Prevent default anchor drag behavior, on browsers which let you drag anchors to the desktop
            e.preventDefault();
        }, "a");
    });

</script>