![在这里插入图片描述](https://img-blog.csdnimg.cn/bd9660b064434dec97bf765623fad632.png#pic_center)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div class="wrapper"></div>
<div class="deom"></div>
<script src="./jquery-3.3.1.js"></script>
<script src="./demo.js"></script>
<script>
$('.wrapper').swiper({
animationType: 'fade',
showSpot: true,
imgList: [
{
href: '#1',
src: './image/1.png',
},
{
href: '#2',
src: './image/2.png',
},
{
href: '#3',
src: './image/3.png',
},
{
href: '#4',
src: './image/4.png',
},
],
imgBtn: true,
time: 1000,
imgWidth: 350,
imgHeight: 300,
isAuto: true,
})
$('.deom').swiper({
animationType: 'animate',
showSpot: true,
imgList: [
{
href: '#1',
src: './image/pic1.jpg',
},
{
href: '#2',
src: './image/pic2.jpg',
},
{
href: '#3',
src: './image/pic3.jpg',
},
],
imgBtn: true,
time: 1000,
imgWidth: 350,
imgHeight: 300,
isAuto: true,
})
</script>
</body>
</html>
(function () {
function Swiper(options, wrap) {
this.wrap = wrap;
this.isAuto = options.isAuto;
this.animationType = options.animationType || "fade";
this.imgList = options.imgList || [];
this.imgBtn = options.imgBtn == undefined ? true : options.imgBtn;
this.showSpot = options.showSpot == undefined ? true : options.showSpot;
this.time = options.time || 2000;
this.imgWidth = options.imgWidth || $(this.wrap).width();
this.imgHeight = options.imgHeight || $(this.wrap).height();
this.timer = null;
this.nowIndex = 0;
this.lock = false;
this.imgLength = this.imgList.length;
this.init = function () {
this.createDom();
this.initCss();
this.bindEvent();
if (this.isAuto) {
this.AutoChange();
}
};
}
Swiper.prototype.createDom = function () {
var swiperDiv = $('<div class="my-swiper"></div>');
var imgUl = $('<ul class="img-list"></ul>');
var spotDiv = $('<div class="spot"></div>');
this.imgList.forEach(function (item) {
$(
'<li><a href="' +
item.href +
'"><img src="' +
item.src +
'"></img></a></li>'
).appendTo(imgUl);
$("<span></span>").appendTo(spotDiv);
});
imgUl.find("li").eq(0).clone().appendTo(imgUl);
imgUl.appendTo(swiperDiv);
if (this.imgBtn) {
$(swiperDiv)
.append($('<div class="left-btn btn"><</div>'))
.append($('<div class="right-btn btn">></div>'));
}
if (this.showSpot) {
swiperDiv.append(spotDiv);
}
$(this.wrap).append(swiperDiv);
};
Swiper.prototype.initCss = function () {
$(".my-swiper", this.wrap).css({
width: this.imgWidth,
height: this.imgHeight,
position: "relative",
});
$(".my-swiper *", this.wrap).css({
listStyle: "none",
padding: 0,
margin: 0,
});
$("a", this.wrap).css({
display: "inline-block",
width: "100%",
height: "100%",
});
$("img", this.wrap).css({
width: "100%",
height: "100%",
});
if (this.animationType == "fade") {
$(".img-list > li", this.wrap)
.css({
position: "absolute",
width: this.imgWidth,
height: this.imgHeight,
display: "none",
})
.eq(this.nowIndex)
.css("display", "block");
} else if ((this.animationType = "animate")) {
$(this.wrap).css({
position: "relative",
overflow: "hidden",
width: this.imgWidth,
});
$(".img-list", this.wrap).css({
width: this.imgWidth * (this.imgLength + 1),
height: this.imgHeight,
position: "absolute",
});
$(".img-list > li", this.wrap).css({
float: "left",
width: this.imgWidth,
height: this.imgHeight,
});
}
$(".btn", this.wrap)
.css({
width: 25,
height: 35,
backgroundColor: "rgba(0, 0, 0, .5)",
color: "#fff",
position: "absolute",
top: "50%",
marginTop: -17.5,
lineHeight: "35px",
textAlign: "center",
cursor: "pointer",
})
.filter(".right-btn")
.css({
right: 0,
});
$(".spot", this.wrap).css({
position: "absolute",
bottom: 0,
width: "100%",
textAlign: "center",
});
$(".spot > span", this.wrap)
.css({
display: "inline-block",
width: 10,
height: 10,
margin: "10px 5px",
borderRadius: "50%",
backgroundColor: "#ddd",
cursor: "pointer",
})
.eq(this.nowIndex)
.css({
backgroundColor: "#fff",
});
};
Swiper.prototype.bindEvent = function () {
var self = this;
$(".left-btn", this.wrap).click(function () {
if (self.lock) {
return false;
}
self.lock = true;
if (self.nowIndex == 0) {
if (self.animationType == "animate") {
$(".img-list", self.wrap).css({
left: -self.imgWidth * self.imgLength,
});
}
self.nowIndex = self.imgLength - 1;
} else {
self.nowIndex--;
}
self.change();
});
$(".right-btn", this.wrap).click(function () {
if (self.lock) {
return false;
}
self.lock = true;
if (self.nowIndex == self.imgLength - 1 && self.animationType == "fade") {
self.nowIndex = 0;
} else {
if (self.nowIndex == self.imgLength) {
$(".img-list", self.wrap).css({
left: 0,
});
self.nowIndex = 1;
} else {
self.nowIndex++;
}
}
self.change();
});
$(".spot > span", this.wrap).click(function () {
if (self.lock) {
return false;
}
self.lock = true;
console.log($(this).index());
self.nowIndex = $(this).index();
self.change();
});
$(this.wrap)
.mouseenter(function () {
clearInterval(self.timer);
})
.mouseleave(function () {
if (self.isAuto) {
self.AutoChange();
}
});
};
Swiper.prototype.change = function () {
var self = this;
if (this.animationType == "fade") {
$(".img-list > li", this.wrap)
.fadeOut()
.eq(this.nowIndex)
.fadeIn(function () {
self.lock = false;
});
} else if (this.animationType == "animate") {
$(".img-list", this.wrap).animate(
{
left: -this.nowIndex * this.imgWidth,
},
function () {
self.lock = false;
}
);
}
$(".spot > span", this.wrap)
.css({
backgroundColor: "#ddd",
})
.eq(this.nowIndex % this.imgLength)
.css("backgroundColor", "#fff");
};
Swiper.prototype.AutoChange = function () {
var self = this;
this.timer = setInterval(function () {
$(".right-btn", self.wrap).click();
}, this.time);
};
$.fn.extend({
swiper: function (options) {
var obj = new Swiper(options, this);
obj.init();
},
});
})();