1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/**
* A class that implements marquee functionality for HTML elements
* Original source: https://shi.foo/static/js/libs/marquee.js
* Credit must be given if this code is used in any way
*/
class Marquee {
/** @readonly */
static DIRECTION = {
LEFT: 'left',
RIGHT: 'right',
UP: 'up',
DOWN: 'down'
};
/** @readonly */
static BEHAVIOR = {
SCROLL: 'scroll',
SLIDE: 'slide',
ALTERNATE: 'alternate'
};
/** @readonly */
static DEFAULTS = {
behavior: 'scroll',
bgcolor: null,
direction: 'left',
height: null,
width: null,
hspace: 0,
vspace: 0,
loop: -1,
scrollamount: 6,
scrolldelay: 85,
truespeed: false
};
/**
* Creates a new Marquee instance
* @param {HTMLElement} element - The element to transform into a marquee
* @param {Object} [options] - Configuration options
* @param {string} [options.behavior] - Scroll behavior (scroll, slide, alternate)
* @param {string} [options.bgcolor] - Background color
* @param {string} [options.direction] - Scroll direction (left, right, up, down)
* @param {string|number} [options.height] - Container height
* @param {string|number} [options.width] - Container width
* @param {number} [options.hspace] - Horizontal margin
* @param {number} [options.vspace] - Vertical margin
* @param {number} [options.loop] - Number of loops (-1 for infinite)
* @param {number} [options.scrollamount] - Pixels to move per step
* @param {number} [options.scrolldelay] - Delay between steps in ms
* @param {boolean} [options.truespeed] - Whether to respect exact scroll delay
* @throws {Error} If invalid element is provided
*/
constructor(element, options = {}) {
if (!(element instanceof HTMLElement)) {
throw new Error('Invalid element provided to Marquee');
}
this.element = element;
const attributeOptions = {
behavior: element.getAttribute('behavior'),
bgcolor: element.getAttribute('bgcolor'),
direction: element.getAttribute('direction'),
height: element.getAttribute('height'),
width: element.getAttribute('width'),
hspace: element.getAttribute('hspace'),
vspace: element.getAttribute('vspace'),
loop: element.getAttribute('loop'),
scrollamount: element.getAttribute('scrollamount'),
scrolldelay: element.getAttribute('scrolldelay'),
truespeed: element.hasAttribute('truespeed')
};
this.config = {
...Marquee.DEFAULTS,
...Object.fromEntries(Object.entries(attributeOptions).filter(([_, v]) => v != null)),
...this._normalizeOptions(options)
};
this._rafId = null;
this._currentPos = 0;
this._lastTime = Date.now();
this._isPaused = false;
this._loopCount = 0;
this._originalContent = '';
this._setupContainer();
this._setupContent();
this._setupDimensions();
this._setupEventListeners();
this._bindInlineHandlers();
this.start();
}
/**
* Normalizes numeric options by parsing them to integers
* @private
* @param {Object} options - Options to normalize
* @returns {Object} Normalized options
*/
_normalizeOptions(options) {
return {
...options,
scrollamount: parseInt(options.scrollamount) || Marquee.DEFAULTS.scrollamount,
scrolldelay: parseInt(options.scrolldelay) || Marquee.DEFAULTS.scrolldelay,
loop: parseInt(options.loop) || Marquee.DEFAULTS.loop,
hspace: parseInt(options.hspace) || Marquee.DEFAULTS.hspace,
vspace: parseInt(options.vspace) || Marquee.DEFAULTS.vspace
};
}
/**
* Sets up the container element
* @private
*/
_setupContainer() {
this.container = document.createElement('div');
this.container.style.overflow = 'hidden';
this.container.style.position = 'relative';
this.container.style.width = this.config.width || '100%';
this.container.style.height = this.config.height || 'auto';
if (this.config.bgcolor) {
this.container.style.backgroundColor = this.config.bgcolor;
}
this.container.style.margin = `${this.config.vspace}px ${this.config.hspace}px`;
this._originalContent = this.element.innerHTML;
this.element.innerHTML = '';
this.element.appendChild(this.container);
const eventHandlers = this.element.attributes;
for (let i = 0; i < eventHandlers.length; i++) {
const attr = eventHandlers[i];
if (attr.name.startsWith('on')) {
this.container[attr.name] = this.element[attr.name];
}
}
}
/**
* Sets up the content wrapper
* @private
*/
_setupContent() {
this.contentWrapper = document.createElement('div');
this.contentWrapper.style.position = 'absolute';
this.contentWrapper.style.width = '100%';
this.contentWrapper.innerHTML = this._originalContent.trim();
if (this.config.direction === 'up' || this.config.direction === 'down') {
this.contentWrapper.style.display = 'block';
} else {
this.contentWrapper.style.whiteSpace = 'nowrap';
}
if (this.config.behavior !== Marquee.BEHAVIOR.ALTERNATE) {
const children = Array.from(this.contentWrapper.children);
children.forEach(child => {
const clone = child.cloneNode(true);
this.contentWrapper.appendChild(clone);
});
}
this.container.appendChild(this.contentWrapper);
}
/**
* Sets up initial dimensions and positions
* @private
*/
_setupDimensions() {
requestAnimationFrame(() => {
switch (this.config.direction) {
case Marquee.DIRECTION.LEFT:
this._currentPos = this.container.offsetWidth;
break;
case Marquee.DIRECTION.RIGHT:
this._currentPos = -this.contentWrapper.offsetWidth;
break;
case Marquee.DIRECTION.UP:
this._currentPos = this.container.offsetHeight;
break;
case Marquee.DIRECTION.DOWN:
this._currentPos = -this.contentWrapper.offsetHeight;
break;
}
this._applyPosition();
});
}
/**
* Sets up event listeners for hover and visibility
* @private
*/
_setupEventListeners() {
if (!this.config.truespeed) {
this.container.addEventListener('mouseenter', () => this.pause());
this.container.addEventListener('mouseleave', () => this.start());
}
this._visibilityHandler = () => {
if (document.hidden) {
this.pause();
} else if (!this._isPaused) {
this.start();
}
};
document.addEventListener('visibilitychange', this._visibilityHandler);
}
/**
* Sets up inline event handlers
* @private
*/
_bindInlineHandlers() {
if (this.element.hasAttribute('onmouseover')) {
const originalStop = this.element.getAttribute('onmouseover');
this.element.removeAttribute('onmouseover');
this.element.addEventListener('mouseover', () => this.pause());
}
if (this.element.hasAttribute('onmouseout')) {
const originalStart = this.element.getAttribute('onmouseout');
this.element.removeAttribute('onmouseout');
this.element.addEventListener('mouseout', () => this.start());
}
}
/**
* Applies the current position to the content wrapper
* @private
*/
_applyPosition() {
const isVertical = this.config.direction === 'up' || this.config.direction === 'down';
const transform = isVertical ?
`translateY(${this._currentPos}px)` :
`translateX(${this._currentPos}px)`;
this.contentWrapper.style.transform = transform;
}
/**
* Animation frame handler
* @private
*/
_animate() {
const currentTime = Date.now();
const deltaTime = currentTime - this._lastTime;
if (deltaTime >= this.config.scrolldelay) {
this._lastTime = currentTime;
const pixelsToMove = this.config.scrollamount;
switch (this.config.direction) {
case Marquee.DIRECTION.LEFT:
this._currentPos -= pixelsToMove;
if (this._currentPos <= -this.contentWrapper.offsetWidth / 2) {
this._currentPos = this.container.offsetWidth;
this._handleLoop();
}
break;
case Marquee.DIRECTION.RIGHT:
this._currentPos += pixelsToMove;
if (this._currentPos >= this.container.offsetWidth) {
this._currentPos = -this.contentWrapper.offsetWidth / 2;
this._handleLoop();
}
break;
case Marquee.DIRECTION.UP:
this._currentPos -= pixelsToMove;
if (this._currentPos <= -this.contentWrapper.offsetHeight / 2) {
this._currentPos = this.container.offsetHeight;
this._handleLoop();
}
break;
case Marquee.DIRECTION.DOWN:
this._currentPos += pixelsToMove;
if (this._currentPos >= this.container.offsetHeight) {
this._currentPos = -this.contentWrapper.offsetHeight / 2;
this._handleLoop();
}
break;
}
this._applyPosition();
}
if (!this._isPaused) {
this._rafId = requestAnimationFrame(() => this._animate());
}
}
/**
* Handles loop counting and stopping
* @private
*/
_handleLoop() {
if (this.config.loop !== -1) {
this._loopCount++;
if (this._loopCount >= this.config.loop) {
this.stop();
}
}
}
/**
* Starts or resumes the marquee animation
* @public
*/
start() {
this._isPaused = false;
this._lastTime = Date.now();
if (!this._rafId) {
this._animate();
}
}
/**
* Pauses the marquee animation
* @public
*/
pause() {
this._isPaused = true;
if (this._rafId) {
cancelAnimationFrame(this._rafId);
this._rafId = null;
}
}
/**
* Stops the marquee animation and resets position
* @public
*/
stop() {
this.pause();
this._currentPos = 0;
this._loopCount = 0;
this._applyPosition();
}
/**
* Cleans up the marquee and restores original content
* @public
*/
destroy() {
this.stop();
this.container.remove();
this.element.innerHTML = this._originalContent;
document.removeEventListener('visibilitychange', this._visibilityHandler);
}
}
|