(Another) Scrollable Plugin for jQuery

I was having a bit of difficulty finding a jQuery plugin that not only provided scrolling behavior to a list of elements, but also used & then generated markup that could still be accessible, semantic & validate whether or not the user ran the javascript.

jQueryThe best plugin that I could find that was already out there was Tero Piirainen's jquery.scrollable.js. If you check it out you'll quickly see that the markup that's used to generate the scrolling items could be improved upon. Nothing in my version of jquery.scrollable.js removes functionality from what Tero put together, and the improvements are basic.

  1. make those items scrollable horizotally or vertically
  2. decide how many items are visible at once
  3. scroll elements with mouse, arrow keys and mousewheel (requires mousewheel.js)
  4. make navigational buttons without programming
  5. have programmatic actions: next, prev, nextPage, prevPage, seekTo, begin, end
  6. need to know when list is scrolled with custom event listener
  7. You can now start with semantic markup
  8. I want this all in single js file that weights only 3.9 4.1 Kb minified!

First we start with simple & semantic markup

There's nothing fancy going on here at all, just a wrapping element, an anchor and an unordered list. Unfortunately due to the design requirements of this particular implementation, an additional <div> had to be added. If it wasn't for the creative constraint, and the lack of support for vertical-align: middle across many browsers, this extra <div> would have not been included.

HTML:
  1. <div class="scrolling-list">
  2.     <a href="#newspage" class="more-link">More News</a>
  3.   <ol>
  4.         <li><div><a href="#">Someone reports second-quarter 2008 earnings &raquo;</a></div></li>
  5.         <li><div><a href="#">Bear scares comedian, everybody laughs &raquo;</a></div></li>
  6.         <li><div><a href="#">Someone Completes Someone else's Acquisition &raquo;</a></div></li>
  7.         <li><div><a href="#">Someone selected by Some Company, S.A.B. de C.V. for ADR program &raquo;</a></div></li>
  8.         <li><div><a href="#">Someone wins landmark multinational asset pooling mandate &raquo;</a></div></li>
  9.         <li><div><a href="#">Someone Announces Internal Restructuring Transactions and Guarantees Related to Acquisition &raquo;</a></div></li>
  10.         <li><div><a href="#">Someone wins comprehensive custodial mandate with one of New Zealand's largest organizations &raquo;</a></div></li>
  11.         <li><div><a href="#">Someone selected by East Timor to provide custodial services and training opportunities &raquo;</a></div></li>
  12.         <li><div><a href="#">Someone launches latest Global Derivatives Collateral offering &raquo;</a></div></li>
  13.         <li><div><a href="#">Someone expands Boston fund services operations &raquo;</a></div></li>
  14.   </ol>
  15. </div>

Presentation

Now we add styling that makes it match the look & feel of the main site.

The css required to make this bit work for you will be different for every project, so take a look at the included styles and tweak as necessary.

And now for the behavior

If the user's browser allows javascript to run, we add some additional unobtrusive behavior (including mousewheel support)

JavaScript:
  1. jQuery(function( $ ){
  2.     /**
  3.      * make sure the CSS knows that we're modifying the DOM and then style accordingly
  4.      **/       
  5.     $('.scrollable').addClass('js');
  6.     /**
  7.      * If you have an element you'd like to use the same style, but not attach the
  8.      * behavior you can turn it off by first adding the scrollable classname, but making
  9.      * sure the js classname is not applied to the element (this is what I did to show the
  10.      * "no javascript" version in the second example). Keeping the use of the scrollable
  11.      * classname for the general styles prevents having to write redundant styles
  12.      **/
  13.     $('.scrollable.nojs').removeClass('js');
  14.     /**
  15.      * a little DOM modification to keep all of the markup valid
  16.      **/
  17.     $('.scrollable.js ol').attr('id','items').wrap('<ul><li></li></ul>');
  18.     $('.scrollable.js ul>li').attr('id','itemswrapper');
  19.     var emptyLinkTarget = (window.opera) ? "#" : "javascript:function(){return}"; // a hash breaks a few flavors of IE & a javascript link (though it's poor form) breaks Opera.
  20.     $('.scrollable.js ul').prepend('<li><a class="prev" href="'+emptyLinkTarget+'">&lt;&lt;</a></li>').append('<li><a class="next" href="'+emptyLinkTarget+'">&gt;&gt;</a></li>');
  21.     /**
  22.      * and now we initiate
  23.      **/
  24.     $('.scrollable.js').scrollable({
  25.         size:3,
  26.         horizontal:false,
  27.         duration:1500,
  28.         items:'#items',
  29.         prev:'.prev',
  30.         next:'.next'
  31.     });
  32. });

This is the generated source we end up with... There's one thing you might notice, and that's the div class="__scrollable", though I initially placed that element outside of it's parent ol it did not work. It has something to do with how the xhtml 1.0 strict doctype is handled by the browsers. Though this may work in HTML 4.0 or various browser quirks modes, I haven't specifically tested all possible cases.

HTML:
  1. <div class="scrollable js">
  2.     <a href="#" class="more-link">More News</a>
  3.     <ul>
  4.         <li><a class="prev" href="javascript:function(){return}">&lt;&lt;</a></li>
  5.         <li id="itemswrapper">
  6.             <ol style="overflow: hidden; position: relative; visibility: visible; height: 124px;" id="items">
  7.                 <div class="__scrollable" style="position: absolute; height: 200000em;">
  8.                     <li><div><a href="#">Someone reports second-quarter 2008 earnings »</a></div></li>
  9.                     <li><div><a href="#">Bear scares comedian, everybody laughs »</a></div></li>
  10.                     <li><div><a href="#">Someone Completes Someone else's Acquisition »</a></div></li>
  11.                     <li><div><a href="#">Someone selected by Some Company, S.A.B. de C.V. for ADR program »</a></div></li>
  12.                     <li><div><a href="#">Someone wins landmark multinational asset pooling mandate »</a></div></li>
  13.                     <li><div><a href="#">Someone Announces Internal Restructuring Transactions and Guarantees Related to Acquisition »</a></div></li>
  14.                     <li><div><a href="#">Someone wins comprehensive custodial mandate with one of New Zealand's largest organizations »</a></div></li>
  15.                     <li><div><a href="#">Someone selected by East Timor to provide custodial services and training opportunities »</a></div></li>
  16.                     <li><div><a href="#">Someone launches latest Global Derivatives Collateral offering »</a></div></li>
  17.                     <li><div><a href="#">Someone expands Boston fund services operations »</a></div></li>
  18.                 </div>
  19.                 <br clear="all"/>
  20.             </ol>
  21.         </li>
  22.         <li><a class="next" href="javascript:function(){return}">&gt;&gt;</a></li>
  23.     </ul>
  24. </div>

This build is currently in a non-public svn repository, but I will be moving it to google code or github shortly (depending on whether or not I feel like learning how to use Git)

Standalone Demo:
http://www.naterkane.com/sandbox/scrollable/
Download:
First you'll of course need jQuery v.1.2.6
jquery.scrollable.js (7.5k)
jquery.scrollable.min.js (4.1k)
scrolling-list.css – the stylesheet used for the example
You can also grab a copy of jquery.mousewheel.js here


35 Responses a “(Another) Scrollable Plugin for jQuery”

  1. Can you extend this to allow adding and removing of navigation items?

  2. Can you extend this to allow adding and removing of navigation items?

  3. Can you extend this to allow adding and removing of navigation items?

  4. Can you extend this to allow adding and removing of navigation items?

  5. Can you extend this to allow adding and removing of navigation items?

  6. Can you extend this to allow adding and removing of navigation items?

  7. this looks great, congrats

  8. this looks great, congrats

  9. this looks great, congrats

  10. this looks great, congrats

  11. this looks great, congrats

  12. this looks great, congrats

  13. Thanks for the info. May God have mercy on us all.

  14. Barbara J Najera Says:

    god bless your blog my friend :)

  15. delorisrhoyle Says:

    Thank you for sharing this information.
    Fidelity 401k

  16. buyresearchpaper Says:

    To create is very doughty decision. I hope you’ll anticipate more the pressures that may push customers into using essay writing services, and the ways we might try to facilitate those encumbrance and not simply mingle them. Anyway, thanks for the design for augment the materiality of essay writing for those who value it as a quality of learning exercise.
    http://www.bestwritingservice.com

  17. A lot students give the duty to expert resume writers because they don't have the talent to write a satisfactory resume in that the reason why people
    need to resume writer, but such people like creator don't do that. Thanks for the information. Very interesting topic about this.

  18. You make very perfect contribution just about Nater! So, the dissertation writing service or some thesis writing services, could use in the best dissertation.

  19. it can be expanded to add and remove items, however the init script would have to be rewritten to also be attached to a custom even that's fired when those items are added or removed.

  20. You should apply css - overflow: hidden - to the buttons and perhaps - outline: none - as they are extending outline far to the left.
    Apart from this great tutorial - thanks for sharing!

  21. You should apply css - overflow: hidden - to the buttons and perhaps - outline: none - as they are extending outline far to the left.
    Apart from this great tutorial - thanks for sharing!

  22. [...] (Another) Scrollable Plugin for jQuery [...]

  23. [...] Scrollable Plugin for jQuery [...]

  24. wow very nice :)
    But I just wanted to know can we make it auto scroll ??

    Right now it's on manual scroll... so is it possible to make it auto scroll after some time delay ??

  25. [...] 31-Scrollable Plugin for jQuery [...]

  26. sure, though i don't personally like the idea of any content moving in an interface without there being any deliberate user interaction.

    if you make it happen, and let it be a configurable option, send me a patch and i might include it in an update.

  27. Very interesting read about a plugin that you have found - Tero Piirainen'S jquery.scrollable.js.On really good and easy to use. And thanks to the author, you wrote about in great detail the installation and use of this plugin. Helped me a lot about your article Scrollable plugin for JQuery.

  28. Nice article.keep it up!

  29. Honestly, I have seen lots of better implementations

  30. [...] (Another) Scrollable Plugin for jQuery [...]

  31. it was written in 2008.

  32. [...] 5. Scrollable [...]

  33. [...] (Another) Scrollable Plugin for jQuery [...]

  34. [...] 5. Scrollable [...]

  35. Can you extend this to allow adding and removing of navigation items?

Leave a comment

Categories

Archives

About

Nater Kane is freelance developer and user experience & technology consultant based in Brooklyn, NY.

Nater's focus is to make the web a better place, one decision at a time.

He likes to spend time playing with his cats, riding bicycles around the city, working on his diesel vw rabbit or motorcycle, and enjoying a decent espresso.

RSS

Twitter » What I'm Up To

Elsewhere

JavaScript JS Documentation: JS String toUpperCase, JavaScript String toUpperCase, JS String .toUpperCase, JavaScript String .toUpperCase

Basecamp

Meta


Nater Kane naterkane personal http://www.naterkane.com LinkedIn Profile Web Technologist personal nater@naterkane.com 1978-09-12 voice 845.234.6698 | fax 707.922.0593
964 Flushing Ave. Brooklyn, NY 11206