(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.
The 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.
- make those items scrollable horizotally or vertically
- decide how many items are visible at once
- scroll elements with mouse, arrow keys and mousewheel (requires mousewheel.js)
- make navigational buttons without programming
- have programmatic actions: next, prev, nextPage, prevPage, seekTo, begin, end
- need to know when list is scrolled with custom event listener
- You can now start with semantic markup
- I want this all in single js file that weights only
3.94.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.
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)
-
jQuery(function( $ ){
-
/**
-
* make sure the CSS knows that we're modifying the DOM and then style accordingly
-
**/
-
$('.scrollable').addClass('js');
-
/**
-
* If you have an element you'd like to use the same style, but not attach the
-
* behavior you can turn it off by first adding the scrollable classname, but making
-
* sure the js classname is not applied to the element (this is what I did to show the
-
* "no javascript" version in the second example). Keeping the use of the scrollable
-
* classname for the general styles prevents having to write redundant styles
-
**/
-
$('.scrollable.nojs').removeClass('js');
-
/**
-
* a little DOM modification to keep all of the markup valid
-
**/
-
$('.scrollable.js ol').attr('id','items').wrap('<ul><li></li></ul>');
-
$('.scrollable.js ul>li').attr('id','itemswrapper');
-
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.
-
$('.scrollable.js ul').prepend('<li><a class="prev" href="'+emptyLinkTarget+'"><<</a></li>').append('<li><a class="next" href="'+emptyLinkTarget+'">>></a></li>');
-
/**
-
* and now we initiate
-
**/
-
$('.scrollable.js').scrollable({
-
size:3,
-
horizontal:false,
-
duration:1500,
-
items:'#items',
-
prev:'.prev',
-
next:'.next'
-
});
-
});
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.
-
<div class="scrollable js">
-
<a href="#" class="more-link">More News</a>
-
<li id="itemswrapper">
-
<ol style="overflow: hidden; position: relative; visibility: visible; height: 124px;" id="items">
-
<div class="__scrollable" style="position: absolute; height: 200000em;">
-
</div>
-
<br clear="all"/>
-
</ol>
-
</li>
-
</ul>
-
</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.jshere

