Wednesday, February 20, 2008

Rotator

Introduction

This small Javascript class, serves as a generic rotator for different kind of collections.

Arguments

The Rotator class constructor expects 3 arguments, the last 2 are optional.
  • collection
    Can be a string, an array or an array-like object( arguments, nodelist, numeric hash ).
    If it's a string, it will get split by character.
  • setter
    A function that receives a value, an element of the collection and it's index.
    It must, either set the value internally or return the new value.
    The scope (this) will be the element, the arguments are the value and then the index.
    If it's a string, it means you want to set the value to the attribute of the element with that name.
    If none is given, then Rotator.defaultSetter is used, which, by default, returns the new value.
  • getter:
    A function that receives an element of the collection and the index.
    It must return the desired information from them.
    The scope (this) will be the element and the first argument, the index.
    If it's a string, it means you want the attribute of the element with that name.
    If none is given, then Rotator.defaultGetter is assigned, which, by default, returns the element.

How to use

Once you create an instance (obj) of the class, you can get/set the collection using the method obj.collection().
To rotate the elements, call obj.left() or obj.right().
With each call, the elements will be rotated one position in the direction you specified, and the functions will be called.
Note that the collection held by the object won't be the same that you gave it. Yours remains untouched.

Check the demo to see it working. If you view the source, you'll see 3 different instances.

Links

Downloads

jQuery.Listen 1.0 released

I updated jQuery.Listen and it got to it's first stable version. This release includes mostly features. A few optimizations and a couple of structural changes.
As specified in the change list, the licensing changed from GPL to GPL+MIT.

Optimizations

  • Reduced the code size, this release (with all its features) is smaller than the previous one.
  • Added many optimizations for minified code size.
  • Improved the cleaning done on window.onload to avoid memory leaks.

Fixes

  • The [].splice.call(arguments,..) wasn't working in my Opera.

Changes

  • $.listen is not used for stopping/restarting the indexers.
    use $(..).indexer(...).(start|stop)() instead.
  • Changed the licensing from GPL to GPL+MIT.

Features

  • Added Jörn Zaefferer's approach of focusin/focusout. You can now safely listen for blur and focus events.
  • Added $(..).indexer( event_name ) to get access to the indexers of the first matched element and $.indexer(..) to the global indexer.
  • $.listen.fixes is a hash that maps event/fixedEvent. If you add a fix at $.event.special, add it to this hash and it will work automatically.
  • It's possible to instruct indexers to emulate the natural bubbling like this: $(..).indexer( ... ).bubbles = true, use with discretion, will hit on perfomance.
  • Added support for multiple events separated with spaces.
Kudos to Jörn Zaefferer for lending me the code that now allows listening for focus and blur!

Links

Downloads

I really advice using the minified versions. The code is optimized to those releases. Source versions should only serve to learn.

Monday, February 11, 2008

jQuery.SerialScroll

Notice

I've pretty much stopped updating this blog, but the plugin development is still on-going. You can find the link to the Github project page at the bottom of the article.

Introduction

This plugin allows you to easily animate any series of elements, by sequentially scrolling them. It uses jQuery.ScrollTo to achieve the scrolling animation. It is a very unrestricted plugin, that lets you customize pretty much everything from outside. You can use horizontal or vertical scroll, also combined.

What's it for ?

jQuery.SerialScroll doesn't have one definite purpose. It's generic and adaptable. You can certainly use it as a screen slider. That is, to sequentially navigate a group of screens.
This plugin can also animate a text scroller in no time.
It can definitely handle slideshows, the high customizability of the scrolling effect lets you create beautiful animations.
You can even build an automatic news ticker!
Three of these uses are exemplified in the demo.
Remember, it's not restricted to these situations. It will take care of any collection of html elements that you want to scroll consecutively.

Settings and customization

jQuery.SerialScroll gives you access to a lot of options.
These are:
  • target
    The element to scroll, it's relative to the matched element.
    If you don't specify this option, the scrolled element is the one you called serialScroll on.
  • event
    on which event to react (click by default).
  • start
    first element of the series (zero-based index, 0 by default).
  • step
    how many elements to scroll each time. Use a negative number to go on the other way.
  • lock
    if true(default), the plugin will ignore events if already animating. Then animations can't be queued.
  • cycle
    if true, the first element will be shown after going over the last, and the other way around.
  • stop
    if true, the plugin will stop any previous animations of the element, to avoid queuing.
  • force
    if true, an initial scroll will be forced on start.
  • jump
    if true, the specified event can be triggered on the items, and the container will scroll to them.
  • items
    selector to the items(relative to the scrolled element).
  • prev
    (optional)selector to the 'previous' button.
  • next
    (optional)selector to the 'next' button.
  • lazy
    if true, the items are collected each time, allowing dynamic content(AJAX, AHAH, jQuery manipulation, etc).
  • interval
    If you specify a number, the plugin will add auto scrolling with that interval.
  • constant
    Should the speed remain constant, no matter how many items we scroll at once ? (true by default).
  • navigation
    Optionally, a selector to a group of elements, that allow scrolling to specific elements by index. Can be less than the amount of items.
  • excludenew
    If you want the plugin, to stop scrolling before the actual last element, set this to a number, and that amount of items is ignored counting from the end.
    This is useful if you show many items simultaneously, in that case, you probably want to set this option to the amount of visible items - 1.
  • onBefore
    A function to be called before each scrolling. It receives the following arguments: event object, targeted element, element to be scrolled, collection of items and position of the targeted element in the collection.
    The scope(this) will point to the element that got the event. If the function returns false, the event is ignored.
Also, you can use jQuery.ScrollTo's settings!
Check its demo to see all of them.

The option 'target'
This option is a new addition, included since 1.2.0.
Before, you needed to call the plugin once for each scrolled element.
When this option is specified, the matched elements are no longer the scrolled elements, but a container.
In this case, the selectors of prev, next, navigation and target will be relative to this container, allowing you to call SerialScroll on many elements at once.

External manipulation, event triggering

jQuery.SerialScroll automatically binds 3 events to the containers.
These are:
  • prev.serialScroll
    Scrolls to the previous element.
  • next.serialScroll
    Scrolls to the next element.
  • goto.serialScroll
    Scrolls to the specified index, starts with 0.
  • start.serialScroll
    (Re)starts autoscrolling.
  • stop.serialScroll
    Stops the autoscrolling.
  • notify.serialScroll
    Updates the active item.
This looks confusing, but it's not. You use it like this:
$(container).trigger( 'prev' );
$(container).trigger( 'next' );
$(container).trigger( 'goto', [ 3 ] );
$(container).trigger( 'start' );
$(container).trigger( 'stop' );
$(container).trigger( 'notify', [ 4 ] );
'notify' also accepts a DOM element(item), or any of its descendants.
$(container) is the element that gets scrolled each time. If you specified a 'target', then that element, else, the element you called the plugin on.
Note that to use 'start' and 'stop' you need to use the option 'interval' first.
If your container element already has any of these event names bound(odd!), then just add the namespace when you trigger.
You probably won't need to deal with these events, but if so, this is how.

What makes jQuery.SerialScroll so special ?

This plugin has many positive features, of course, it won't fit everyone's needs. That's impossible.
  • Small Footprint
    This plugin is tiny, as said before, it requires jQuery.ScrollTo. Both plugins together, take less than 3.5kb minified.
    If by chance, you decide to include jQuery.LocalScroll, the 3 of them require less than 5kb. Including this plugin is not a bad idea, it can be used, instead of the option 'navigation' to build a widget with sequential and random scrolling.
  • Highly customizable
    This plugin has many settings to customize, in addition, it can use jQuery.ScrollTo's settings. That makes 27 different options!
    If you take a while to analyze them all, you can make your work really unique.
  • Accessible, degrades gracefully
    Probably many will automatically skip this part, shame on you!
    If you make sure non-javascript users will see the scrollbars, then they can perfectly navigate your content. You can show the scrollbars only for these few users, easily, using css/js.
    This is one of the main differences with many similar scripts, they generate the content and the styling using javascript.
  • Adaptable
    jQuery.SerialScroll won't alter the html or styles at all.
    You are in control of the styles and content of your collections. You don't need the plugin to decide what html to use, or how many items to show simultaneously, and you can safely change that yourself, the plugin will always work.
    The items don't need to have fixed size, nor to be aligned. SerialScroll will scroll from one to the other, no matter what.
    If you want a plugin with premade styles or automatic generation of html, then you should consider any of jQuery carousels.
  • Generic and reusable
    Finally, as mentioned before, this plugin can be used for many different situations and doesn't have one specific application.

Links

Friday, February 8, 2008

jQuery.LocalScroll 1.2 released

A new major update of jQuery.LocalScroll has seen the light.
Two minor releases were added after it and is now at 1.2.2. I'll detail them all together:

Optimizations

  • Replaced a $('[name='+name+']') for a document.getElementsByName(name) to critically improve perfomance.
  • Small improvements to make the code shorter.

Fixes

  • The last argument received by onBefore when scrolling the window, is no more $(window) but the real element being scrolled.

Changes

  • Renamed the option 'persistent' to 'lazy', the latter seemed more adequate. Using 'persistent' will still work (backwards compatibilty)

Features

  • Added the option 'stop', if true (default), each event will stop any previous animations of the target.
  • Added the option 'lock', if true, the plugin will ignore events if already animating.
  • Added $.localScroll.hash( settings ); which will scroll to the given element if the URL contains a valid hash.
  • Removed the option 'cancel' that wasn't working well, and added the option 'hash'. It does what 'cancel' was meant to do, but in a different way.
    After a scroll, the hash( #some_id ) of the link is added to the URL.
    Note:This setting is not compatible with options like offset and margin, as the browser will natively scroll in the end.
    If you use the option 'target'(to scroll an overflowed element) and the window has overflow, setting the hash will scroll the window as well. So my advice is:
    only use 'hash' when scrolling the window.
jQuery.ScrollTo is now at 1.3.2, it has a new option called 'over', check its demo to see it in action.
jQuery.LocalScroll 1.2.x requires jQuery.ScrollTo 1.3.1 or higher.

Links

Downloads

I really advice using the minified versions. The code is optimized for those releases. Source versions should only be used to learn.

Saturday, February 2, 2008

Feedback

I thought I'd create a post where readers could easily add general opinions and/or suggestions. The scope of this post is the entire blog, so it can be about any jQuery plugin, Maxthon's console, or the script to add Static methods to Array. General input about the blog is welcomed too.

Also I'd like to know, if someone's using any of these scripts on a site. So if you do, please let me know by posting a comment.

I also plan to make a list of sites using these scripts. So if you are interested, add the url and I'll link to your site.