Tuesday, October 30, 2007

jQuery.ScrollShow

I've made a new plugin, that supercedes this one.
Please check jQuery.SerialScroll.
It's very small and offers high customizability, can be used for slideshows, site navigation, news tickers, etc. (These 3 exemplified in the demo).

jQuery.ScrollShow is a very customizable slideshow, that relies on jQuery.ScrollTo, to slide(scroll) the items. Documentation is included in the source file. The plugin is still in beta stage, but will be soon (hopefully) stable. Feedback and bug reports are very welcome.

Links

Monday, October 29, 2007

jQuery.ScrollTo 1.2 released

Changes

  • The option 'onafter' is now called 'onAfter'.
  • The option 'margin' can be setted to true, then the margin of the target element, will be taken into account and will be deducted.
  • Two axis can be scrolled together, this is setted with the option 'axis'.
  • In case 2 axis are chosen, the scrolling can be queued: one scrolls, and then the other.
  • There's an intermediary event, 'onAfterFirst' called in case the axis are queued, after the first ends.
  • If the option 'margin' is set to true, the plugin will take in account, the margin of the target(no use if target is a value).

Notes

  • If queuing is activated, the order of the axes ('xy' or 'yx') will tell the plugin, in what order to scroll the axis.
  • 'margin' will only be valid, if the target is a selector, a DOM element, or a jQuery Object.
  • If the first axis to be scrolled, is already positioned, that animation will be skipped, to avoid a delay in the animation.
Along with this release, jQuery.LocalScroll 1.0.1 is released. Changes:
  • 'onbefore' is now named 'onBefore', to keep jQuery.ScrollTo naming conventions.
  • A filter can be specified, so that only some of the suitable links are bound.

Links

Wednesday, October 24, 2007

jQuery.LocalScroll

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.

What does this plugin do ?

This plugin will animate a regular anchor navigation [1] [2].
It will allow your visitors to navigate your site with a smooth scrolling effect.

Each time a link is clicked, the element you decide(can be the whole screen), will gradually scroll to the targeted element, instead of "jumping" as it'd normally do.
jQuery.ScrollTo is used for the animation.

How to implement it ?

That's easy! you need to set links and anchors, that means, set an id to all those elements you want to scroll to.
These are the anchors.
Then add links with the respective id in the href, like this:

<a href="#the_id">your_text</a>
Let's call these "local links".
Lastly, you must call jQuery.LocalScroll on elements that contain our local links, you must wait for the document to be ready.

So if (for example) you have a div with id "navigation" and the local links are inside. You add this inside a document.ready:
$('#navigation').localScroll();
If your links are all spread, you can use:
$.localScroll();
That gets all the local links in the page. Both calls to the plugin accept one optional argument, that is, a hash specifying the settings described below. None is required.

Settings

  • target
    What to scroll (selector, DOMElement, or jQuery Object). If none is specified, the whole window will be scrolled.
  • filter
    String or function filter to ignore some of the links.
  • event
    On which event of the link to react (click by default)
  • lazy
    If true, more links can be added to the matched elements ( by AJAX, jQuery, etc ) and the plugin will also react to them.
  • stop
    If true (default), the plugin will stop any previous animations of the target, to avoid queuing.
  • lock
    If true, the plugin will ignore events if already animating (alternative to 'stop').
  • hash
    If true, the hash of the clicked link, will appear on the address bar once the animation ends.
    Note:This option isn't too cool when scrolling overflown elements, it's mostly recommended when scrolling the window.
  • onBefore
    A function to be called before each scrolling. It receives the following arguments: event object, targeted element and target to be scrolled. The 'this' will point to the set of settings.
  • reset
    Only for $.localScroll.hash(), if true (default) elements' scroll is reset before actual scrolling.
In addition to that, you can use jQuery.ScrollTo's settings!
Check its demo to see all of them.

The plugin also adds a function, $.localScroll.hash() , that checks the URL in the address bar, and if there's a hash(#an_id), it will scroll to the element. It accepts a hash of settings, just like $.localScroll. You will likely call it on document ready. Check the regular example to see it in action.

How do I use the settings ?

The most important setting is 'target', you might not want to navigate(scroll) the screen, but only an overflowed element(like the demo).
In that case, you specify the target, with a selector or the element itself.
$('#navigation').localScroll({
   target:'#content'
});
Instead of the window, an element with id 'content' will be scrolled.

Finally, a more complex call to show some customization in action:
Let's say you need to scroll on both axes to uncover all the anchors, then we'll need the option 'axis'.
You want to do an horizontal scroll, and then vertical, the code would be:
$('ul.sections').localScroll({
   target:'#content',
   axis:'xy',
   queue:true //one axis at a time
});
Note that 'axis' and 'queue' actually belong to jQuery.ScrollTo, not to jQuery.LocalScroll.

Dynamic content( AJAX, AHAH, or simply jQuery )

The plugin supports dynamically added/loaded anchors and local links. You need to set the option 'lazy' to true. Note that the matched elements (#navigation in the first example) must be preserved in the DOM. But any content inside it can be modified. You don't have this problem if you use the global call.

Troubleshooting

When using the browser's back button, the scroll isn't reverted

This is a known fact when scrolling overflown elements (as in not the window).

When clicking a link, it jumps directly to the target

That's the browser default behavior (no javascript). You either have a javascript error (check with Firebug) or something's wrong in the call to the plugin.

Strange things happen

Try removing the option 'hash' or set it to false. It can cause problems.

Check ScrollTo's troubleshooting.

Links

jQuery.ScrollTo 1.1 released

Changes

  • Relative animations are now supported.
  • Fixed a bug, the plugin would fail if the 'target' is a selector and it ends with a number.
  • The 'target' selector no longer supports 'em' or '%' as they won't work with animate anyway.
  • The plugin is no longer dependant on jQuery.Dimensions! :)
  • Fixed a bug, any-number + 'px' or relative animations, combined with no speed or speed 0 would fail. This no longer happens.

Links

Friday, October 19, 2007

jQuery.Bubble

This plugin adds bubbling functionality to jQuery. The code is similar to jQuery.fn.trigger's because it's meant to extend it. It also generates it's own event object, that will remain untouched through the bubbling, meaning it's safe to attach attributes to it and grab them with the ancestors while the event bubbles up. I tried to use jQuery's core functions as much as possible, but most functions I needed were not compatible, and would destroy the custom event object, still the code remains short. NOTES:

  • jQuery.Bubble 1.0 was tested with jQuery 1.3.1 and it's compatible with it's event system.
  • jQuery.Bubble 1.2 and further versions will stick to the new system(1.2 's).
  • There're two versions, the regular (async) and the synchronic version. I'm not sure which one works better, I suppose the asynchronic one resembles more to the browser's native (bubbling) behavior, so I'd use that one. Please let me know if you test them.
Links: Related Links:

Thursday, October 18, 2007

jQuery.ScrollTo

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

An article about animated scrolling with jQuery inspired me to make a small, customizable plugin for scrolling elements, or the window itself.

How to specify what to scroll ?

Simple, all the matched elements will be scrolled, for example:
$('div.pane').scrollTo(...);//all divs w/class pane
If you need to scroll the window (screen), then use:
$.scrollTo(...);//the plugin will take care of this

How to specify where ?

There are many different ways to specify the target position.
These are:
  • A raw number
  • A string('44', '100px', '+=30px', etc )
  • A DOM element (logically, child of the scrollable element)
  • A selector, that will be relative to the scrollable element
  • The string 'max' to scroll to the end.
  • A string specifying a percentage to scroll to that part of the container (f.e: 50% goes to to the middle).
  • A hash { top:x, left:y }, x and y can be any kind of number/string like above.
Note that you only need to use the hash form, if you are scrolling on both axes, and they have different positions.
Don't use the hash to scroll on both axes. Instead, keep reading :)

Settings

The plugin offers you many options to customize the animation and also the final position.
The settings are:
  • axis: Axes to be scrolled, 'x', 'y', 'xy' or 'yx'. 'xy' is the default.
  • duration: Length of the animation, none (sync) by default.
  • easing: Name of the easing equation.
  • margin: If true, target's border and margin are deducted.
  • offset: Number or hash {left: x, top:y }. This will be added to the final position(can be negative).
  • over: Add a fraction of the element's height/width (can also be negative).
  • queue: If true and both axes are scrolled, it will animate on one axis, and then on the other. Note that 'axis' being 'xy' or 'yx', concludes the order
  • onAfterFirst: If queing, a function to be called after scrolling the first axis.
  • onAfter: A function to be called after the whole animation ended.
  • You can use any other setting accepted by $().animate()
These settings are very well explained in the demo. Make sure to check it to understand them all.

Manually finding the scrolling limit

$.scrollTo always had an internal function that calculates the scrolling limit for both axes. Since 1.4.2, this function is exposed as $.scrollTo.max.

It's not too nice to use yet but it's probably not something you'll need, you must pass two arguments: a DOM element and an axis string ('x' or 'y') and it will return the max number.


Overloading

This plugin accepts the arguments in two ways, like $.animate().
$(...).scrollTo( target, duration, settings );
$(...).scrollTo( target, settings );
In this second case, you can specify the duration in the hash. You don't need to include any setting, not even the duration, everything has defaults.
The hash of defaults can be accessed at: $.scrollTo.defaults.

jQuery.scrollTo's sons

Indeed, jQuery.scrollTo has offspring :)
  • jQuery.localScroll: Will add a scroll effect, to any anchor navigation. Ideal for slides, table of contents, etc. It's small, and incredibly easy to implement.
  • jQuery.serialScroll: It's a multi-purpose plugin to animate any kind of sequential navigation(prev/next). It's also small and highly customizable.
These plugins require jQuery.scrollTo and can use its settings!.
That makes around 20 options to fully customize each of them.
They are wrappers for common situations where you might need this plugin.
Using them will save you a lot of time and will give you even more customization.
They can be safely used simultaneously and the 3 of them minified, take merely 3.5kb altogether!

Troubleshooting

Doesn't scroll on IE
Sometimes, you need to set a position (relative or absolute) to the container and give it fixed dimensions, in order to hide the overflow.
If this doesn't work, try giving the container fixed dimensions (height & width).

Links

Tuesday, October 16, 2007

jQuery.Listen

This plugin brings a clean, light solution, to websites with dynamic loaded content, or full of event bindings.
Intead of bound, handlers for events, are registered along with matching selectors. And they'll still work for new added content.
This is achieved, using Event delegation, so the plugin will only work for events that bubble.

The plugin supports these kind of selectors:

  • #id
  • tag
  • .cssClass
  • tag.cssClass
The plugin now supports comma-separated selectors
I'd like to add more support in the future, but perfomance and scalability are more important. These simple selectors give enough functionality for many cases. If, for example, you want ULs, to react to clicks on its LIs, then you can do this:
$('ul').listen('click','li',function(){
    alert('you clicked an item!!');
});
No matter how many times you add/remove items, they'll still be clickable. You can also do this:
$.listen('click','li',function(){
    alert('you clicked an item!!');
});
This time, the listener will be the document. So now you can add/remove ULs as well, clicking the items will still trigger the handler.

Links

Downloads

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

Update 2/20/08:
jQuery.Listen can now handle both focus and blur events thanks to the focusin/focusout workaround.
Many thanks to Jörn Zaefferer for lending me the code!
Update 3/7/08:
Added 1.0.3, it can handle comma-separated selectors, and there's a new experimental option. You can call:
  $.listen.cache(true);
Only after you finished with ALL the bindings, it will then start caching some things. Again, this is experimental.