-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
110 lines (86 loc) · 2.94 KB
/
Copy pathjavascript.js
File metadata and controls
110 lines (86 loc) · 2.94 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
const header = document.querySelector('.header');
const nav = document.querySelector('.nav');
const viewButton = document.querySelector('.btn');
document.addEventListener('scroll', function () {
header.classList.toggle('active', window.scrollY > 300);
});
window.addEventListener('scrollend', function () {
nav.classList.remove('active');
});
// 高亮脚本
document.addEventListener('scroll', function () {
const sections = ['#home', '#info', '#picture', '#sharlin'];
const navLinks = {
'#home': document.getElementById('nav-home'),
'#info': document.getElementById('nav-info'),
'#picture': document.getElementById('nav-picture'),
'#sharlin': document.getElementById('nav-sharlin')
};
sections.forEach(section => {
const element = document.querySelector(section);
if (element) {
const rect = element.getBoundingClientRect();
if (rect.top <= 100 && rect.bottom >= 100) {
// 先移除所有导航链接的active类
Object.values(navLinks).forEach(link => link.classList.remove('active'));
// 给当前对应的导航链接添加active类,实现高亮
navLinks[section].classList.add('active');
}
}
});
});
// 轮播图
const sliderWrapper = document.querySelector('.slider-wrapper');
const slides = document.querySelectorAll('.slide');
const prevButton = document.querySelector('.prev-button');
const nextButton = document.querySelector('.next-button');
const indicators = document.querySelectorAll('.indicator');
let currentIndex = 0;
const slideWidth = slides[0].offsetWidth;
let autoSlideInterval;
function goToSlide(index) {
sliderWrapper.style.transform = `translateX(-${index * slideWidth}px)`;
currentIndex = index;
updateIndicators();
}
function updateIndicators() {
indicators.forEach((indicator, i) => {
if (i === currentIndex) {
indicator.classList.add('active');
} else {
indicator.classList.remove('active');
}
});
}
prevButton.addEventListener('click', () => {
if (currentIndex > 0) {
goToSlide(currentIndex - 1);
resetAutoSlide();
}
});
nextButton.addEventListener('click', () => {
if (currentIndex < slides.length - 1) {
goToSlide(currentIndex + 1);
resetAutoSlide();
}
});
indicators.forEach((indicator, i) => {
indicator.addEventListener('click', () => {
goToSlide(i);
resetAutoSlide();
});
});
function startAutoSlide() {
autoSlideInterval = setInterval(() => {
if (currentIndex < slides.length - 1) {
goToSlide(currentIndex + 1);
} else {
goToSlide(0);
}
}, 3000);
}
function resetAutoSlide() {
clearInterval(autoSlideInterval);
startAutoSlide();
}
startAutoSlide();