- uploading 1.1 in tags
[mootools.git] / Plugins / SmoothScroll.js
blob4158ffb47e2a41c8d1648709d97f941e54d42971
1 /*
2 Script: SmoothScroll.js
3         Contains <SmoothScroll>
5 License:
6         MIT-style license.
7 */
9 /*
10 Class: SmoothScroll
11         Auto targets all the anchors in a page and display a smooth scrolling effect upon clicking them.
13 Note:
14         SmoothScroll requires an XHTML doctype.
16 Arguments:
17         options - the Fx.Scroll options (see: <Fx.Scroll>) plus links, a collection of elements you want your smoothscroll on. Defaults to document.links.
19 Example:
20         >new SmoothScroll();
23 var SmoothScroll = Fx.Scroll.extend({
25         initialize: function(options){
26                 this.parent(window, options);
27                 this.links = (this.options.links) ? $$(this.options.links) : $$(document.links);
28                 var location = window.location.href.match(/^[^#]*/)[0] + '#';
29                 this.links.each(function(link){
30                         if (link.href.indexOf(location) != 0) return;
31                         var anchor = link.href.substr(location.length);
32                         if (anchor && $(anchor)) this.useLink(link, anchor);
33                 }, this);
34                 if (!window.webkit419) this.addEvent('onComplete', function(){
35                         window.location.hash = this.anchor;
36                 });
37         },
39         useLink: function(link, anchor){
40                 link.addEvent('click', function(event){
41                         this.anchor = anchor;
42                         this.toElement(anchor);
43                         event.stop();
44                 }.bindWithEvent(this));
45         }
47 });