Mercurial > hg > vamp-website
comparison forum/Themes/default/scripts/fader.js @ 76:e3e11437ecea website
Add forum code
author | Chris Cannam |
---|---|
date | Sun, 07 Jul 2013 11:25:48 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
75:72f59aa7e503 | 76:e3e11437ecea |
---|---|
1 function smf_NewsFader(oOptions) | |
2 { | |
3 this.opt = oOptions; | |
4 | |
5 this.oFaderHandle = document.getElementById(this.opt.sFaderControlId); | |
6 | |
7 // Fade from... what text color? Default to black. | |
8 this.oFadeFrom = 'oFadeFrom' in this.opt ? this.opt.oFadeFrom : { | |
9 r: 0, | |
10 g: 0, | |
11 b: 0 | |
12 }; | |
13 | |
14 // To which background color? Default to white. | |
15 this.oFadeTo = 'oFadeTo' in this.opt ? this.opt.oFadeTo : { | |
16 r: 255, | |
17 g: 255, | |
18 b: 255 | |
19 }; | |
20 | |
21 // Surround each item with... anything special? | |
22 this.sItemTemplate = 'sItemTemplate' in this.opt ? this.opt.sItemTemplate : '%1$s'; | |
23 | |
24 // Fade delay (in milliseconds). | |
25 this.iFadeDelay = 'iFadeDelay' in this.opt ? this.opt.iFadeDelay : 5000; | |
26 | |
27 // The array that contains all the lines of the news for display. | |
28 this.aFaderItems = 'aFaderItems' in this.opt ? this.opt.aFaderItems : []; | |
29 | |
30 // Should we look for fader data, still? | |
31 this.bReceivedItemsOnConstruction = 'aFaderItems' in this.opt; | |
32 | |
33 // The current item in smfFadeContent. | |
34 this.iFadeIndex = -1; | |
35 | |
36 // Percent of fade (-64 to 510). | |
37 this.iFadePercent = 510 | |
38 | |
39 // Direction (in or out). | |
40 this.bFadeSwitch = false; | |
41 | |
42 // Just make sure the page is loaded before calling the init. | |
43 setTimeout(this.opt.sSelf + '.init();', 1); | |
44 } | |
45 | |
46 smf_NewsFader.prototype.init = function init() | |
47 { | |
48 var oForeEl, oForeColor, oBackEl, oBackColor; | |
49 | |
50 // Try to find the fore- and background colors. | |
51 if ('currentStyle' in this.oFaderHandle) | |
52 { | |
53 oForeColor = this.oFaderHandle.currentStyle.color.match(/#([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/); | |
54 this.oFadeFrom = { | |
55 r: parseInt(oForeColor[1]), | |
56 g: parseInt(oForeColor[2]), | |
57 b: parseInt(oForeColor[3]) | |
58 }; | |
59 | |
60 oBackEl = this.oFaderHandle; | |
61 while (oBackEl.currentStyle.backgroundColor == 'transparent' && 'parentNode' in oBackEl) | |
62 oBackEl = oBackEl.parentNode; | |
63 | |
64 oBackColor = oBackEl.currentStyle.backgroundColor.match(/#([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/); | |
65 this.oFadeTo = { | |
66 r: eval('0x' + oBackColor[1]), | |
67 g: eval('0x' + oBackColor[2]), | |
68 b: eval('0x' + oBackColor[3]) | |
69 }; | |
70 } | |
71 else if (!('opera' in window) && 'defaultView' in document) | |
72 { | |
73 oForeEl = this.oFaderHandle; | |
74 while (document.defaultView.getComputedStyle(oForeEl, null).getPropertyCSSValue('color') == null && 'parentNode' in oForeEl && 'tagName' in oForeEl.parentNode) | |
75 oForeEl = oForeEl.parentNode; | |
76 | |
77 oForeColor = document.defaultView.getComputedStyle(oForeEl, null).getPropertyValue('color').match(/rgb\((\d+), (\d+), (\d+)\)/); | |
78 this.oFadeFrom = { | |
79 r: parseInt(oForeColor[1]), | |
80 g: parseInt(oForeColor[2]), | |
81 b: parseInt(oForeColor[3]) | |
82 }; | |
83 | |
84 oBackEl = this.oFaderHandle; | |
85 while (document.defaultView.getComputedStyle(oBackEl, null).getPropertyCSSValue('background-color') == null && 'parentNode' in oBackEl && 'tagName' in oBackEl.parentNode) | |
86 oBackEl = oBackEl.parentNode; | |
87 | |
88 oBackColor = document.defaultView.getComputedStyle(oBackEl, null).getPropertyValue('background-color'); | |
89 this.oFadeTo = { | |
90 r: parseInt(oBackColor[1]), | |
91 g: parseInt(oBackColor[2]), | |
92 b: parseInt(oBackColor[3]) | |
93 }; | |
94 } | |
95 | |
96 // Did we get our fader items on construction, or should we be gathering them instead? | |
97 if (!this.bReceivedItemsOnConstruction) | |
98 { | |
99 // Get the news from the list in boardindex | |
100 var oNewsItems = this.oFaderHandle.getElementsByTagName('li'); | |
101 | |
102 // Fill the array that has previously been created | |
103 for (var i = 0, n = oNewsItems.length; i < n; i ++) | |
104 this.aFaderItems[i] = oNewsItems[i].innerHTML; | |
105 } | |
106 | |
107 // The ranges to fade from for R, G, and B. (how far apart they are.) | |
108 this.oFadeRange = { | |
109 'r': this.oFadeFrom.r - this.oFadeTo.r, | |
110 'g': this.oFadeFrom.g - this.oFadeTo.g, | |
111 'b': this.oFadeFrom.b - this.oFadeTo.b | |
112 }; | |
113 | |
114 // Divide by 20 because we are doing it 20 times per one ms. | |
115 this.iFadeDelay /= 20; | |
116 | |
117 // Start the fader! | |
118 window.setTimeout(this.opt.sSelf + '.fade();', 20); | |
119 } | |
120 | |
121 // Main fading function... called 50 times every second. | |
122 smf_NewsFader.prototype.fade = function fade() | |
123 { | |
124 if (this.aFaderItems.length <= 1) | |
125 return; | |
126 | |
127 // A fix for Internet Explorer 4: wait until the document is loaded so we can use setInnerHTML(). | |
128 if ('readyState' in document && document.readyState != 'complete') | |
129 { | |
130 window.setTimeout(this.opt.sSelf + '.fade();', 20); | |
131 return; | |
132 } | |
133 | |
134 // Starting out? Set up the first item. | |
135 if (this.iFadeIndex == -1) | |
136 { | |
137 setInnerHTML(this.oFaderHandle, this.sItemTemplate.replace('%1$s', this.aFaderItems[0])); | |
138 this.iFadeIndex = 1; | |
139 | |
140 // In Mozilla, text jumps around from this when 1 or 0.5, etc... | |
141 if ('MozOpacity' in this.oFaderHandle.style) | |
142 this.oFaderHandle.style.MozOpacity = '0.90'; | |
143 else if ('opacity' in this.oFaderHandle.style) | |
144 this.oFaderHandle.style.opacity = '0.90'; | |
145 // In Internet Explorer, we have to define this to use it. | |
146 else if ('filter' in this.oFaderHandle.style) | |
147 this.oFaderHandle.style.filter = 'alpha(opacity=100)'; | |
148 } | |
149 | |
150 // Are we already done fading in? If so, fade out. | |
151 if (this.iFadePercent >= 510) | |
152 this.bFadeSwitch = !this.bFadeSwitch; | |
153 | |
154 // All the way faded out? | |
155 else if (this.iFadePercent <= -64) | |
156 { | |
157 this.bFadeSwitch = !this.bFadeSwitch; | |
158 | |
159 // Go to the next item, or first if we're out of items. | |
160 setInnerHTML(this.oFaderHandle, this.sItemTemplate.replace('%1$s', this.aFaderItems[this.iFadeIndex ++])); | |
161 if (this.iFadeIndex >= this.aFaderItems.length) | |
162 this.iFadeIndex = 0; | |
163 } | |
164 | |
165 // Increment or decrement the fade percentage. | |
166 if (this.bFadeSwitch) | |
167 this.iFadePercent -= 255 / this.iFadeDelay * 2; | |
168 else | |
169 this.iFadePercent += 255 / this.iFadeDelay * 2; | |
170 | |
171 // If it's not outside 0 and 256... (otherwise it's just delay time.) | |
172 if (this.iFadePercent < 256 && this.iFadePercent > 0) | |
173 { | |
174 // Easier... also faster... | |
175 var tempPercent = this.iFadePercent / 255, rounded; | |
176 | |
177 if ('MozOpacity' in this.oFaderHandle.style) | |
178 { | |
179 rounded = Math.round(tempPercent * 100) / 100; | |
180 this.oFaderHandle.style.MozOpacity = rounded == 1 ? '0.99' : rounded; | |
181 } | |
182 else if ('opacity' in this.oFaderHandle.style) | |
183 { | |
184 rounded = Math.round(tempPercent * 100) / 100; | |
185 this.oFaderHandle.style.opacity = rounded == 1 ? '0.99' : rounded; | |
186 } | |
187 else | |
188 { | |
189 var done = false; | |
190 if ('alpha' in this.oFaderHandle.filters) | |
191 { | |
192 try | |
193 { | |
194 this.oFaderHandle.filters.alpha.opacity = Math.round(tempPercent * 100); | |
195 done = true; | |
196 } | |
197 catch (err) | |
198 { | |
199 } | |
200 } | |
201 | |
202 if (!done) | |
203 { | |
204 // Get the new R, G, and B. (it should be bottom + (range of color * percent)...) | |
205 var r = Math.ceil(this.oFadeTo.r + this.oFadeRange.r * tempPercent); | |
206 var g = Math.ceil(this.oFadeTo.g + this.oFadeRange.g * tempPercent); | |
207 var b = Math.ceil(this.oFadeTo.b + this.oFadeRange.b * tempPercent); | |
208 | |
209 // Set the color in the style, thereby fading it. | |
210 this.oFaderHandle.style.color = 'rgb(' + r + ', ' + g + ', ' + b + ')'; | |
211 } | |
212 } | |
213 } | |
214 | |
215 // Keep going. | |
216 window.setTimeout(this.opt.sSelf + '.fade();', 20); | |
217 } |