<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
*{
padding: 0;
margin: 0;
}
.wrap{
width: 800px;
height: 400px;
position: relative;
}
.list{
width: 800px;
height: 400px;
list-style: none;
border: 1px solid yellow;
position: relative;
}
.item{
width: 100%;
height: 100%;
color: white;
font-size: 20px;
opacity: 0;
transition: all .8s;
position: absolute;
}
.item:nth-child(1){
background-color: black;
}
.item:nth-child(2){
background-color: red;
}
.item:nth-child(3){
background-color: yellow;
}
.item:nth-child(4){
background-color: green;
}
.item:nth-child(5){
background-color: pink;
}
.btn{
width: 50px;
height: 100px;
position: absolute;
top: 150px;
z-index: 100;
}
#getPre{
left: 0px;
}
#getNext{
right: 0px;
}
.item.active{
opacity: 1;
z-index: 10;
}
.pointList{
padding-left: 0px;
list-style: none;
z-index: 10000;
position: absolute;
right: 20px;
bottom: 20px;
}
.point{
width: 8px;
height: 8px;
background-color: rgba(0,0,0,0.4);
border-radius: 100%;
float: left;
margin-right: 14px;
border-style: solid;
border-width: 2px;
border-color: rgba(255,255,255,0.6);
cursor: pointer;
}
.point.active{
background-color: rgba(255, 255, 255, 0.4);
}
</style>
</head>
<body>
<div class="wrap" id="wrap">
<ul class="list">
<li class="item active">0</li>
<li class="item">1</li>
<li class="item">2</li>
<li class="item">3</li>
<li class="item">4</li>
</ul>
<ul class="pointList">
<li class="point active" data-index='0'></li>
<li class="point" data-index='1'></li>
<li class="point"data-index='2'></li>
<li class="point"data-index='3'></li>
<li class="point"data-index='4'></li>
</ul>
<button type="button" class="btn" id="getPre"><</button>
<button type="button" class="btn" id="getNext">></button>
</div>
<script>
var items = document.getElementsByClassName("item");
var goPreBtn = document.getElementById("getPre");
var goNextBtn = document.getElementById("getNext");
var points = document.getElementsByClassName("point");
var time=0;
var index = 0;
function clearAction(){
for(var i=0;i<items.length;i++){
items[i].className='item'
}
for(var i=0;i<points.length;i++){
points[i].className='point'
}
}
function goIndex(){
clearAction()
items[index].className = 'item active'
points[index].className = 'point active'
}
function goNext(){
if(index<items.length-1){
index ++;
}else{
index=0
}
time=0
goIndex();
}
function goPre(){
if(index==0){
index=items.length-1
}else{
index--;
}
time=0
goIndex();
}
goNextBtn.addEventListener('click',function(){
goNext();
})
goPreBtn.addEventListener('click',function(){
goPre();
})
for(var i=0;i<points.length;i++){
points[i].addEventListener('click',function(){
var pointIndex = this.getAttribute('data-index');
console.log(pointIndex)
index=pointIndex
goIndex()
time=0;
})
}
function showTime(){
time++;
if(time==2){
goNext();
time=0
}
}
var timer= setInterval(showTime,1000);
var inner = document.querySelector('#wrap');
inner.onmouseover = function () {
clearInterval(timer);
};
var inner = document.querySelector('#wrap');
inner.onmouseout = function () {
timer = setInterval(showTime,1000);
};
</script>
</body>
</html>