1 | /* |
---|
2 | moo.fx, simple effects library built with prototype.js (http://prototype.conio.net). |
---|
3 | by Valerio Proietti (http://mad4milk.net) MIT-style LICENSE. |
---|
4 | for more info (http://moofx.mad4milk.net). |
---|
5 | 10/24/2005 |
---|
6 | v(1.0.2) |
---|
7 | */ |
---|
8 | var Class = { |
---|
9 | create: function() { |
---|
10 | return function() { |
---|
11 | this.initialize.apply(this, arguments); |
---|
12 | } |
---|
13 | } |
---|
14 | } |
---|
15 | |
---|
16 | Object.extend = function(destination, source) { |
---|
17 | for (property in source) { |
---|
18 | destination[property] = source[property]; |
---|
19 | } |
---|
20 | return destination; |
---|
21 | } |
---|
22 | |
---|
23 | Function.prototype.bind = function(object) { |
---|
24 | var __method = this; |
---|
25 | return function() { |
---|
26 | return __method.apply(object, arguments); |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | function $() { |
---|
31 | var elements = new Array(); |
---|
32 | |
---|
33 | for (var i = 0; i < arguments.length; i++) { |
---|
34 | var element = arguments[i]; |
---|
35 | if (typeof element == 'string') |
---|
36 | element = document.getElementById(element); |
---|
37 | |
---|
38 | if (arguments.length == 1) |
---|
39 | return element; |
---|
40 | |
---|
41 | elements.push(element); |
---|
42 | } |
---|
43 | |
---|
44 | return elements; |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | if (!window.Element) { |
---|
49 | var Element = new Object(); |
---|
50 | } |
---|
51 | |
---|
52 | Object.extend(Element, { |
---|
53 | remove: function(element) { |
---|
54 | element = $(element); |
---|
55 | element.parentNode.removeChild(element); |
---|
56 | }, |
---|
57 | |
---|
58 | hasClassName: function(element, className) { |
---|
59 | element = $(element); |
---|
60 | if (!element) |
---|
61 | return; |
---|
62 | var a = element.className.split(' '); |
---|
63 | for (var i = 0; i < a.length; i++) { |
---|
64 | if (a[i] == className) |
---|
65 | return true; |
---|
66 | } |
---|
67 | return false; |
---|
68 | }, |
---|
69 | |
---|
70 | addClassName: function(element, className) { |
---|
71 | element = $(element); |
---|
72 | Element.removeClassName(element, className); |
---|
73 | element.className += ' ' + className; |
---|
74 | }, |
---|
75 | |
---|
76 | removeClassName: function(element, className) { |
---|
77 | element = $(element); |
---|
78 | if (!element) |
---|
79 | return; |
---|
80 | var newClassName = ''; |
---|
81 | var a = element.className.split(' '); |
---|
82 | for (var i = 0; i < a.length; i++) { |
---|
83 | if (a[i] != className) { |
---|
84 | if (i > 0) |
---|
85 | newClassName += ' '; |
---|
86 | newClassName += a[i]; |
---|
87 | } |
---|
88 | } |
---|
89 | element.className = newClassName; |
---|
90 | }, |
---|
91 | |
---|
92 | // removes whitespace-only text node children |
---|
93 | cleanWhitespace: function(element) { |
---|
94 | element = $(element); |
---|
95 | for (var i = 0; i < element.childNodes.length; i++) { |
---|
96 | var node = element.childNodes[i]; |
---|
97 | if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) |
---|
98 | Element.remove(node); |
---|
99 | } |
---|
100 | } |
---|
101 | }); |
---|
102 | //base |
---|
103 | var fx = new Object(); |
---|
104 | fx.Base = function(){}; |
---|
105 | fx.Base.prototype = { |
---|
106 | setOptions: function(options) { |
---|
107 | this.options = { |
---|
108 | duration: 500, |
---|
109 | onComplete: '' |
---|
110 | } |
---|
111 | Object.extend(this.options, options || {}); |
---|
112 | }, |
---|
113 | |
---|
114 | go: function() { |
---|
115 | this.duration = this.options.duration; |
---|
116 | this.startTime = (new Date).getTime(); |
---|
117 | this.timer = setInterval (this.step.bind(this), 13); |
---|
118 | }, |
---|
119 | |
---|
120 | step: function() { |
---|
121 | var time = (new Date).getTime(); |
---|
122 | var Tpos = (time - this.startTime) / (this.duration); |
---|
123 | if (time >= this.duration+this.startTime) { |
---|
124 | this.now = this.to; |
---|
125 | clearInterval (this.timer); |
---|
126 | this.timer = null; |
---|
127 | if (this.options.onComplete) setTimeout(this.options.onComplete.bind(this), 10); |
---|
128 | } |
---|
129 | else { |
---|
130 | this.now = ((-Math.cos(Tpos*Math.PI)/2) + 0.5) * (this.to-this.from) + this.from; |
---|
131 | //this time-position, sinoidal transition thing is from script.aculo.us |
---|
132 | } |
---|
133 | this.increase(); |
---|
134 | }, |
---|
135 | |
---|
136 | custom: function(from, to) { |
---|
137 | if (this.timer != null) return; |
---|
138 | this.from = from; |
---|
139 | this.to = to; |
---|
140 | this.go(); |
---|
141 | }, |
---|
142 | |
---|
143 | hide: function() { |
---|
144 | this.now = 0; |
---|
145 | this.increase(); |
---|
146 | }, |
---|
147 | |
---|
148 | clearTimer: function() { |
---|
149 | clearInterval(this.timer); |
---|
150 | this.timer = null; |
---|
151 | } |
---|
152 | } |
---|
153 | //fader |
---|
154 | fx.Opacity = Class.create(); |
---|
155 | fx.Opacity.prototype = Object.extend(new fx.Base(), { |
---|
156 | initialize: function(el, options) { |
---|
157 | this.el = $(el); |
---|
158 | this.now = 1; |
---|
159 | this.increase(); |
---|
160 | this.setOptions(options); |
---|
161 | }, |
---|
162 | |
---|
163 | increase: function() { |
---|
164 | if (this.now == 1) this.now = 0.9999; |
---|
165 | if (this.now > 0 && this.el.style.visibility == "hidden") this.el.style.visibility = "visible"; |
---|
166 | if (this.now == 0) this.el.style.visibility = "hidden"; |
---|
167 | if (window.ActiveXObject) this.el.style.filter = "alpha(opacity=" + this.now*100 + ")"; |
---|
168 | this.el.style.opacity = this.now; |
---|
169 | }, |
---|
170 | |
---|
171 | toggle: function() { |
---|
172 | if (this.now > 0) this.custom(1, 0); |
---|
173 | else this.custom(0, 1); |
---|
174 | } |
---|
175 | }); |
---|