Showing posts with label jQuery.LocalScroll. Show all posts
Showing posts with label jQuery.LocalScroll. Show all posts

Wednesday, March 11, 2009

jQuery.LocalScroll 1.2.7 released

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.

In this release I cleaned up the code a little and added some features. Also removed a few old (aka deprecated) stuff that were still hanging around.

Enhancements

  • Added some misc enhancements and cleanup the code.
  • Updated the plugin to take advantage of recent scrollTo additions.

Changes

  • The element that triggered the scroll cannot be accessed anymore from within the onBefore, you can bind your own click (or w/e) to them in order to add a class or things like that.
  • settings.persistent is no longer supported (was deprecated).

Features

  • The set of settings can be accessed from within the onBefore as the 'this'.
  • The hash (#foo) is set to the URL before scrolling, so the back button works accurately (when scrolling the window).
  • The option 'hash' doesn't make the window jump when scrolling overflown elements
  • $.localScroll.hash now resets the element scroll to (0,0) by default. You can set the setting 'reset' to false to avoid this.
Enjoy!

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, November 17, 2007

jQuery.LocalScroll 1.1.2 released

Changes

  • The defaults have been made public for modification.
  • The anchor filter has been optimized
  • A bug was fixed, clicking an anchor with href=# and then a special link, wasn't scrolling.
  • A trigger event can be specified, if click is not the desired one.
  • If 'cancel' is set to false, the default behaviour of the anchor will occur.

New option 'lazy'

jQuery.LocalScroll counts with a new, and very useful option. If lazy is set to true, then the plugin will react to new links, that are appended to the originally matched elements. jQuery.ScrollTo had some minor releases and is now at 1.2.4. This is not the version included in this release of jQuery.LocalScroll, but they are perfectly compatible. Update:jQuery.LocalScroll 1.1.3 was released, the upgrade is recommended, upgrading to jQuery.ScrollTo 1.3 is also adviced (included in the last release).

Links

Downloads

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

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

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