]> git.sur5r.net Git - contagged/blob - scripts/interface/ifxscrollto.js
Version 0.6.5
[contagged] / scripts / interface / ifxscrollto.js
1 /**
2  * Interface Elements for jQuery
3  * FX - scroll to
4  * 
5  * http://interface.eyecon.ro
6  * 
7  * Copyright (c) 2006 Stefan Petre
8  * Dual licensed under the MIT (MIT-LICENSE.txt) 
9  * and GPL (GPL-LICENSE.txt) licenses.
10  *   
11  *
12  */
13 /**
14  * Applies a scrolling effect to document until the element gets into viewport
15  */
16 jQuery.fn.extend (
17         {
18                 /**
19                  * @name ScrollTo
20                  * @description scrolls the document until the lement gets into viewport
21                  * @param Mixed speed animation speed, integer for miliseconds, string ['slow' | 'normal' | 'fast']
22                  * @param String axis (optional) whatever to scroll on vertical, horizontal or both axis ['vertical'|'horizontal'|null]
23                  * @param String easing (optional) The name of the easing effect that you want to use.
24                  * @type jQuery
25                  * @cat Plugins/Interface
26                  * @author Stefan Petre
27                  */
28                 ScrollTo : function(speed, axis, easing) {
29                         o = jQuery.speed(speed);
30                         return this.queue('interfaceFX',function(){
31                                 new jQuery.fx.ScrollTo(this, o, axis, easing);
32                         });
33                 },
34                 /**
35                  * @name ScrollToAnchors
36                  * @description all links to '#elementId' will animate scroll
37                  * @param Mixed speed animation speed, integer for miliseconds, string ['slow' | 'normal' | 'fast']
38                  * @param String axis (optional) whatever to scroll on vertical, horizontal or both axis ['vertical'|'horizontal'|null]
39                  * @param String easing (optional) The name of the easing effect that you want to use.
40                  * @type jQuery
41                  * @cat Plugins/Interface
42                  * @author Stefan Petre
43                  */
44                 /*inspired by David Maciejewski www.macx.de*/
45                 ScrollToAnchors : function(speed, axis, easing) {
46                         return this.each(
47                                 function()
48                                 {
49                                         jQuery('a[@href*="#"]', this).click(
50                                                 function(e)
51                                                 {
52                                                         parts = this.href.split('#');
53                                                         jQuery('#' + parts[1]).ScrollTo(speed, axis, easing);
54                                                         return false;
55                                                 }
56                                         );
57                                 }
58                         )
59                 }
60         }
61 );
62
63 jQuery.fx.ScrollTo = function (e, o, axis, easing)
64 {
65         var z = this;
66         z.o = o;
67         z.e = e;
68         z.axis = /vertical|horizontal/.test(axis) ? axis : false;
69         z.easing = easing;
70         p = jQuery.iUtil.getPosition(e);
71         s = jQuery.iUtil.getScroll();
72         z.clear = function(){clearInterval(z.timer);z.timer=null;jQuery.dequeue(z.e, 'interfaceFX');};
73         z.t=(new Date).getTime();
74         s.h = s.h > s.ih ? (s.h - s.ih) : s.h;
75         s.w = s.w > s.iw ? (s.w - s.iw) : s.w;
76         z.endTop = p.y > s.h ? s.h : p.y;
77         z.endLeft = p.x > s.w ? s.w : p.x;
78         z.startTop = s.t;
79         z.startLeft = s.l;
80         z.step = function(){
81                 var t = (new Date).getTime();
82                 var n = t - z.t;
83                 var p = n / z.o.duration;
84                 if (t >= z.o.duration+z.t) {
85                         z.clear();
86                         setTimeout(function(){z.scroll(z.endTop, z.endLeft)},13);
87                 } else {
88                         if (!z.axis || z.axis == 'vertical') {
89                                 if (!jQuery.easing || !jQuery.easing[z.easing]) {
90                                         st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.endTop-z.startTop) + z.startTop;
91                                 } else {
92                                         st = jQuery.easing[z.easing](p, n, z.startTop, (z.endTop - z.startTop), z.o.duration);
93                                 }
94                         } else {
95                                 st = z.startTop;
96                         }
97                         if (!z.axis || z.axis == 'horizontal') {
98                                 if (!jQuery.easing || !jQuery.easing[z.easing]) {
99                                         sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.endLeft-z.startLeft) + z.startLeft;
100                                 } else {
101                                         sl = jQuery.easing[z.easing](p, n, z.startLeft, (z.endLeft - z.startLeft), z.o.duration);
102                                 }
103                         } else {
104                                 sl = z.startLeft;
105                         }
106                         z.scroll(st, sl);
107                 }
108         };
109         z.scroll = function (t, l){window.scrollTo(l, t);};
110         z.timer=setInterval(function(){z.step();},13);
111 };