marquee-content 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "marquee-content",
3
- "version": "1.2.0",
4
- "description": "Marquee content with GSAP",
3
+ "version": "1.4.0",
4
+ "description": "Infinite marquee animation with GSAP",
5
5
  "author": "ux-ui.pro",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -37,6 +37,6 @@
37
37
  "webpack-stream": "^7.0.0"
38
38
  },
39
39
  "keywords": [
40
- "marquee"
40
+ "marquee animation"
41
41
  ]
42
42
  }
package/src/index.html CHANGED
@@ -11,7 +11,7 @@
11
11
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
12
12
  <link href="https://fonts.googleapis.com/css2?family=Alkatra:wght@700&family=Anton&family=Lobster&family=PT+Sans+Narrow:wght@700&family=Prompt:wght@500&display=swap" rel="stylesheet">
13
13
  <style type="text/css">
14
- html{font-family: system-ui, -apple-system, "Open Sans", "Helvetica Neue", Arial, "Noto Sans", "Liberation Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";font-size: 10px}body{margin: 150px 0 400px;font-feature-settings: "kern" 1,"liga" 0;font-size: 1.6rem;font-weight: 400;line-height: 1.5;color: #444}html,body,input,textarea,button{-webkit-font-smoothing: subpixel-antialiased}marquee-content{pointer-events: none;max-width: 100vw;justify-content: center;align-items: center;display: flex;position: relative;overflow: hidden}.font{white-space: nowrap}.alkatra{font-family: 'Alkatra', cursive;font-weight: 700;font-size: 48px}.anton{font-family: 'Anton', sans-serif;font-weight: 700;font-size: 40px}.pt{font-family: 'PT Sans Narrow', sans-serif;font-weight: 400;font-size: 52px}.lobster{font-family: 'Lobster', cursive;font-weight: 400;font-size: 50px}.prompt{font-family: 'Prompt', sans-serif;font-weight: 500;font-size: 48px}ul{list-style: none;margin: 0;padding: 0;display: flex;align-items: center}li{padding: 0 10px;text-align: center}.example-1 li{padding: 0 0 0 66px;background:url("data:image/svg xml,<svg width='26' height='26' viewBox='0 0 26 26' fill='none' xmlns='http://www.w3.org/2000/svg'><path d='M15.0527 10.9474L13 0L10.9473 10.9474L0 13L10.9473 15.0526L13 26L15.0527 15.0526L26 13L15.0527 10.9474Z' fill='#444444'/></svg>")center left 20px / 26px 26pxno-repeat}.example-2 li{padding: 0 15px}.example-3 li{position: relative;padding: 0 0 0 56px}.example-3 li::before{content: '';position: absolute;top: 0;right: -33px;bottom: 0;left: auto;margin: auto;width: 7px;height: 7px;transform: translateY(5px);background: #444;border-radius: 100%}.example-4{text-transform: uppercase}.data-code{margin: 80px auto 30px;padding: 0 20px;max-width: 1400px}.data-code strong{display: inline-flex;padding: 5px 12px;border: 1px dotted #ccc}
14
+ html{font-family: system-ui, -apple-system, "Open Sans", "Helvetica Neue", Arial, "Noto Sans", "Liberation Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";font-size: 10px}body{margin: 150px 0 400px;font-feature-settings: "kern" 1,"liga" 0;font-size: 1.6rem;font-weight: 400;line-height: 1.5;color: #444}html,body,input,textarea,button{-webkit-font-smoothing: subpixel-antialiased}.marquee-content{pointer-events: none;max-width: 100vw;justify-content: center;align-items: center;display: flex;position: relative;overflow: hidden}.font{white-space: nowrap}.alkatra{font-family: 'Alkatra', cursive;font-weight: 700;font-size: 48px}.anton{font-family: 'Anton', sans-serif;font-weight: 700;font-size: 40px}.pt{font-family: 'PT Sans Narrow', sans-serif;font-weight: 400;font-size: 52px}.lobster{font-family: 'Lobster', cursive;font-weight: 400;font-size: 50px}.prompt{font-family: 'Prompt', sans-serif;font-weight: 500;font-size: 48px}ul{list-style: none;margin: 0;padding: 0;display: flex;align-items: center}li{padding: 0 10px;text-align: center}.example-1 li{padding: 0 0 0 66px;background:url("data:image/svg xml,<svg width='26' height='26' viewBox='0 0 26 26' fill='none' xmlns='http://www.w3.org/2000/svg'><path d='M15.0527 10.9474L13 0L10.9473 10.9474L0 13L10.9473 15.0526L13 26L15.0527 15.0526L26 13L15.0527 10.9474Z' fill='#444444'/></svg>")center left 20px / 26px 26pxno-repeat}.example-2 li{padding: 0 15px}.example-3 li{position: relative;padding: 0 0 0 56px}.example-3 li::before{content: '';position: absolute;top: 0;right: -33px;bottom: 0;left: auto;margin: auto;width: 7px;height: 7px;transform: translateY(5px);background: #444;border-radius: 100%}.example-4{text-transform: uppercase}.data-code{margin: 80px auto 30px;padding: 0 20px;max-width: 1400px}.data-code strong{display: inline-flex;padding: 5px 12px;border: 1px dotted #ccc}
15
15
  </style>
16
16
  </head>
17
17
  <body>
@@ -20,7 +20,7 @@
20
20
  <p>The basic version, with scrolling content from right to left</p>
21
21
  </section>
22
22
  <section class="example-1">
23
- <marquee-content
23
+ <div class="marquee-content"
24
24
  data-mc-duration="60"
25
25
  role="marquee">
26
26
  <ul class="font alkatra">
@@ -33,10 +33,10 @@
33
33
  <li>Vomero</li>
34
34
  <li>Pegasus</li>
35
35
  </ul>
36
- </marquee-content>
36
+ </div>
37
37
  </section>
38
38
  <section class="example-7">
39
- <marquee-content
39
+ <div class="marquee-content"
40
40
  data-mc-duration="20"
41
41
  role="marquee">
42
42
  <ul>
@@ -45,14 +45,14 @@
45
45
  <li><img src="//fastly.picsum.photos/id/235/180/100.jpg?hmac=5KlYbDb93R7me0GThn6FFoK_OZ8ziPn-fb98FHIMb5c" alt></li>
46
46
  <li><img src="//fastly.picsum.photos/id/1050/180/100.jpg?hmac=f9i0nGYKBetkohJors44GETXmUhSbLcXMUlSvMCluJY" alt></li>
47
47
  </ul>
48
- </marquee-content>
48
+ </div>
49
49
  </section>
50
50
  <section class="data-code">
51
51
  <strong>data-mc-duration=&quot;60&quot; data-mc-direction=&quot;ltr&quot;</strong>
52
52
  <p><code>data-mc-direction="ltr"</code> &mdash; changes the scroll direction from left to right</p>
53
53
  </section>
54
54
  <section class="example-2">
55
- <marquee-content
55
+ <div class="marquee-content"
56
56
  data-mc-duration="60"
57
57
  data-mc-direction="ltr"
58
58
  role="marquee">
@@ -67,14 +67,14 @@
67
67
  <li><span class="font anton">Vomero</span></li>
68
68
  <li><span class="font pt">Pegasus</span></li>
69
69
  </ul>
70
- </marquee-content>
70
+ </div>
71
71
  </section>
72
72
  <section class="data-code">
73
73
  <strong>data-mc-duration=&quot;60&quot; data-mc-direction=&quot;auto&quot;</strong>
74
74
  <p><code>data-mc-direction="auto"</code> &mdash; automatically changes the scrolling direction of the component when scrolling the page</p>
75
75
  </section>
76
76
  <section class="example-3">
77
- <marquee-content
77
+ <div class="marquee-content"
78
78
  data-mc-duration="60"
79
79
  data-mc-direction="auto"
80
80
  role="marquee">
@@ -89,14 +89,14 @@
89
89
  <li>Vomero</li>
90
90
  <li>Pegasus</li>
91
91
  </ul>
92
- </marquee-content>
92
+ </div>
93
93
  </section>
94
94
  <section class="data-code">
95
95
  <strong>data-mc-duration=&quot;60&quot; data-mc-skew=&quot;-4&quot;</strong>
96
96
  <p><code>data-mc-skew="-4"</code> &mdash; transforms the component by tilting it along the Y axis (it is acceptable to use a positive or negative value)</p>
97
97
  </section>
98
98
  <section class="example-4">
99
- <marquee-content
99
+ <div class="marquee-content"
100
100
  data-mc-duration="60"
101
101
  data-mc-skew="-4"
102
102
  role="marquee">
@@ -111,35 +111,35 @@
111
111
  <li>Vomero</li>
112
112
  <li>Pegasus</li>
113
113
  </ul>
114
- </marquee-content>
114
+ </div>
115
115
  </section>
116
116
  <section class="data-code">
117
117
  <strong>data-mc-duration=&quot;5&quot; data-mc-min=&quot;992&quot;</strong>
118
118
  <p><code>data-mc-min="992"</code> &mdash; sets the minimum screen width at which the animation will be played (when the breakpoint intersects, the cloned elements will be removed and the component styles will be cleared)</p>
119
119
  </section>
120
120
  <section class="example-5">
121
- <marquee-content
121
+ <div class="marquee-content"
122
122
  data-mc-duration="5"
123
123
  data-mc-min="992"
124
124
  role="marquee">
125
125
  <ul class="font pt">
126
126
  <li>Jordan</li>
127
127
  </ul>
128
- </marquee-content>
128
+ </div>
129
129
  </section>
130
130
  <section class="data-code">
131
131
  <strong>data-mc-duration=&quot;5&quot; data-mc-max=&quot;992&quot;</strong>
132
132
  <p><code>data-mc-max="992"</code> &mdash; sets the maximum screen width at which the animation will be played (when the breakpoint intersects, the cloned elements will be removed and the component styles will be cleared)</p>
133
133
  </section>
134
134
  <section class="example-6">
135
- <marquee-content
135
+ <div class="marquee-content"
136
136
  data-mc-duration="5"
137
137
  data-mc-max="992"
138
138
  role="marquee">
139
139
  <ul class="font pt">
140
140
  <li>Presto</li>
141
141
  </ul>
142
- </marquee-content>
142
+ </div>
143
143
  </section>
144
144
  <script src="js/marquee-content.min.js"></script>
145
145
  </body>
package/src/js/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  import { MarqueeContent } from './marquee-content.js'
2
2
 
3
- new MarqueeContent()
3
+ MarqueeContent()
@@ -3,180 +3,187 @@ import ScrollTrigger from 'gsap/ScrollTrigger.js'
3
3
 
4
4
  gsap.registerPlugin(ScrollTrigger)
5
5
 
6
- export class MarqueeContent extends HTMLElement {
7
- constructor() {
8
- super()
9
-
10
- this.mm = gsap.matchMedia()
11
- this.tl = gsap.timeline()
12
- this.dur = this.dataset.mcDuration || 20
13
- this.skew = this.dataset.mcSkew
14
- this.max = this.dataset.mcMax
15
- this.min = this.dataset.mcMin
16
- this.dir = this.dataset.mcDirection
17
-
18
- this.breakpoints()
19
- this.cloning()
20
- this.skewed()
21
- this.marquee()
22
- this.scrollReverse()
23
- this.debounce()
24
- this.resizing()
25
- }
26
-
27
- breakpoints() {
28
- if(this.max || this.min) {
29
- if(this.max) {
30
- this.breakpoint = `(max-width: ${this.max - 0.02}px)`
31
- }
32
-
33
- if(this.min) {
34
- this.breakpoint = `(min-width: ${this.min}px)`
6
+ const MarqueeContent = () => {
7
+ const items = document.querySelectorAll('.marquee-content')
8
+
9
+ for (const item of items) {
10
+ let matchMedia = gsap.matchMedia(),
11
+ duration = item.dataset.mcDuration || 20,
12
+ skew = item.dataset.mcSkew,
13
+ maxWidth = item.dataset.mcMax,
14
+ minWidth = item.dataset.mcMin,
15
+ direction = item.dataset.mcDirection,
16
+ breakpoint
17
+
18
+ const
19
+
20
+ debounce = (fn, delay) => {
21
+ let timer
22
+
23
+ return (...args) => {
24
+ if (timer) clearTimeout(timer)
25
+ timer = setTimeout(() => fn(...args), delay)
35
26
  }
36
- } else {
37
- this.breakpoint = ``
38
- }
39
- }
40
-
41
- cloning() {
42
- const removingClones = () => {
43
- while (this.children.length > 1) {
44
- this.removeChild(this.lastChild)
45
- }
46
- }
47
-
48
- removingClones()
49
-
50
- this.mm.add(this.breakpoint, () => {
51
- if (this.children.length > 0) {
52
- let requiredQuantity = Math.ceil(this.clientWidth / this.children[0].scrollWidth)
27
+ },
53
28
 
54
- for (let i = 1; i < (requiredQuantity) + 2; i++) {
55
- let item = this.firstElementChild
56
- let clone = item.cloneNode(true)
57
- item.parentNode.append(clone)
29
+ breakpoints = () => {
30
+ if(maxWidth || minWidth) {
31
+ if(maxWidth) {
32
+ breakpoint = `(max-width: ${maxWidth - 0.02}px)`
58
33
  }
59
- }
60
34
 
61
- return () => {
62
- removingClones()
35
+ if(minWidth) {
36
+ breakpoint = `(min-width: ${minWidth}px)`
37
+ }
38
+ } else {
39
+ breakpoint = ``
63
40
  }
64
- })
65
- }
66
-
67
- skewed() {
68
- if(!this.skew) { return }
69
-
70
- this.mm.add(this.breakpoint, () => {
71
- let abs = (this.skew < 0) ? this.skew * -1 : this.skew
41
+ },
72
42
 
73
- this.style.transformOrigin = `center center`
74
- this.style.transform = `skew(0deg, ${this.skew}deg)`
75
- this.style.minHeight = `calc(${abs * 1.25}vh + ${abs * 1.25}vw)`
76
-
77
- return () => {
78
- this.style.removeProperty('transform-origin')
79
- this.style.removeProperty('transform')
80
- this.style.removeProperty('min-height')
43
+ cloning = () => {
44
+ const removingClones = () => {
45
+ while (item.children.length > 1) {
46
+ item.removeChild(item.lastChild)
47
+ }
81
48
  }
82
- })
83
- }
84
49
 
85
- marquee() {
86
- this.mm.add(this.breakpoint, () => {
87
- this.tl = gsap.timeline()
88
-
89
- gsap.config({
90
- nullTargetWarn: false
91
- })
50
+ removingClones()
92
51
 
93
- this.tl.paused(true)
52
+ matchMedia.add(breakpoint, () => {
53
+ if (item.children.length > 0) {
54
+ let requiredQuantity = Math.ceil(item.clientWidth / item.children[0].scrollWidth)
94
55
 
95
- gsap.set(this.children, {
96
- 'will-change': 'transform'
97
- })
56
+ for (let i = 1; i < (requiredQuantity) + 2; i++) {
57
+ let cloned = item.firstElementChild
58
+ let clone = cloned.cloneNode(true)
59
+ cloned.parentNode.append(clone)
60
+ }
61
+ }
98
62
 
99
- this.tl.to(this.children, {
100
- duration: this.dur,
101
- x: '-100%',
102
- ease: 'none',
103
- repeat: -1
104
- }).timeScale(this.dir === 'ltr' ? -1 : 1).totalProgress(.5)
105
-
106
- ScrollTrigger.create({
107
- trigger: this,
108
- start: 'top bottom',
109
- end: 'bottom top',
110
- onEnter: () => this.tl.resume(),
111
- onLeave: () => this.tl.pause(),
112
- onEnterBack: () => this.tl.resume(),
113
- onLeaveBack: () => this.tl.pause()
63
+ return () => {
64
+ removingClones()
65
+ }
114
66
  })
115
- })
116
- }
67
+ },
117
68
 
118
- scrollReverse() {
119
- if(this.dir !== 'auto') { return }
69
+ skewed = () => {
70
+ if(!skew) { return }
120
71
 
121
- this.mm.add(this.breakpoint, () => {
122
- let currentScroll = 0,
123
- scrollDirection = 1
72
+ matchMedia.add(breakpoint, () => {
73
+ let abs = (skew < 0) ? skew * -1 : skew
124
74
 
125
- window.addEventListener('scroll', () => {
126
- let orientation = (window.pageYOffset > currentScroll) ? 1 : -1
75
+ item.style.transformOrigin = `center center`
76
+ item.style.transform = `skew(0deg, ${skew}deg)`
77
+ item.style.minHeight = `calc(${abs * 1.25}vh + ${abs * 1.25}vw)`
127
78
 
128
- if (orientation !== scrollDirection) {
129
- gsap.to(this.tl, {
130
- timeScale: orientation,
79
+ return () => {
80
+ item.style.removeProperty('transform-origin')
81
+ item.style.removeProperty('transform')
82
+ item.style.removeProperty('min-height')
83
+ }
84
+ })
85
+ },
86
+
87
+ marquee = () => {
88
+ matchMedia.add(breakpoint, () => {
89
+ gsap.config({
90
+ nullTargetWarn: false
91
+ })
92
+
93
+ gsap.set(item.children, {
94
+ 'will-change': 'transform'
95
+ })
96
+
97
+ let tween = gsap.to(item.children, {
98
+ duration: duration,
99
+ x: '-100%',
100
+ ease: 'none',
101
+ repeat: -1,
102
+ scrollTrigger: {
103
+ trigger: item,
104
+ start: 'top bottom',
105
+ end: 'bottom top',
106
+ toggleActions: 'resume pause resume pause'
107
+ },
108
+ }).totalProgress(.5)
109
+
110
+ // TODO refactoring of the content scrolling direction
111
+ let previousScrollPosition = 0
112
+
113
+ const isScrollingDown = () => {
114
+ let someCondition = false
115
+
116
+ let scrollPosition = window.pageYOffset
117
+
118
+ if (scrollPosition > previousScrollPosition) {
119
+ someCondition = true
120
+ }
121
+
122
+ previousScrollPosition = scrollPosition
123
+
124
+ gsap.to(tween, {
125
+ timeScale: someCondition ? -1 : 1,
131
126
  overwrite: true
132
127
  })
128
+ }
129
+
130
+ let someCondition
133
131
 
134
- scrollDirection = orientation
132
+ if(direction === 'ltr') {
133
+ someCondition = true
134
+ } else if(direction === 'auto') {
135
+ window.addEventListener('scroll', isScrollingDown, { capture: true, passive: true })
135
136
  }
136
137
 
137
- currentScroll = window.pageYOffset
138
+ gsap.to(tween, {
139
+ timeScale: someCondition ? -1 : 1,
140
+ overwrite: true
141
+ })
138
142
  })
139
- })
140
- }
143
+ },
141
144
 
142
- debounce(fn, delay) {
143
- let timer
145
+ resizing = () => {
146
+ const resetAnimation = () => {
147
+ let tl = gsap.timeline()
144
148
 
145
- return (...args) => {
146
- if (timer) clearTimeout(timer)
147
- timer = setTimeout(() => fn(...args), delay)
148
- }
149
- }
149
+ tl.kill()
150
+ tl = null
150
151
 
151
- resizing() {
152
- const resetAnimation = () => {
153
- this.mm.add(this.breakpoint, () => {
154
- this.tl.kill()
155
- this.tl = null
152
+ gsap.set(item.children, { clearProps: true })
156
153
 
157
- gsap.set(this.children, { clearProps: true })
154
+ cloning()
155
+ marquee()
156
+ }
158
157
 
159
- this.cloning()
160
- this.marquee()
161
- })
162
- }
158
+ matchMedia.add('(any-pointer: coarse)', () => {
159
+ let portrait = window.matchMedia('(orientation: portrait)')
163
160
 
164
- this.mm.add('(any-pointer: coarse)', () => {
165
- let portrait = window.matchMedia('(orientation: portrait)')
161
+ portrait.addEventListener('change', debounce((e) => {
162
+ if(!e.matches) {
163
+ resetAnimation()
164
+ }
165
+ }, 250))
166
+ })
166
167
 
167
- portrait.addEventListener('change', this.debounce((e) => {
168
- if(!e.matches) {
168
+ matchMedia.add('(any-pointer: fine)', () => {
169
+ window.addEventListener('resize', debounce(() => {
169
170
  resetAnimation()
170
- }
171
- }, 250))
172
- })
173
-
174
- this.mm.add('(any-pointer: fine)', () => {
175
- window.addEventListener('resize', this.debounce(() => {
176
- resetAnimation()
177
- }, 250))
178
- })
171
+ }, 250))
172
+ })
173
+ },
174
+
175
+ init = () => {
176
+ breakpoints()
177
+ cloning()
178
+ skewed()
179
+ marquee()
180
+ resizing()
181
+ }
182
+
183
+ init()
179
184
  }
180
185
  }
181
186
 
182
- customElements.get('marquee-content') || customElements.define('marquee-content', MarqueeContent)
187
+ export {
188
+ MarqueeContent
189
+ }