<style type="text/css">
* {
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
}
.warp {
width: 520px;
height: 280px;
overflow: hidden;
margin: 0 auto;
position: relative;
}
.imgBox {
width: 9999px;
}
.imgBox::after {
content: "";
display: block;
clear: both;
}
.imgBox li {
float: left;
width: 520px;
height: 280px;
}
.imgBox li img {
width: 100%;
}
.next,
.pre {
position: absolute;
width: 30px;
height: 30px;
border-radius: 50%;
background: rgba(0, 0, 0, 0.2);
color: #fff;
font-size: 20px;
text-align: center;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
}
.next:hover,
.pre:hover {
background: rgba(0, 0, 0, 0.3);
}
.next {
right: 0;
}
.pre {
left: 0;
}
.tran {
transition: 500ms;
}
.pager {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
}
.pager span {
display: inline-block;
width: 10px;
height: 10px;
border-radius: 50%;
background: #fff;
margin: 0 3px;
cursor: pointer;
}
.pager .active {
background: skyblue;
}
</style>
<div class="warp">
<ul class="imgBox ">
<li><img src="img/11.jpg" /></li>
<li><img src="img/33.jpg" /></li>
<li><img src="img/QQ图片20180809104057.jpg" /></li>
</ul>
<div class="next">></div>
<div class="pre"><</div>
<div class="pager">
</div>
</div>
<script type="text/javascript">
var warp = document.querySelector(".warp"),
imgBox = document.querySelector(".imgBox"),
next = document.querySelector(".next"),
pre = document.querySelector(".pre"),
ali = imgBox.querySelectorAll("li");
var imgW = ali[0].offsetWidth;
var index = 1;
imgBox.style.transform = "translateX(-" + imgW * index + "px)";
var isclick = true;
var liclone_first = ali[0].cloneNode(true);
var liclone_last = ali[ali.length - 1].cloneNode(true);
imgBox.appendChild(liclone_first);
imgBox.insertBefore(liclone_last, ali[0]);
next.addEventListener("click", function() {
if(isclick) {
index++;
imgBox.style.transform = "translateX(-" + imgW * index + "px)";
if(!imgBox.classList.contains("tran")) {
imgBox.classList.add("tran");
}
addColr(index);
}
isclick = false;
});
pre.addEventListener("click", function() {
if(isclick == true) {
index--;
if(!imgBox.classList.contains("tran")) {
imgBox.classList.add("tran");
}
imgBox.style.transform = "translateX(-" + imgW * index + "px)";
addColr(index);
}
isclick = false;
})
imgBox.addEventListener("transitionend", function() {
if(index == ali.length + 1) {
index = 1;
imgBox.style.transform = "translateX(-" + imgW * index + "px)";
imgBox.classList.remove("tran");
}
if(index == 0) {
index = ali.length;
imgBox.style.transform = "translateX(-" + imgW * index + "px)";
imgBox.classList.remove("tran");
}
isclick = true;
});
setInterval(function() {
if(isclick) {
index++;
imgBox.style.transform = "translateX(-" + imgW * index + "px)";
if(!imgBox.classList.contains("tran")) {
imgBox.classList.add("tran");
}
addColr(index);
}
isclick = false;
}, 1000)
var pager = document.querySelector(".pager");
for(var i = 0; i < ali.length; i++) {
var span = document.createElement("span");
pager.appendChild(span);
if(i == 0) {
span.classList.add("active");
}
}
var aSpan = pager.children;
for(var i = 0; i < aSpan.length; i++) {
(function(k) {
aSpan[k].addEventListener("click", function() {
if(!imgBox.classList.contains("tran")) {
imgBox.classList.add("tran");
}
imgBox.style.transform = "translateX(-" + imgW * (k + 1) + "px)";
for(var j = 0; j < aSpan.length; j++) {
aSpan[j].classList.remove("active");
}
aSpan[k].classList.add("active");
index = k + 1;
})
})(i);
}
function addColr(k) {
k = k - 1;
if(k > aSpan.length - 1) {
k = 0;
} else if(k < 0) {
k = 2;
}
for(var j = 0; j < aSpan.length; j++) {
aSpan[j].classList.remove("active");
}
aSpan[k].classList.add("active");
}
</script>