-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
80 lines (71 loc) · 2.56 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
// 轮播图功能
const carousel = document.querySelector('.carousel-container');
const prevBtn = document.querySelector('.prev');
const nextBtn = document.querySelector('.next');
const images = document.querySelectorAll('.carousel-container img');
let currentIndex = 0;
function showImage(index) {
// 确保 index 在有效范围内
index = (index + images.length) % images.length;
carousel.style.transition = 'transform 0.5s ease-in-out';
carousel.style.transform = `translateX(-${index * 100}%)`;
currentIndex = index;
}
function nextImage() {
showImage(currentIndex + 1);
}
function prevImage() {
showImage(currentIndex - 1);
}
prevBtn.addEventListener('click', prevImage);
nextBtn.addEventListener('click', nextImage);
// 处理循环
carousel.addEventListener('transitionend', () => {
if (currentIndex === images.length - 1) {
carousel.style.transition = 'none';
carousel.style.transform = 'translateX(0)';
currentIndex = 0;
setTimeout(() => {
carousel.style.transition = 'transform 0.5s ease-in-out';
}, 50);
}
if (currentIndex === -1) {
carousel.style.transition = 'none';
carousel.style.transform = `translateX(-${(images.length - 1) * 100}%)`;
currentIndex = images.length - 1;
setTimeout(() => {
carousel.style.transition = 'transform 0.5s ease-in-out';
}, 50);
}
});
// 自动轮播
setInterval(nextImage, 5000);
// 留言板功能
const form = document.getElementById('contact-form');
const formStatus = document.getElementById('form-status');
form.addEventListener('submit', function(e) {
e.preventDefault();
const formData = new FormData(form);
fetch(form.action, {
method: 'POST',
body: formData,
headers: {
'Accept': 'application/json'
}
}).then(response => {
if (response.ok) {
formStatus.innerHTML = "谢谢!您的留言已成功发送。";
form.reset();
} else {
response.json().then(data => {
if (Object.hasOwn(data, 'errors')) {
formStatus.innerHTML = data["errors"].map(error => error["message"]).join(", ");
} else {
formStatus.innerHTML = "糟糕!提交表单时出现问题。请稍后再试。";
}
})
}
}).catch(error => {
formStatus.innerHTML = "糟糕!提交表单时出现问题。请稍后再试。";
});
});