-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtilted-card.js
More file actions
101 lines (89 loc) · 2.71 KB
/
tilted-card.js
File metadata and controls
101 lines (89 loc) · 2.71 KB
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
// TiltedCard Component for Vanilla JS with GSAP
class TiltedCard {
constructor(element, options = {}) {
this.element = element;
this.options = {
scaleOnHover: 1.1,
rotateAmplitude: 14,
showTooltip: true,
...options
};
this.init();
}
init() {
this.setupElement();
this.bindEvents();
}
setupElement() {
this.element.style.perspective = '800px';
this.element.style.transformStyle = 'preserve-3d';
const img = this.element.querySelector('img');
if (img) {
img.style.borderRadius = '15px';
img.style.willChange = 'transform';
img.style.transform = 'translateZ(0)';
}
}
bindEvents() {
this.element.addEventListener('mousemove', (e) => this.handleMouseMove(e));
this.element.addEventListener('mouseenter', () => this.handleMouseEnter());
this.element.addEventListener('mouseleave', () => this.handleMouseLeave());
}
handleMouseMove(e) {
const rect = this.element.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
const offsetX = e.clientX - centerX;
const offsetY = e.clientY - centerY;
const rotateX = (offsetY / (rect.height / 2)) * -this.options.rotateAmplitude;
const rotateY = (offsetX / (rect.width / 2)) * this.options.rotateAmplitude;
if (typeof gsap !== 'undefined') {
gsap.to(this.element, {
duration: 0.1,
rotateX: rotateX,
rotateY: rotateY,
ease: "power2.out"
});
} else {
this.element.style.transform = `perspective(800px) rotateX(${rotateX}deg) rotateY(${rotateY}deg)`;
}
}
handleMouseEnter() {
if (typeof gsap !== 'undefined') {
gsap.to(this.element, {
duration: 0.3,
scale: this.options.scaleOnHover,
ease: "power2.out"
});
} else {
this.element.style.transition = 'transform 0.3s ease';
this.element.style.transform += ` scale(${this.options.scaleOnHover})`;
}
}
handleMouseLeave() {
if (typeof gsap !== 'undefined') {
gsap.to(this.element, {
duration: 0.3,
scale: 1,
rotateX: 0,
rotateY: 0,
ease: "power2.out"
});
} else {
this.element.style.transform = 'perspective(800px) rotateX(0deg) rotateY(0deg) scale(1)';
}
}
}
// Initialize tilted cards when DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
// Wait a bit for React component to render
setTimeout(() => {
const childElements = document.querySelectorAll('#page3 .child');
childElements.forEach(child => {
new TiltedCard(child, {
scaleOnHover: 1.05,
rotateAmplitude: 12,
});
});
}, 100);
});