-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslider.html
More file actions
123 lines (116 loc) · 3.77 KB
/
Copy pathslider.html
File metadata and controls
123 lines (116 loc) · 3.77 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
margin: 0;
}
.slider{
width: 100%;
position: relative;
}
.slider_item {
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
color: #fff;
position: absolute;
top: 0;
z-index: 0;
opacity: 0;
transition: all .5s ease-in-out;
transform: scale(0.9);
}
.slider_item:nth-child(odd){
background-color: #8e44ad;
}
.slider_item:nth-child(even){
background-color: #3498db;
}
.showing {
opacity: 1;
z-index: 1;
transform: none;
}
.arrow {
height: 100vh;
margin: 0 auto;
width: 70%;
z-index: 99;
position: relative;
display: flex;
justify-content: space-between;
align-items: center;
font-size: 3rem;
color: #fff;
cursor: pointer;
}
.arrow>span:hover{
color: #5ac0e9;
}
</style>
</head>
<body>
<div id="slider">
<div class="slider_item showing"><h1>1</h1></div>
<div class="slider_item"><h1>2</h1></div>
<div class="slider_item"><h1>3</h1></div>
<div class="slider_item"><h1>4</h1></div>
<div class="slider_item"><h1>5</h1></div>
</div>
<div class="arrow">
<span><</span>
<span>></span>
</div>
<script>
const SHOWING_CLASS = "showing";
const firstSlider = document.querySelector(".slider_item:first-child");
const lastSlider = document.querySelector(".slider_item:last-child");
const rightArrow = document.querySelector(".arrow span:last-child");
const leftArrow = document.querySelector(".arrow span:first-child");
function rightArrowClick() {
const currentSlide = document.querySelector(`.${SHOWING_CLASS}`);
currentSlide.classList.remove(SHOWING_CLASS);
const nextSlide = currentSlide.nextElementSibling;
if(nextSlide){
nextSlide.classList.add(SHOWING_CLASS);
} else {
firstSlider.classList.add(SHOWING_CLASS);
}
}
function leftArrowClick() {
const currentSlide = document.querySelector(`.${SHOWING_CLASS}`);
currentSlide.classList.remove(SHOWING_CLASS);
const previousSlide = currentSlide.previousElementSibling;
if(previousSlide){
previousSlide.classList.add(SHOWING_CLASS);
} else {
lastSlider.classList.add(SHOWING_CLASS);
}
}
function slide() {
const currentSlide = document.querySelector(`.${SHOWING_CLASS}`);
if (currentSlide) {
currentSlide.classList.remove(SHOWING_CLASS);
const nextSlide = currentSlide.nextElementSibling;
if(nextSlide){
nextSlide.classList.add(SHOWING_CLASS);
} else {
firstSlider.classList.add(SHOWING_CLASS);
}
} else {
firstSlider.classList.add(SHOWING_CLASS);
}
}
setInterval(slide, 5000);
rightArrow.addEventListener("click", rightArrowClick);
leftArrow.addEventListener("click", leftArrowClick);
</script>
</body>
</html>