Js实现:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
* {
margin: 0;
padding: 0;
text-decoration: none;
}
body {
padding: 20px;
}
.container {
position: relative;
width: 600px;
height: 400px;
border: 3px solid #333;
overflow: hidden;
}
.list {
position: absolute;
z-index: 1;
width: 4200px;
height: 400px;
}
.list img {
float: left;
width: 600px;
height: 400px;
}
.buttons {
position: absolute;
left: 250px;
bottom: 20px;
z-index: 2;
height: 10px;
width: 100px;
}
.buttons span {
float: left;
margin-right: 5px;
width: 10px;
height: 10px;
border: 1px solid #fff;
border-radius: 50%;
background: #333;
cursor: pointer;
}
.buttons .on {
background: orangered;
}
.arrow {
position: absolute;
top: 180px;
z-index: 2;
display: none;
width: 40px;
height: 40px;
font-size: 36px;
font-weight: bold;
line-height: 39px;
text-align: center;
color: #fff;
background-color: RGBA(0, 0, 0, .3);
cursor: pointer;
}
.arrow:hover {
background-color: RGBA(0, 0, 0, .7);
}
.container:hover .arrow {
display: block;
}
.prev {
left: 20px;
}
.next {
right: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="list" style="left: -600px;">
<img src="images/img_5.jpg" class="img-active-enter" alt="1" />
<img src="images/img_1.jpg" alt="1" />
<img src="images/img_2.jpg" alt="2" />
<img class="img-active-leave" src="images/img_3.jpg" alt="3" />
<img src="images/img_4.jpg" alt="4" />
<img src="images/img_5.jpg" alt="5" />
<img src="images/img_1.jpg" alt="5" />
</div>
<div class="buttons">
<span index="1" class="on"></span>
<span index="2"></span>
<span index="3"></span>
<span index="4"></span>
<span index="5"></span>
</div>
<a href="javascript:;" class="prev arrow"><</a>
<a href="javascript:;" class="next arrow">></a>
</div>
<script type="text/javascript">
window.onload = function() {
var container = document.getElementsByClassName(‘container‘)[0];
var list = document.getElementsByClassName(‘list‘)[0];
var prev = document.getElementsByClassName(‘prev‘)[0];
var next = document.getElementsByClassName(‘next‘)[0];
var buttons = document.getElementsByClassName(‘buttons‘)[0].getElementsByTagName(‘span‘);
var index = 1;
// 暂停标识
var stopFlag = false;
function buttonsShow() {
for (var i=0; i< buttons.length; i++) {
if (buttons[i].className === ‘on‘) {
buttons[i].className = ‘‘;
}
}
buttons[index -1].className = ‘on‘;
}
prev.onclick = function () {
if (stopFlag) {
return;
}
index -= 1;
if (index < 1) {
index = 5
}
buttonsShow();
animate(600)
}
next.onclick = function () {
if (stopFlag) {
return;
}
index += 1;
if (index > 5) {
index = 1
}
buttonsShow();
animate(-600);
}
for (let i = 0; i< buttons.length; i++) {
buttons[i].onclick = function () {
var clickIndex = parseInt(this.getAttribute(‘index‘));
var offset = 600 * (index - clickIndex);
animate(offset);
index = clickIndex;
buttonsShow();
}
}
function animate(offset) {
//获取的是style.left,是相对左边获取距离,所以第一张图后style.left都为负值,
//且style.left获取的是字符串,需要用parseInt()取整转化为数字。
var newLeft = parseInt(list.style.left) + offset;
if(newLeft<-3000){
list.style.left = -3600 + ‘px‘;
list.style.transition = ‘left 1s‘;
stopFlag = true;
setTimeout(function(){
list.style.left= -600 + ‘px‘
list.style.transition=‘left 0s‘;
stopFlag = false;
},1000)
return;
}
if(newLeft>-600){
list.style.left = 0 + ‘px‘;
list.style.transition = ‘left 1s‘
stopFlag = true;
setTimeout(function(){
list.style.left=‘-3000px‘
list.style.transition=‘left 0s‘;
stopFlag = false;
},1000)
return;
}
list.style.left = newLeft + ‘px‘;
list.style.transition = ‘left 1s‘
}
// 创建定时器
var timer;
function play() {
timer = setInterval(function () {
next.onclick();
}, 1500)
}
function stop() {
clearInterval(timer)
}
play();
container.onmouseover = stop;
container.onmouseout = play;
}
</script>
</body>
</html>