Html:
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>Carouseltitle>
<script src="js/Carousel.js">script>
<style>style>
head>
<body>
<script>
var arr = ["img/left.png", "img/right.png"];
var arr1 = [
"img/a.png",
"img/b.png",
"img/c.png",
"img/d.png",
"img/e.png"
];
var arr2 = [
"img/1.jpg",
"img/2.jpg",
"img/3.jpg",
"img/4.jpg",
"img/5.jpg",
"img/6.jpg",
"img/7.jpg"
];
var arr3 = ["img/aaa.jpg", "img/bbb.jpg", "img/ccc.jpg"];
var carousel = new Carousel(document.body, arr1, arr);
var carousel2 = new Carousel(document.body, arr2, arr);
var carousel3 = new Carousel(document.body, arr3, arr);
carousel.autoPlay = true;
carousel2.autoPlay = true;
carousel3.autoPlay = true;
animate();
function animate() {
requestAnimationFrame(animate);
carousel.animate();
carousel2.animate();
carousel3.animate();
}
script>
body>
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
Js:
var Carousel = (function () {
var liStyle = {
width: "20px",
height: "20px",
borderRadius: "20px",
backgroundColor: "rgba(240,137,137,0.3)",
border: "1px solid #F08989",
float: "left",
lineHeight: "20px",
textAlign: "center",
marginLeft: "20px",
color: "white"
};
var ulStyle = {
margin: 0,
padding: 0,
listStyle: "none",
position: "absolute",
bottom: "20px"
};
var imgConStyle = {
position: "absolute",
left: "0px"
};
var maskDivStyle = {
overflow: "hidden",
position: "relative",
margin: "auto",
backgroundColor: "antiquewhite"
};
function Carousel(parent, list, btnList) {
this.init(parent, btnList);
this.sourse = list;
}
Carousel.prototype = {
loadImg: null,
imgList: [],
carouselBox: null,
_width: 0,
_height: 0,
_sourse: [],
bool: false,
index: 0,
direct: "",
speed: 30,
autoPlay: false,
dotLi: null,
autoTime: 200,
set sourse(value) {
if (!value || !Array.isArray(value) || !value.length) return;
this.widthValue = 0;
this.heightValue = 0;
if (this.loadImg) {
this.loadImg.removeEventListener("load", this.loadImgHandler);
}
this.imgList.length = 0;
this._sourse = value;
this.reLoadImg(value);
},
get sourse() {
return this._sourse;
},
set widthValue(value) {
this._width = value;
if (this._width === 0) return;
this.carouselBox.style.width = value + "px";
var ul = this.carouselBox.lastElementChild;
ul.style.left = (value - ul.offsetWidth) / 2 + "px";
},
get widthValue() {
return this._width;
},
set heightValue(value) {
this._height = value;
if (this._height === 0) return;
this.carouselBox.style.height = value + "px";
this.carouselBox.firstElementChild.style.height = value + "px";
this.carouselBox.children[1].style.top = this.carouselBox.children[2].style.top = (this.heightValue - this.carouselBox.children[1].offsetHeight) / 2 + "px";
},
get heightValue() {
return this._height;
},
reLoadImg: function (list) {
var img = new Image();
img.addEventListener("load", this.loadImgHandler);
img.self = this;
img.num = 0;
img.list = list;
img.imgList = [];
img.src = list[img.num];
},
loadImgHandler: function (e) {
this.imgList.push(this.cloneNode(false));
this.num++;
if (this.num > this.list.length - 1) {
this.self.imgLoadFinish(this.imgList);
return;
}
this.src = this.list[this.num];
},
init: function (parent, btnList) {
if (!this.carouselBox) {
this.carouselBox = document.createElement("div");
var imgBox = document.createElement("div");
this.carouselBox.appendChild(imgBox);
for (var i = 0; i < btnList.length; i++) {
var btimg = new Image();
btimg.src = btnList[i];
this.carouselBox.appendChild(btimg);
if (i === 0) {
btimg.style.left = "10px";
btimg.style.position = "absolute";
} else {
btimg.style.right = "10px";
btimg.style.position = "absolute";
}
btimg.self = this;
this.carouselBox.self = this;
this.carouselBox.addEventListener("mouseenter", this.isAuto);
this.carouselBox.addEventListener("mouseleave", this.isAuto);
btimg.addEventListener("click", this.changeImgHandler);
}
var ul = document.createElement("ul");
Object.assign(ul.style, ulStyle);
Object.assign(this.carouselBox.style, maskDivStyle);
Object.assign(imgBox.style, imgConStyle);
this.carouselBox.appendChild(ul);
parent.appendChild(this.carouselBox);
}
return this.carouselBox;
},
imgLoadFinish: function (imgList) {
this.imgList = imgList;
var imgCon = this.carouselBox.firstElementChild;
var ul = this.carouselBox.lastElementChild;
this.clearNode(imgCon);
this.clearNode(ul);
for (var i = 0; i < this.imgList.length; i++) {
var li = document.createElement("li");
ul.appendChild(li);
Object.assign(li.style, liStyle);
}
ul.style.left = (this.widthValue - ul.offsetWidth) / 2 + "px";
imgCon.appendChild(this.imgList[0]);
ul.self = this;
ul.addEventListener("click", this.changeDot);
if (this.widthValue === 0) {
this.widthValue = this.imgList[0].width;
} else {
this.carouselBox.style.width = this.widthValue + "px";
this.setSize(imgCon, this.widthValue, 0);
}
if (this.heightValue === 0) {
this.heightValue = this.imgList[0].height;
} else {
this.carouselBox.style.height = this.heightValue + "px";
imgCon.firstElementChild.style.height = this.heightValue + "px";
this.setSize(imgCon, 0, this.heightValue);
}
this.dotColor();
this.carouselBox.children[1].style.top = this.carouselBox.children[2].style.top = (this.heightValue - this.carouselBox.children[1].offsetHeight) / 2 + "px";
},
setSize: function (con, w, h) {
for (var i = 0; i < con.children.length; i++) {
if (w) {
con.children[i].style.width = w + "px";
}
if (h) {
con.children[i].style.height = h + "px";
}
}
},
clearNode: function (node) {
var len = node.children.length;
for (var i = 0; i < len; i++) {
node.firstElementChild.remove();
}
},
changeImgHandler: function (e) {
if (this.self.bool) return;
if (this.style.left === "10px") {
this.self.direct = "right";
this.self.index--;
if (this.self.index < 0) {
this.self.index = this.self.imgList.length - 1;
}
} else if (this.style.right === "10px") {
this.self.direct = "left";
this.self.index++;
if (this.self.index > this.self.imgList.length - 1) {
this.self.index = 0;
}
}
this.self.creatNextPic();
},
creatNextPic: function () {
if (this.direct !== "left" && this.direct !== "right") return;
var imgCount = this.carouselBox.firstElementChild;
if (this.direct === "left") {
imgCount.appendChild(this.imgList[this.index]);
imgCount.style.width = this.widthValue * 2 + "px";
imgCount.style.left = 0;
} else {
imgCount.insertBefore(this.imgList[this.index], imgCount.firstElementChild);
imgCount.style.width = this.widthValue * 2 + "px";
imgCount.style.left = -this.widthValue + "px";
}
if (!this.widthValue) {
this.imgList[this.index].style.width = this.widthValue + "px";
}
if (!this.heightValue) {
this.imgList[this.index].style.height = this.heightValue + "px";
}
this.dotColor();
this.bool = true;
},
movePic: function () {
if (!this.bool) return;
if (this.direct !== "left" && this.direct !== "right") {
this.bool = false;
return;
}
var imgCount = this.carouselBox.firstElementChild;
if (this.direct === "left") {
imgCount.style.left = imgCount.offsetLeft - this.speed + "px";
if (imgCount.offsetLeft <= -this.widthValue) {
imgCount.firstElementChild.remove();
imgCount.style.left = 0 + "px";
this.direct = "";
this.bool = false;
}
} else {
imgCount.style.left = imgCount.offsetLeft + this.speed + "px";
if (imgCount.offsetLeft >= 0) {
imgCount.lastElementChild.remove();
imgCount.style.left = 0 + "px";
this.direct = "";
this.bool = false;
}
}
},
changeDot: function (e) {
if (this.self.bool) return;
if (e.target instanceof HTMLUListElement) return;
var arr = Array.from(this.children);
var count = arr.indexOf(e.target);
if (count === this.self.index) return;
if (count > this.self.index) {
this.self.direct = "left";
} else if (count < this.self.index) {
this.self.direct = "right";
}
this.self.index = count;
this.self.creatNextPic();
this.self.dotColor();
},
dotColor: function () {
if (this.dotLi) {
this.dotLi.style.backgroundColor = "rgba(240,137,137,0.3)";
}
this.dotLi = this.carouselBox.lastElementChild.children[this.index];
this.dotLi.style.backgroundColor = "rgba(240,137,137,1)";
},
isAuto: function (e) {
if (e.type === "mouseenter") {
this.self.autoPlay = false;
} else {
this.self.autoPlay = true;
}
},
carouselAuto: function () {
if (!this.autoPlay) return;
this.autoTime--;
if (this.autoTime > 0) return;
this.autoTime = 200;
this.direct = "left";
this.index++;
if (this.index > this.imgList.length - 1) {
this.index = 0;
}
this.creatNextPic();
},
animate: function () {
this.movePic();
this.carouselAuto();
}
};
Carousel.prototype.constructor = Carousel;
return Carousel;
})();
- 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
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291