html
css
js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="css/css1.css"/>
</head>
<body>
<div id="container">
<div id="list" style="left: -600px;">
<img src="img/pic1.jpg" alt="1" />
<img src="img/pic2.jpg" alt="2" />
<img src="img/pic3.jpg" alt="3" />
<img src="img/pic4.jpg" alt="4" />
<img src="img/pic5.jpg" alt="5" />
<img src="img/pic1.jpg" alt="5" />
</div>
<a href="javascript:;" id="prev" class="arrow"><</a>
<a href="javascript:;" id="next" class="arrow">></a>
</div>
<script src="js/js1.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>
* {
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;
}
window.onload = function() {
var list = document.getElementById('list');//获取图片轴对象
var prev = document.getElementById('prev');//获取左滑按钮对象
var next = document.getElementById('next');//获取右滑按钮对象
function animate(offset) {
//获取的是style.left,是相对左边获取距离,所以第一张图后style.left都为负值,
//且style.left获取的是字符串,需要用parseInt()取整转化为数字。
var newLeft = parseInt(list.style.left) + offset;
list.style.left = newLeft + 'px';
if(newLeft<-3000){
list.style.left = -600 + 'px';//判断左滑长度超过图片轴长度后回到第一张图片
}
if(newLeft>-600){
list.style.left = -3000 + 'px';判断右滑长度超过图片轴长度后回到最后一张图片
}
}
prev.onclick = function() {
animate(600);//设置左滑
}
next.onclick = function() {
animate(-600);//设置右滑
}
}
//var timer;//声明变量
//function play() {
// timer = setInterval(function () {prev.onclick()}, 1500)//调用左滑方法并设置时间
//}
//play();//调用方法