@linear_non/stellar-libs 1.0.24 → 1.0.25
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 +1 -1
- package/src/Sticky/index.js +36 -14
package/package.json
CHANGED
package/src/Sticky/index.js
CHANGED
|
@@ -5,13 +5,11 @@ import { gsap, ScrollTrigger } from "@linear_non/stellar-kit/gsap"
|
|
|
5
5
|
export default class Sticky {
|
|
6
6
|
constructor(obj) {
|
|
7
7
|
const { el, sticky, isSmooth, onUpdateCallback } = obj
|
|
8
|
-
|
|
9
8
|
this.el = el
|
|
10
9
|
this.sticky = sticky
|
|
11
10
|
this.isSticky = false
|
|
12
11
|
this.isSmooth = isSmooth
|
|
13
12
|
this.progress = 0
|
|
14
|
-
this.total = 0
|
|
15
13
|
this.positionX = 0
|
|
16
14
|
this.positionY = 0
|
|
17
15
|
this.scrollTrigger = null
|
|
@@ -24,23 +22,26 @@ export default class Sticky {
|
|
|
24
22
|
totalHeight: 0,
|
|
25
23
|
totalItems: 0,
|
|
26
24
|
}
|
|
25
|
+
|
|
27
26
|
if (!isSmooth) sticky.style.pointerEvents = "none"
|
|
28
27
|
this.init()
|
|
29
28
|
}
|
|
30
29
|
|
|
31
30
|
observer() {
|
|
32
|
-
if (this.scrollTrigger) this.scrollTrigger.kill()
|
|
31
|
+
if (this.scrollTrigger) this.scrollTrigger.kill()
|
|
33
32
|
|
|
34
33
|
this.scrollTrigger = ScrollTrigger.create({
|
|
35
34
|
trigger: this.el,
|
|
36
35
|
start: "top top",
|
|
37
36
|
end: "bottom bottom",
|
|
38
37
|
scrub: true,
|
|
38
|
+
invalidateOnRefresh: true,
|
|
39
39
|
onEnter: () => this.toggleSticky(true),
|
|
40
40
|
onEnterBack: () => this.toggleSticky(true),
|
|
41
41
|
onLeave: () => this.toggleSticky(false, true),
|
|
42
42
|
onLeaveBack: () => this.toggleSticky(false),
|
|
43
43
|
onUpdate: self => this.onUpdate(self),
|
|
44
|
+
onRefresh: self => this.onRefresh(self),
|
|
44
45
|
})
|
|
45
46
|
}
|
|
46
47
|
|
|
@@ -67,17 +68,13 @@ export default class Sticky {
|
|
|
67
68
|
const newY = newProgress * totalHeight
|
|
68
69
|
const newX = -newProgress * totalWidth
|
|
69
70
|
|
|
70
|
-
// Only update if values change to prevent unnecessary calls
|
|
71
71
|
if (this.progress !== newProgress || this.positionY !== newY || this.positionX !== newX) {
|
|
72
72
|
this.progress = newProgress
|
|
73
73
|
this.positionY = newY
|
|
74
74
|
this.positionX = newX
|
|
75
75
|
|
|
76
|
-
if (this.isSmooth) {
|
|
77
|
-
gsap.set(this.sticky, { y: this.positionY })
|
|
78
|
-
}
|
|
76
|
+
if (this.isSmooth) gsap.set(this.sticky, { y: this.positionY })
|
|
79
77
|
|
|
80
|
-
// Call the callback function if provided
|
|
81
78
|
if (this.onUpdateCallback) {
|
|
82
79
|
this.onUpdateCallback({
|
|
83
80
|
progress: this.progress,
|
|
@@ -88,18 +85,43 @@ export default class Sticky {
|
|
|
88
85
|
}
|
|
89
86
|
}
|
|
90
87
|
|
|
91
|
-
|
|
92
|
-
|
|
88
|
+
onRefresh(self) {
|
|
89
|
+
this.computeSizes()
|
|
93
90
|
|
|
94
|
-
|
|
91
|
+
const { totalWidth, totalHeight } = this.config
|
|
92
|
+
this.progress = self?.progress || 0
|
|
93
|
+
this.positionY = this.progress * totalHeight
|
|
94
|
+
this.positionX = -this.progress * totalWidth
|
|
95
|
+
|
|
96
|
+
if (this.isSmooth) {
|
|
97
|
+
gsap.set(this.sticky, { y: this.positionY })
|
|
98
|
+
} else {
|
|
99
|
+
if (this.isSticky) gsap.set(this.sticky, { top: 0 })
|
|
100
|
+
else gsap.set(this.sticky, { top: `${this.positionY}px` })
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
computeSizes() {
|
|
105
|
+
if (!this.sticky) return
|
|
95
106
|
const vh = window.innerHeight
|
|
96
|
-
|
|
107
|
+
const stickyRect = bounds(this.el)
|
|
108
|
+
const totalHeight = Math.max(stickyRect.height - vh, 0)
|
|
97
109
|
|
|
98
110
|
Object.assign(this.config, {
|
|
99
|
-
totalHeight
|
|
111
|
+
totalHeight,
|
|
112
|
+
totalWidth: this.config.totalWidth || 0,
|
|
100
113
|
})
|
|
101
114
|
}
|
|
102
115
|
|
|
116
|
+
resize() {
|
|
117
|
+
this.computeSizes()
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
refresh() {
|
|
121
|
+
this.computeSizes()
|
|
122
|
+
if (this.scrollTrigger) this.scrollTrigger.refresh()
|
|
123
|
+
}
|
|
124
|
+
|
|
103
125
|
destroy() {
|
|
104
126
|
if (this.scrollTrigger) {
|
|
105
127
|
this.scrollTrigger.kill()
|
|
@@ -110,7 +132,7 @@ export default class Sticky {
|
|
|
110
132
|
}
|
|
111
133
|
|
|
112
134
|
init() {
|
|
113
|
-
this.
|
|
135
|
+
this.computeSizes()
|
|
114
136
|
this.observer()
|
|
115
137
|
}
|
|
116
138
|
}
|