<style>
* {
margin: 0;
padding: 0;
list-style: none;
border: 0;
}
img {
cursor: pointer;
width: 500px;
height: 200px;
}
.all {
width: 500px;
height: 200px;
padding: 7px;
border: 1px solid #cccccc;
margin: 100px auto;
position: relative;
}
.screen {
width: 500px;
height: 200px;
position: relative;
overflow: hidden;
}
.screen ul {
width: 3000px;
top: 0;
left: 0;
position: absolute;
}
.screen li {
width: 500px;
height: 200px;
float: left;
}
.screen ol {
position: absolute;
width: 140px;
height: 20px;
border-radius: 10px;
background: rgba(0, 0, 0, .6);
bottom: 15px;
left: 50%;
margin-left: -70px;
cursor: pointer;
}
.screen ol li {
width: 12px;
height: 12px;
background-color: #fff;
border-radius: 50%;
float: left;
margin: 4px 8px;
}
.screen ol .current {
background-color: #f10215;
}
#arr {
display: none;
}
#arr #left,
#arr #right {
position: absolute;
width: 30px;
height: 50px;
top: 50%;
margin-top: -25px;
background: rgba(0, 0, 0, .3);
font-size: 30px;
color: #ffffff;;
cursor: pointer;
text-align: center;
line-height: 50px;
}
#right {
right: 7px;
}
</style>
<div class="all" id='box'>
<div class="screen">
<ul>
<li><img src="img/01.jpg"/></li>
<li><img src="img/02.jpg"/></li>
<li><img src="img/03.jpg"/></li>
<li><img src="img/04.jpg"/></li>
<li><img src="img/05.jpg"/></li>
</ul>
<ol>
</ol>
</div>
<div id="arr">
<span id="left"><</span>
<span id="right">></span>
</div>
</div>
<script>
var box = my$("box")
var screen = box.children[0];
var imgWidth = screen.offsetWidth;
var ulObj = screen.children[0];
var list = ulObj.children;
var olObj = screen.children[1];
var arr = my$("arr");
var pic = 0;
for (var i = 0; i < list.length; i++) {
var liObj = document.createElement("li");
olObj.appendChild(liObj);
liObj.setAttribute("index", i);
liObj.onmouseover = function () {
for (var j = 0; j < olObj.children.length; j++) {
olObj.children[j].removeAttribute("class");
}
this.className = "current";
pic = this.getAttribute("index");
animate(ulObj, -pic * imgWidth);
};
}
olObj.children[0].className = "current";
var timeId = setInterval(clickHandle, 1000);
ulObj.appendChild(ulObj.children[0].cloneNode(true));
box.onmouseover = function () {
arr.style.display = "block";
clearInterval(timeId);
};
box.onmouseout = function () {
arr.style.display = "none";
timeId = setInterval(clickHandle, 1000);
};
my$("right").onclick = clickHandle;
function clickHandle() {
if (pic == list.length - 1) {
pic = 0;
ulObj.style.left = 0 + "px";
}
pic++;
animate(ulObj, -pic * imgWidth);
if (pic == list.length - 1) {
olObj.children[olObj.children.length - 1].className = "";
olObj.children[0].className = "current";
} else {
for (var i = 0; i < olObj.children.length; i++) {
olObj.children[i].className = "";
}
olObj.children[pic].className = "current";
}
};
my$("left").onclick = function () {
if (pic == 0) {
pic = list.length - 1;
ulObj.style.left = -pic * imgWidth + "px";
}
pic--;
console.log(pic);
animate(ulObj, -pic * imgWidth);
for (var i=0;i<olObj.children.length;i++) {
olObj.children[i].className = "";
}
olObj.children[pic].className = "current";
};
</script>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/536eca32a5dcc59b01ec95eceb85a07d.png)