<!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>Document</title>
<style>
* {
padding: 0;
margin: 0;
border: 0;
list-style: none
}
.all {
width: 600px;
height: 400px;
padding: 6px;
margin: 200px auto;
position: relative;
border: 1px solid #ccc;
}
.screen {
width: 600px;
height: 400px;
overflow: hidden;
position: relative;
}
.screen li {
width: 600px;
height: 400px;
overflow: hidden;
float: left;
}
.screen ul {
position: absolute;
left: 0;
top: 0px;
width: 3600px;
}
.all ol {
position: absolute;
text-align: center;
right: 10px;
bottom: 10px;
line-height: 30px;
}
.all ol li {
float: left;
width: 30px;
height: 30px;
background-color: #fff;
border: 1px solid #ccc;
margin-left: 15px;
cursor: pointer;
}
.all ol li.current {
background-color: pink;
}
#arr {
display: none;
z-index: 1000;
}
#arr span {
width: 50px;
height: 50px;
position: absolute;
left: 5px;
top: 50%;
margin-top: -25px;
background: #000;
cursor: pointer;
line-height: 50px;
text-align: center;
font-size: 30px;
color: #fff;
opacity: 0.3;
border: 1px solid #ccc;
}
#arr #right {
right: 5px;
left: auto;
}
</style>
</head>
<body>
<div class='all' id="box">
<div class="screen">
<ul>
<li><img src="images/wf1.jpg" width='600' height='400'></li>
<li><img src="images/wf2.jpg" width='600' height='400'></li>
<li><img src="images/wf3.jpg" width='600' height='400'></li>
<li><img src="images/wf4.jpg" width='600' height='400'></li>
<li><img src="images/wf5.jpg" width='600' height='400'></li>
</ul>
<ol></ol>
</div>
<div id="arr"><span id='left'><</span><span id='right'>></span></div>
</div>
<script>
var box = document.getElementById('box');
var screen = box.children[0];
var ul = screen.children[0];
var ol = screen.children[1];
var arr = document.getElementById('arr');
var arrLeft = document.getElementById('left');
var arrRight = document.getElementById('right')
var imgWidth = screen.offsetWidth;
var count = ul.children.length;
for (var i = 0; i < count; i++) {
var li = document.createElement('li');
ol.appendChild(li);
setInnerText(li, i + 1);
li.onclick = liClick;
li.setAttribute('index', i)
}
function liClick() {
for (var i = 0; i < ol.children.length; i++) {
var li = ol.children[i];
li.className = '';
}
this.className = 'current';
var liIndex = parseInt(this.getAttribute('index'));
animate(ul, -liIndex * imgWidth)
}
ol.children[0].className = 'current';
box.onmouseenter = function() {
arr.style.display = 'block';
}
box.onmouseleave = function() {
arr.style.display = 'none';
}
var index = 0;
arrRight.onclick = function() {
if (index === count) {
ul.style.left = '0px';
index = 0;
}
index++;
if (index < count) {
ol.children[index].click()
} else {
animate(ul, -index * imgWidth);
for (var i = 0; i < ol.children.length; i++) {
var li = ol.children[i];
li.className = '';
}
ol.children[0].className = 'current';
}
}
arrLeft.onclick = function() {
if (index === 0) {
index = count;
ul.style.left = -index * imgWidth + 'px';
}
index--;
ol.children[index].click()
}
var firstLi = ul.children[0];
var cloneLi = firstLi.cloneNode(true);
ul.appendChild(cloneLi)
function animate(element, target) {
if (element.timeId) {
clearInterval(element.timeId);
element.timeId = null;
}
element.timeId = setInterval(function() {
var step = 10;
var current = element.offsetLeft;
if (current > target) {
step = -Math.abs(step);
}
if (Math.abs(current - target) <= Math.abs(step)) {
clearInterval(element.timeId);
element.style.left = target + 'px';
return;
}
current += step;
element.style.left = current + 'px'
}, 10)
}
function setInnerText(element, content) {
if (typeof element.innerText === 'string') {
element.innerText = content;
} else {
element.textContent = content;
}
}
</script>
</body>
</html>