-
Notifications
You must be signed in to change notification settings - Fork 0
/
前端-第5周第1次-JS(4)-2022·2·15.js
76 lines (55 loc) · 2.01 KB
/
前端-第5周第1次-JS(4)-2022·2·15.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
// 当前幻灯片页:1-10
let current_page = 1;
// 页面点点击事件
function handlePointClick () {
const id = parseInt(this.dataset?.id);
if(!isNaN(id))
handleSlideshow(id);
// 更新自动切页
clearInterval(interval);
interval = setInterval(handleAutoSlide, UPDATE_TIME);
}
// 前后页切换
function handleSwitchPageClick() {
if(this.className === 'left')
handleSlideshow(current_page - 1);
if(this.className === 'right')
handleSlideshow(current_page + 1);
// 更新自动切页
clearInterval(interval);
interval = setInterval(handleAutoSlide, UPDATE_TIME);
}
// 根据id切页方法:1-9
function handleSlideshow (id) {
// 检测输入有效性
if(id === 0) {
id = 9;
document.getElementsByClassName('list')[0].style.transform = `translateX(-5400px)`;
}
// 切换图片
document.getElementsByClassName('list')[0].style.transform = `translateX(${-600 * (id - 1)}px)`;
// 检测输入有效性
if(id === 10) {
id = 1;
document.getElementsByClassName('list')[0].style.transform = `translateX(0px)`;
}
// 切换页面点
document.querySelectorAll('.points > li').forEach((v, i) => v.className = i === id - 1 ? 'point-active' : '');
// 更新页面
current_page = id;
}
// 换页自动切换
let interval = null;
const UPDATE_TIME = 2500000;
function handleAutoSlide () {
handleSlideshow(current_page + 1);
}
window.onload = function () {
// 添加页面点点击事件
document.querySelectorAll('.points > li').forEach(v => v.addEventListener('click', handlePointClick));
// 添加前后换页事件
document.getElementsByClassName('left')[0].addEventListener('click', handleSwitchPageClick);
document.getElementsByClassName('right')[0].addEventListener('click', handleSwitchPageClick);
// 添加定时轮换功能
interval = setInterval(handleAutoSlide, UPDATE_TIME);
};