|
1 |
| -/* |
2 |
| - Vanilla Javascript Marquee |
3 |
| - Version: 0.2.1 |
4 |
| - Author: Robert Bossaert <https://github.com/robertbossaert> |
5 |
| - Example call: |
6 |
| -
|
7 |
| - new Marquee('element'); |
8 |
| -
|
9 |
| - new Marquee('element', { |
10 |
| - direction: 'rtl', |
11 |
| - }); |
12 |
| -*/ |
13 |
| -var Marquee = function (element, defaults) { |
14 |
| - var elem = document.getElementById(element), |
15 |
| - options = (defaults === undefined) ? {} : defaults, |
16 |
| - continuous = options.continuous || true, // once or continuous |
17 |
| - direction = options.direction || 'ltr', // ltr or rtl |
18 |
| - loops = options.loops || -1, |
19 |
| - speed = options.speed || 0.5, |
20 |
| - milestone = 0, |
21 |
| - marqueeElem = null, |
22 |
| - elemWidth = null, |
23 |
| - self = this, |
24 |
| - ltrCond = 0, |
25 |
| - loopCnt = 0, |
26 |
| - start = 0, |
27 |
| - textcolor = options.textcolor || '#000000', // Define the text color |
28 |
| - bgcolor = options.bgcolor || '#ffffff', // Define the background color |
29 |
| - opacity = options.opacity || 1.0, |
30 |
| - process = null, |
31 |
| - constructor = function (elem) { |
32 |
| - |
33 |
| - // Build html |
34 |
| - var elemHTML = elem.innerHTML; |
35 |
| - var elemNode = elem.childNodes[1] || elem; |
36 |
| - elemWidth = elemNode.offsetWidth; |
37 |
| - marqueeElem = '<div>' + elemHTML + '</div>'; |
38 |
| - elem.innerHTML = marqueeElem; |
39 |
| - marqueeElem = elem.getElementsByTagName('div')[0]; |
40 |
| - elem.style.overflow = 'hidden'; |
41 |
| - marqueeElem.style.whiteSpace = 'nowrap'; |
42 |
| - marqueeElem.style.position = 'relative'; |
43 |
| - marqueeElem.style.color = textcolor; |
44 |
| - marqueeElem.style.backgroundColor = bgcolor; |
45 |
| - marqueeElem.style.opacity = opacity; |
46 |
| - |
47 |
| - if (continuous) { |
48 |
| - marqueeElem.innerHTML += elemHTML; |
49 |
| - marqueeElem.style.width = '200%'; |
50 |
| - |
51 |
| - if (direction === 'ltr') { |
52 |
| - start = -elemWidth; |
53 |
| - } |
54 |
| - } else { |
55 |
| - ltrCond = elem.offsetWidth; |
56 |
| - |
57 |
| - if (direction === 'rtl') { |
58 |
| - milestone = ltrCond; |
59 |
| - } |
60 |
| - } |
61 |
| - |
62 |
| - if (direction === 'ltr') { |
63 |
| - milestone = -elemWidth; |
64 |
| - } else if (direction === 'rtl') { |
65 |
| - speed = -speed; |
| 1 | + function Marquee(selector, speed) { |
| 2 | + const parentSelector = document.querySelector(selector); |
| 3 | + const clone = parentSelector.innerHTML; |
| 4 | + const firstElement = parentSelector.children[0]; |
| 5 | + let i = 0; |
| 6 | + console.log(firstElement); |
| 7 | + parentSelector.insertAdjacentHTML('beforeend', clone); |
| 8 | + parentSelector.insertAdjacentHTML('beforeend', clone); |
| 9 | + |
| 10 | + setInterval(function () { |
| 11 | + firstElement.style.marginLeft = `-${i}px`; |
| 12 | + if (i > firstElement.clientWidth) { |
| 13 | + i = 0; |
66 | 14 | }
|
67 |
| - |
68 |
| - self.start(); |
69 |
| - |
70 |
| - return marqueeElem; |
71 |
| - } |
72 |
| - |
73 |
| - this.start = function () { |
74 |
| - process = window.setInterval(function () { |
75 |
| - self.play(); |
76 |
| - }); |
77 |
| - }; |
78 |
| - |
79 |
| - this.play = function () { |
80 |
| - // beginning |
81 |
| - marqueeElem.style.left = start + 'px'; |
82 |
| - start = start + speed; |
83 |
| - |
84 |
| - if (start > ltrCond || start < -elemWidth) { |
85 |
| - start = milestone; |
86 |
| - loopCnt++; |
87 |
| - |
88 |
| - if (loops !== -1 && loopCnt >= loops) { |
89 |
| - marqueeElem.style.left = 0; |
90 |
| - } |
91 |
| - } |
92 |
| - } |
93 |
| - |
94 |
| - this.end = function () { |
95 |
| - window.clearInterval(process); |
| 15 | + i = i + speed; |
| 16 | + }, 0); |
96 | 17 | }
|
97 | 18 |
|
98 |
| - // Init plugin |
99 |
| - marqueeElem = constructor(elem); |
100 |
| -} |
| 19 | + //after window is completed load |
| 20 | + //1 class selector for marquee |
| 21 | + //2 marquee speed 0.2 |
| 22 | + window.addEventListener('load', Marquee('[data-trusted-brands]', 0.5)) |
0 commit comments