-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
218 lines (161 loc) · 5.7 KB
/
script.js
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
//preloader -------------------------------------------
const loaderContainer = document.querySelector('.loader-container')
window.addEventListener('load', () => {
loaderContainer.classList.add('hidden')
})
// gsap scrolltrigger ------------------------------------
const entries = document.querySelectorAll('.entry')
entries.forEach(entry => {
let entryMeta = entry.querySelector('.entry__meta')
let entryMedia = entry.querySelector('.entry__media')
let tl = gsap.timeline({
scrollTrigger: {
trigger: entry,
start: 'top bottom',
end: 'bottom 90%',
markers: false,
scrub: true
}
})
tl.fromTo(entryMeta, {xPercent: -100, opacity: 0}, {xPercent:0, opacity:1})
tl.fromTo(entryMedia, {xPercent: 100, opacity: 0}, {xPercent:0, opacity:1}, '<')
})
//lenis gsap scrolltrigger
/*
const lenis = new Lenis()
lenis.on('scroll', ScrollTrigger.update)
gsap.ticker.add((time)=>{
lenis.raf(time * 6000)
})
gsap.ticker.lagSmoothing(0)
*/
//menu button toggle ---------------------------------------
const menuButton = document.querySelector('#menu-button')
const rootElement = document.documentElement
const menuLinks = document.querySelectorAll('#menu-link')
menuButton.addEventListener('click', () => {
rootElement.toggleAttribute('menu-open')
})
var count = 0;
menuLinks.forEach(menulink => {
menulink.addEventListener('click', () => {
//console.log("Clicked! count:" + ++count);
rootElement.toggleAttribute('menu-open')
})
})
// animated submit button --------------------------------------
const submitButton = document.querySelector('#btn-submit')
const submitButtonText = document.querySelector('#btn-submit .button--text')
submitButton.addEventListener('click', (e) => {
e.preventDefault()
//add loading class
submitButton.classList.add('loading')
setTimeout(() => {
//remove loading class
submitButton.classList.remove('loading')
submitButton.classList.add('success')
//change button text
submitButtonText.innerHTML = 'Login Successful'
}, 3000)
})
// typewriter effect --------------------------------------------
const words = ['Style', 'Clarity', 'Passion', 'Precision']
//main timeline
let mainTimeline = gsap.timeline({
repeat: -1
})
//for each word, create new timeline, use text plugin, append that timeline to the main one
words.forEach(word =>{
let textTimeline = gsap.timeline({
repeat: 1,
yoyo: true,
repeatDelay: 3
})
textTimeline.to('#typewriter', {
text: word,
duration: 1,
onUpdate: () => {
cursorTimeline.restart()
cursorTimeline.pause()
},
onComplete: () => {
cursorTimeline.play()
}
})
mainTimeline.add(textTimeline)
})
//blinkin cursor
let cursorTimeline = gsap.timeline({
repeat: -1,
repeatDelay: .8
})
cursorTimeline.to('#cursor', {
opacity: 1,
duration: 0
}).to('#cursor', {
opacity: 0,
duration: 0,
delay: .8
})
// mouse follow ------------------------------------------
//get magnet
const magnet = document.querySelector('.magnet')
const magnetText = document.querySelector('.magnet .text')
const dngr = document.querySelector('#debugger')
//mouse move stuff
const activateMagnet = (event) => {
let boundBox = magnet.getBoundingClientRect()
const magnetStrength = 40
const magnetTextStrength = 80
const newX = ((event.clientX - boundBox.left)/magnet.offsetWidth - 0.5)
const newY = ((event.clientY - boundBox.top)/magnet.offsetHeight - 0.5)
//debugging purposes
/*
dngr.innerHTML = 'cursorX ' + event.clientX + '<br>boxleft: ' + Math.ceil(boundBox.left) + '<br>cursorInsideButton: ' + Math.ceil(event.clientX - boundBox.left) + '<br>relativeToTotalWidth: ' + ((event.clientX - boundBox.left)/magnet.offsetWidth).toFixed(2) + '<br>shifted: ' + ((event.clientX - boundBox.left)/magnet.offsetWidth - 0.5).toFixed(2)
*/
//move button to its new position
gsap.to(magnet, {
duration: 1,
x: newX * magnetStrength,
y: newY * magnetStrength,
ease: Power4.easeOut
})
//move button text to its new posistion
gsap.to(magnetText, {
duration: 1,
x: newX * magnetTextStrength,
y: newY * magnetTextStrength,
ease: Power4.easeOut
})
}
//mouse leave stuff
const resetMagnet = (event) => {
//move button to its default position
gsap.to(magnet, {
duration: 1,
x: 0,
y: 0,
ease: Elastic.easeOut
})
//move button text to its default position
gsap.to(magnetText, {
duration: 1,
x: 0,
y: 0,
ease: Elastic.easeOut
})
}
//add event listeners
magnet.addEventListener('mousemove', activateMagnet)
magnet.addEventListener('mouseleave', resetMagnet)
// change background and button color--------------------------------------
const btn = document.querySelector('.togglebutton')
document.querySelector('.togglebutton').addEventListener('click', () => {
document.getElementById("change-bg").classList.toggle("change-figure-bg");
const initialText = `Change background to light <i class="fa-solid fa-sun"></i>`;
if (btn.innerHTML.toLowerCase().includes(initialText.toLowerCase())) {
btn.innerHTML = `Change background to dark <i class="fa-solid fa-moon">`;
} else {
btn.innerHTML = initialText;
}
})