YUI 3.x Home -

YUI Library Examples: ScrollView: ScrollView with Scroll Indicator and Link Suppression Behavior.

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

  1.  
  2. // Pulls in scrollview-base and scrollview-scrollbars plugin
  3. // and plugs it in (at the class level)
  4.  
  5. YUI().use('scrollview', function(Y) {
  6. ...
  7. });
  8.  
 
// 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:

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

Instantiating The ScrollView Widget

As with the Base ScrollView example, we provide the markup for the ScrollView content on the page, as shown below:

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

  1. YUI().use('scrollview', function(Y) {
  2.  
  3. var scrollView = new Y.ScrollView({
  4. id: "scrollview",
  5. srcNode: '#scrollview-content',
  6. height: 310,
  7. flick: {
  8. minDistance:10,
  9. minVelocity:0.3,
  10. axis: "y"
  11. }
  12. });
  13.  
  14. scrollView.render();
  15. });
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:

  1. /*
  2.   scrollView.scrollbars is an instance of the ScrollViewScrollbars plugin
  3.   */
  4. scrollView.scrollbars.hide();
  5. scrollView.scrollbars.show();
  6. scrollView.scrollbars.flash();
  7. });
    /* 
      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.

  1. var content = scrollView.get("contentBox");
  2.  
  3. content.delegate("click", function(e) {
  4. // Prevent links from navigating as part of a scroll gesture
  5. if (Math.abs(scrollView.lastScrolledAmt) > 2) {
  6. e.preventDefault();
  7. Y.log("Link behavior suppressed.")
  8. }
  9. }, "a");
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.

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

Copyright © 2011 Yahoo! Inc. All rights reserved.

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