-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
223 lines (190 loc) · 6.34 KB
/
script.js
File metadata and controls
223 lines (190 loc) · 6.34 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
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
219
220
221
222
223
// 语言切换功能
class LanguageSwitcher {
constructor() {
this.currentLanguage = 'zh';
this.init();
}
init() {
// 绑定语言切换按钮事件
document.getElementById('lang-zh').addEventListener('click', () => this.switchLanguage('zh'));
document.getElementById('lang-en').addEventListener('click', () => this.switchLanguage('en'));
// 初始化语言
this.updateContent();
}
switchLanguage(lang) {
this.currentLanguage = lang;
// 更新按钮状态
document.querySelectorAll('.lang-btn').forEach(btn => {
btn.classList.remove('active');
});
document.getElementById(`lang-${lang}`).classList.add('active');
// 更新页面内容
this.updateContent();
// 更新页面语言属性
document.documentElement.lang = lang === 'zh' ? 'zh-CN' : 'en';
}
updateContent() {
const elements = document.querySelectorAll('[data-zh][data-en]');
elements.forEach(element => {
const text = element.getAttribute(`data-${this.currentLanguage}`);
if (text) {
element.textContent = text;
}
});
}
}
// 功能幻灯片控制
class FeatureSlider {
constructor() {
this.currentSlide = 0;
this.slides = document.querySelectorAll('.feature-slide');
this.dots = document.querySelectorAll('.dot');
this.totalSlides = this.slides.length;
this.init();
}
init() {
// 绑定控制按钮事件
document.querySelector('.prev-btn').addEventListener('click', () => this.prevSlide());
document.querySelector('.next-btn').addEventListener('click', () => this.nextSlide());
// 绑定圆点事件
this.dots.forEach((dot, index) => {
dot.addEventListener('click', () => this.goToSlide(index));
});
// 自动播放
this.startAutoPlay();
// 鼠标悬停时暂停自动播放
const slider = document.querySelector('.feature-slider');
slider.addEventListener('mouseenter', () => this.stopAutoPlay());
slider.addEventListener('mouseleave', () => this.startAutoPlay());
}
goToSlide(index) {
// 移除当前活动状态
this.slides[this.currentSlide].classList.remove('active');
this.dots[this.currentSlide].classList.remove('active');
// 设置新的活动状态
this.currentSlide = index;
this.slides[this.currentSlide].classList.add('active');
this.dots[this.currentSlide].classList.add('active');
}
nextSlide() {
const nextIndex = (this.currentSlide + 1) % this.totalSlides;
this.goToSlide(nextIndex);
}
prevSlide() {
const prevIndex = (this.currentSlide - 1 + this.totalSlides) % this.totalSlides;
this.goToSlide(prevIndex);
}
startAutoPlay() {
this.autoPlayInterval = setInterval(() => {
this.nextSlide();
}, 5000); // 每5秒切换一次
}
stopAutoPlay() {
if (this.autoPlayInterval) {
clearInterval(this.autoPlayInterval);
}
}
}
// 平滑滚动功能
class SmoothScroll {
constructor() {
this.init();
}
init() {
// 为所有内部链接添加平滑滚动
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', (e) => {
e.preventDefault();
const targetId = anchor.getAttribute('href').substring(1);
const targetElement = document.getElementById(targetId);
if (targetElement) {
targetElement.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
}
}
// 页面加载动画
class PageAnimations {
constructor() {
this.init();
}
init() {
// 监听滚动事件,为元素添加动画
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate-in');
}
});
}, {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
});
// 观察需要动画的元素
document.querySelectorAll('.step, .feature-slide').forEach(el => {
observer.observe(el);
});
}
}
// 添加动画样式
const animationStyles = `
.step, .feature-slide {
opacity: 0;
transform: translateY(30px);
transition: all 0.6s ease-out;
}
.step.animate-in, .feature-slide.animate-in {
opacity: 1;
transform: translateY(0);
}
`;
// 将动画样式添加到页面
const styleSheet = document.createElement('style');
styleSheet.textContent = animationStyles;
document.head.appendChild(styleSheet);
// 页面加载完成后初始化所有功能
document.addEventListener('DOMContentLoaded', () => {
new LanguageSwitcher();
new FeatureSlider();
new SmoothScroll();
new PageAnimations();
// 添加页面加载动画
document.body.style.opacity = '0';
setTimeout(() => {
document.body.style.transition = 'opacity 0.5s ease-in';
document.body.style.opacity = '1';
}, 100);
});
// 添加键盘导航支持
document.addEventListener('keydown', (e) => {
const slider = document.querySelector('.feature-slider');
if (!slider) return;
switch(e.key) {
case 'ArrowLeft':
if (e.target.closest('.feature-slider')) {
document.querySelector('.prev-btn').click();
}
break;
case 'ArrowRight':
if (e.target.closest('.feature-slider')) {
document.querySelector('.next-btn').click();
}
break;
}
});
// 响应式处理
function handleResize() {
// 在移动设备上调整某些功能
if (window.innerWidth <= 768) {
// 移动设备上的特殊处理
document.body.classList.add('mobile');
} else {
document.body.classList.remove('mobile');
}
}
window.addEventListener('resize', handleResize);
window.addEventListener('load', handleResize);