加入动画函数和自动播放函数后小圆点可以移动,图片无法切换
<html>
<head>
<title>轮播效果</title>
</head>
<style type="text/css">
* {
padding: 0;
margin: 0;
}
#wrap {
width: 300px;
height: 500px;
position: relative;
overflow: hidden;
}
#list img {
width: 300px;
height: 500px;
float: left;
z-index: 1
}
#list {
position: absolute;
width: 2100px;
}
#buttons span {
background-color: #535353;
border-radius: 15px;
width: 15px;
height: 15px;
display: block;
border: 1px solid #ccc;
float: left;
z-index: 2
}
#buttons span:nth-child(n+2) {
margin-left: 6px;
}
#buttons {
position: absolute;
width: 150px;
left: 0;
right: 0;
margin: auto;
bottom: 5px;
}
#buttons .on {
background-color: #fff;
}
.arrow {
position: absolute;
z-index: 2;
width: 35px;
height: 35px;
color: #fff;
background-color: #ccc;
font-size: 30px;
font-weight: bold;
text-decoration: none;
text-align: center;
opacity: 0.7;
display: none;
}
.arrow:hover {
background-color: rgba(0, 0, 0, 0.7);
}
#prev {
top: 0;
bottom: 0;
margin: auto;
left: 5px;
}
#next {
top: 0;
bottom: 0;
margin: auto;
right: 5px;
}
#wrap:hover .arrow {
display: block;
}
</style>
<body>
<div id="wrap">
<div id="list" style="left: -300px">
<img src="image/5.jpg" alt="">
<img src="image/1.jpg" alt="">
<img src="image/2.jpg" alt="">
<img src="image/3.jpg" alt="">
<img src="image/4.jpg" alt="">
<img src="image/5.jpg" alt="">
<img src="image/1.jpg" alt="">
</div>
<div id="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:void(0)" class="arrow" id="prev"><</a>
<a href="javascript:void(0)" class="arrow" id="next">></a>
</div>
</body>
<script type="text/javascript">
window.onload = function() {
//获取所有的元素
var carousel = document.getElementById("wrap");
var list = document.getElementById("list");
var buttons = document.getElementById("buttons").getElementsByTagName("span");
var prev = document.getElementById("prev");
var next = document.getElementById("next");
var index = 1;
var animated = true;
var timer;
function showbuttins() {
for(var i = 0; i < buttons.length; i++) {
if(buttons[i].className == "on") {
buttons[i].className = "";
break;
}
}
buttons[index - 1].className = "on";
}
function animate(offset) {
var newleft = parseInt(list.style.left) + offset;
var time = 300;
var interval = 10;
var speed = offset / (time/interval);
var go = function(){
// debugger;
if(parseInt(list.style.left) < newleft && speed > 0 || parseInt(list.style.left) > newleft && speed < 0){
animated = true;
list.style.left = parseInt(list.style.left) + speed + 'px';
setTimeout(go, interval);
}else{
animated = false;
list.style.left = newleft +'px';
list.style.left = check(parseInt(list.style.left)) + 'px';
}
}
go();
}
//检测函数
function check(left){
if(left > -300) return -1500;
if(left < -1500) return -300;
}
next.onclick = function() {
if(index == 5) {
index = 1;
} else {
index += 1;
}
if(!animated) {
animate(-300);
}
showbuttins();
}
prev.onclick = function() {
if(!animated) {
animate(300);
}
if(index == 1) {
index = 5;
} else {
index -= 1;
}
showbuttins();
}
for(var i = 0; i < buttons.length; i++) {
buttons[i].onclick = function() {
if(this.className == "on") {
return; //当程序运行到此处跳出
}
var myIndex = parseInt(this.getAttribute('index'));
var offset = -300 * (myIndex - index);
index = myIndex;
if(!animated) {
animate(offset);
}
showbuttins();
}
}
function play() {
timer = setInterval(function() {
next.onclick();
}, 1000);
}
function stop() {
clearInterval(timer);
}
wrap.onmouseover = stop;
wrap.onmouseout = play;
play();
}
</script>
</html>