-
Notifications
You must be signed in to change notification settings - Fork 5
/
code.js
46 lines (42 loc) · 1.22 KB
/
code.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
let header = document.querySelector('header')
if (header.offsetWidth >= 768) {
let card = document.querySelector('.card3d')
let img = card.querySelector('.card3d-image')
let glare = card.querySelector('.card3d-glare')
header.addEventListener('mousemove', e => {
let {
left,
right,
top,
bottom,
height,
width
} = img.getBoundingClientRect()
let centerX = right - width / 2
let centerY = bottom - height / 2
img.style.transform = `
rotateX(${-15 * (e.pageY - centerY) / header.offsetHeight}deg)
rotateY(${25 * (e.pageX - centerX) / header.offsetWidth}deg)`
glare.style.transform = `
translateX(${-130 * e.pageX / header.offsetWidth}px)
translateY(${-250 * e.pageY / header.offsetHeight}px)`
})
let id
let tran = 50
header.addEventListener('mouseenter', e => {
id = setInterval(() => {
if (tran <= 50) {
clearInterval(id)
} else {
tran -= 6
img.style.transition = `transform ${tran}ms`
}
}, 16)
})
header.addEventListener('mouseleave', e => {
if (id) clearInterval(id)
tran = 500
img.style.transition = `transform ${tran}ms`
img.style.transform = ''
})
}