-
Notifications
You must be signed in to change notification settings - Fork 0
/
infinite-text-carousel.css
150 lines (125 loc) · 3.81 KB
/
infinite-text-carousel.css
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
/* Inserir um texto com efeito de letreiro */
/* 1 - Colar o texto abaixo em Avançado => Atributos (* requer Elementor Pro*) do conteiner:
data-marquee|Seu texto aqui • */
/* ======================================================================= */
/* 2 - Colar o código Javascript abaixo em um widget HTML dentro do conteiner: */
<script>
const getMarqueeNode = content => {
const el = document.createElement('div');
el.setAttribute('data-marquee-style', true);
el.textContent = content
return el;
}
class Marquee {
constructor(el){
this.el = el;
this.content = el.getAttribute('data-marquee')
this.render();
}
render(){
// create the shadow element to measure and calculate
// the amount of animated items required for marquee
// create
const shadow = getMarqueeNode(this.content);
shadow.setAttribute('data-marquee-shadow', true);
// add shadow element to sanitized DOM
this.el.innerHTML = "";
this.el.appendChild(shadow);
// calculate how many visible items are needed
const inView = this.calculateItemsInView(shadow);
// create container to house animated visible items
const overflow = document.createElement('div');
overflow.setAttribute('data-marquee-overflow', true);
const content = document.createElement('div');
content.setAttribute('data-marquee-container', true);
// put the content container into an overflow: hidden wrapper
overflow.appendChild(content);
// double the amount to fill the screen to animate loop
const count = inView * 3;
// populate with children
for( var i = 0; i < count; i++) {
content.appendChild(getMarqueeNode(this.content))
}
// add to DOM
this.el.appendChild(overflow);
// debug
// console.log("visible items required", inView);
// console.log("total items required", count);
}
calculateItemsInView(ref){
const [single, total] = [ref.clientWidth, ref.parentNode.clientWidth];
// ceil in order to ensure there's never a shortage
return Math.floor(total / single) + 1;
}
}
// find elements and create Marquee instances
const nodes = document.querySelectorAll('[data-marquee]')
const arr = Array.from(nodes);
const refs = (arr || []).map( node => new Marquee(node) );
// recalculate on resize
window.addEventListener( 'resize', () => refs.map( r => r.render() ) );
</script>
/* ================================================================ */
/* 3 - Colar o CSS abaixo em Configurações da página => Custom CSS
ou em Custom CSS do conteiner onde está o script */
@-webkit-keyframes marquee {
100% {
transform: translateX(-100%);
}
}
@keyframes marquee {
100% {
transform: translateX(-100%);
}
}
/* Controlar a velocidade do carrossel */
[data-marquee] {
--loop-duration: 30s;
display: block;
}
[data-marquee] [data-marquee-shadow] {
position: absolute;
visibility: hidden;
height: auto;
width: auto;
white-space: nowrap;
}
[data-marquee] [data-marquee-overflow] {
overflow: hidden;
transform: translate3d(0, 0, 0);
}
[data-marquee] [data-marquee-container] {
display: flex;
-webkit-animation: marquee var(--loop-duration) linear infinite;
animation: marquee var(--loop-duration) linear infinite;
}
[data-marquee] [data-marquee-style] {
flex: 0 0 auto;
display: flex;
align-items: center;
justify-content: center;
}
/* Controlar o estilo do texto */
[data-marquee-style] {
padding: 0.3em 0.3em;
display: flex;
color: #1D1E26;
font-size: 40px;
font-weight: 600;
letter-spacing: 3px;
}
/* Controlar o carrossel no celular */
@media (min-width: 350px) and (max-width: 550px){
[data-marquee-style] {
padding: 0.3em 0.3em;
display: flex;
color: #1D1E26;
font-size: 24px;
font-weight: 500;
letter-spacing: 3px;
}
[data-marquee] {
--loop-duration: 10s;
display: block;
}
}