-
Notifications
You must be signed in to change notification settings - Fork 213
/
carousal.html
228 lines (206 loc) · 6.82 KB
/
carousal.html
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
224
225
226
227
228
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>轮播图</title>
<style>
.music-carousal {
height: 250px;
position: relative;
margin-top: 30px;
}
.banner-list {
position: absolute;
width: 460px;
height: 250px;
left: 50%;
margin-left: -230px;
z-index: 3;
}
.banner-list .banner-item {
position: absolute;
width: 100%;
height: 100%;
transition: all 0.5s ease;
}
.banner-list .banner-item-out {
transform: rotateY(0) translateX(0) translateZ(0) scale(0.7);
opacity: 0;
z-index: -1;
}
.banner-list .banner-item-current {
transform: rotateY(0) translateX(0) translateZ(0);
z-index: 5;
}
.banner-list .banner-item-prev {
z-index: 3;
transform: rotateY(30deg) translate3d(-140px, -15px, -200px) scale(0.7);
opacity: 0.7;
}
.banner-list .banner-item-next {
z-index: 3;
transform: rotateY(-30deg) translate3d(140px, -15px, -200px) scale(0.7);
opacity: 0.7;
}
.banner-list .banner-item img {
width: 100%;
box-shadow: 0 8px 10px 0 rgba(88, 10, 0, 0.2);
border-radius: 3px;
}
.pagination-box {
position: absolute;
bottom: 50px;
left: 50%;
transform: translateX(-50%);
z-index: 5;
}
.pagination-box .pagination-item {
width: 8px;
height: 8px;
background: #e2e2e3;
border-radius: 50%;
display: inline-block;
margin: 0 3px;
}
.pagination-box .pagination-item.active {
background: #d71417;
}
</style>
</head>
<body>
<div class="music-carousal"></div>
<script>
let currentIndex = 1; // 当前是在第几个
let timer;
const carousal = {
init: function(object) {
const { el, banners, interval } = object;
// banner至少为3个
const length = banners.length;
if (length === 1) {
banners.push(banners[0]);
banners.push(banners[0]);
}
if (length === 2) {
banners.push(banners[0]);
}
this._render(el, banners);
timer = setInterval(this._next.bind(this), interval);
this.listenPaginationTouch.apply(this);
},
_render: function(el, banners) {
const imgUl = document.createElement("div");
imgUl.className = "banner-list";
let imgUlInnerHTML = "";
const paginationBox = document.createElement("div");
paginationBox.className = "pagination-box";
let paginationBoxInnerHTML = "";
banners.forEach((item, index) => {
imgUlInnerHTML += `<div class="banner-item ${
index === 0 ? "banner-item-prev" : ""
} ${index === 1 ? "banner-item-current" : ""} ${
index === 2 ? "banner-item-next" : ""
} ${index > 2 ? "banner-item-out" : ""}"><img src="${
item.imageUrl
}"></div>`;
paginationBoxInnerHTML += `<div class="pagination-item index-${index} ${
index === currentIndex ? "active" : ""
}"></div>`;
});
imgUl.innerHTML = imgUlInnerHTML;
paginationBox.innerHTML = paginationBoxInnerHTML;
el.appendChild(imgUl);
el.appendChild(paginationBox);
},
// 右边的那个图片变成当前的大图
_next: function() {
let bannerList = document.querySelectorAll(".banner-item");
let length = bannerList.length;
currentIndex = (currentIndex + 1) % length;
this._changeNextOrder(currentIndex);
this._renderPagination(currentIndex);
},
// 向右交换顺序
_changeNextOrder: function(_currentIndex) {
let bannerList = document.querySelectorAll(".banner-item");
let length = bannerList.length;
bannerList.forEach((item, index) => {
if (index === _currentIndex) {
item.className = "banner-item banner-item-current";
} else if (index === (_currentIndex - 1 + length) % length) {
item.className = "banner-item banner-item-prev";
} else if (index === (_currentIndex + 1) % length) {
item.className = "banner-item banner-item-next";
} else {
item.className = "banner-item banner-item-out";
}
});
},
_renderPagination: function(_currentIndex) {
let paginationList = document.querySelectorAll(".pagination-item");
paginationList.forEach((item, index) => {
if (index === _currentIndex) {
item.classList.add("active");
} else {
item.classList.remove("active");
}
});
},
// 监听小圆点按钮点击
listenPaginationTouch: function() {
let paginationBox = document.querySelector(".pagination-box");
paginationBox.addEventListener("click", event => {
const target = event.target;
if (target.classList.contains("pagination-item")) {
try {
let className = target.classList[1];
let index = Number(className.split("-")[1]);
this._changeNextOrder(index);
this._renderPagination(index);
currentIndex = index;
clearInterval(timer);
timer = setInterval(this._next.bind(this), 5000);
} catch (error) {
throw new Error(error);
}
}
});
}
};
const carousalDom = document.querySelector(".music-carousal");
const banners = [
{
imageUrl:
"http://p1.music.126.net/nHJXJZVMsSpi3v1ee8B8XA==/109951163797188493.jpg"
},
{
imageUrl:
"http://p1.music.126.net/iCrkUC8x7ViQwLULtgqkqw==/109951163797194143.jpg"
},
{
imageUrl:
"http://p1.music.126.net/2Jih8vXjnTeIzxtPU-gQiw==/109951163797164224.jpg"
},
{
imageUrl:
"http://p1.music.126.net/c1OhLIjnd9zB8NBUc3Olqg==/109951163797172654.jpg"
},
{
imageUrl:
"http://p1.music.126.net/SWjJnQTGa6vhBOzKrkR6wA==/109951163797179161.jpg"
},
{
imageUrl:
"http://p1.music.126.net/Wa9ENrBphVjH46A6Kt8yiA==/109951163797159700.jpg"
}
];
carousal.init({
el: carousalDom,
banners,
interval: 5000
});
</script>
</body>
</html>