-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
174 lines (150 loc) · 5.53 KB
/
script.js
File metadata and controls
174 lines (150 loc) · 5.53 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
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
// Modern Tab Navigation System
class TabNavigator {
constructor() {
this.tabs = document.getElementsByClassName("tab-content");
this.buttons = document.getElementsByClassName("tab-button");
this.activeTab = null;
this.isAnimating = false;
this.initializeEventListeners();
this.setupIntersectionObserver();
}
initializeEventListeners() {
// Add click listeners to all tab buttons
Array.from(this.buttons).forEach(button => {
button.addEventListener('click', (e) => {
e.preventDefault();
const tabId = button.getAttribute('data-tab');
this.switchTab(tabId);
});
// Add hover animation
button.addEventListener('mouseenter', this.handleButtonHover.bind(this));
button.addEventListener('mouseleave', this.handleButtonLeave.bind(this));
});
// Add keyboard navigation
document.addEventListener('keydown', (e) => {
if (e.key === 'ArrowRight' || e.key === 'ArrowLeft') {
this.handleKeyboardNavigation(e.key);
}
});
}
setupIntersectionObserver() {
// Create observer for animation on scroll
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
}
});
}, { threshold: 0.1 });
// Observe tab contents
Array.from(this.tabs).forEach(tab => {
observer.observe(tab);
});
}
async switchTab(tabId) {
if (this.isAnimating || tabId === this.activeTab) return;
this.isAnimating = true;
const targetTab = document.getElementById(tabId);
const currentTab = this.activeTab ?
document.getElementById(this.activeTab) : null;
// Update button states
this.updateButtonStates(tabId);
// Perform smooth transition
await this.animateTabTransition(currentTab, targetTab);
this.activeTab = tabId;
this.isAnimating = false;
// Dispatch custom event
this.dispatchTabChangeEvent(tabId);
}
updateButtonStates(tabId) {
Array.from(this.buttons).forEach(button => {
const isActive = button.getAttribute('data-tab') === tabId;
button.classList.toggle('active', isActive);
button.setAttribute('aria-selected', isActive);
});
}
async animateTabTransition(currentTab, targetTab) {
// Define animation properties
const timing = {
duration: 600,
easing: 'cubic-bezier(0.4, 0, 0.2, 1)'
};
// Animate out current tab if it exists
if (currentTab) {
await currentTab.animate([
{ opacity: 1, transform: 'translateY(0)' },
{ opacity: 0, transform: 'translateY(20px)' }
], timing).finished;
currentTab.style.display = 'none';
}
// Animate in new tab
targetTab.style.display = 'block';
targetTab.style.opacity = '0';
await targetTab.animate([
{ opacity: 0, transform: 'translateY(-20px)' },
{ opacity: 1, transform: 'translateY(0)' }
], timing).finished;
targetTab.style.opacity = '1';
}
handleButtonHover(event) {
const button = event.target;
if (!button.classList.contains('active')) {
gsap.to(button, {
scale: 1.05,
duration: 0.3,
ease: 'power2.out'
});
}
}
handleButtonLeave(event) {
const button = event.target;
gsap.to(button, {
scale: 1,
duration: 0.3,
ease: 'power2.out'
});
}
handleKeyboardNavigation(key) {
const currentIndex = Array.from(this.buttons).findIndex(
button => button.classList.contains('active')
);
let nextIndex;
if (key === 'ArrowRight') {
nextIndex = (currentIndex + 1) % this.buttons.length;
} else {
nextIndex = (currentIndex - 1 + this.buttons.length) % this.buttons.length;
}
const nextTabId = this.buttons[nextIndex].getAttribute('data-tab');
this.switchTab(nextTabId);
}
dispatchTabChangeEvent(tabId) {
const event = new CustomEvent('tabChanged', {
detail: { tabId, timestamp: Date.now() }
});
document.dispatchEvent(event);
}
}
// Initialize tabs when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
const tabNavigator = new TabNavigator();
tabNavigator.switchTab('home');
// Optional: Add tab change listener
document.addEventListener('tabChanged', (e) => {
console.log(`Tab changed to: ${e.detail.tabId} at ${new Date(e.detail.timestamp).toLocaleTimeString()}`);
});
});
// Add smooth scroll to tabs if they're below the fold
document.querySelectorAll('.tab-button').forEach(button => {
button.addEventListener('click', () => {
const tabsContainer = document.querySelector('.tabs-container');
if (tabsContainer) {
const containerTop = tabsContainer.getBoundingClientRect().top + window.pageYOffset;
if (containerTop < window.pageYOffset) {
window.scrollTo({
top: containerTop - 100, // Adjust offset as needed
behavior: 'smooth'
});
}
}
});
});