function $(id) {
return document.getElementById(id);
}
function animate(ele,target) {
clearInterval(ele.timer);
ele.timer = setInterval(function () {
var current = ele.offsetLeft;
var step =730;
step = current<target?step:-step;
current += step;
if(Math.abs(target-current)>Math.abs(step)){
ele.style.left = current+'px';
} else{
clearInterval(ele.timer);
ele.style.left = target+'px';
}
},20)
}
var oBox = $('box');
var inner = oBox.children[0];
var ulObj = inner.children[0];
var spanObj = inner.children[1].children;
var imgWidth = inner.offsetWidth;
var foucslr = $('foucslr');
var left = $('left');
var right = $('right');
for(var i=0;i<spanObj.length;i++){
spanObj[i].setAttribute('index',i);
spanObj[i].onclick = function () {
for(var i=0;i<spanObj.length;i++){
spanObj[i].removeAttribute('class');
}
this.className = 'current';
var index = this.getAttribute('index');
animate(ulObj,-index*imgWidth);
console.log(index);
console.log(imgWidth*index)
}
}
var timer = setInterval(rightMove,1000);
oBox.onmouseover = function () {
foucslr.style.display = 'block';
clearInterval(timer);
}
oBox.onmouseout = function () {
foucslr.style.display = 'none';
timer = setInterval(rightMove,1000)
};
ulObj.appendChild(ulObj.children[0].cloneNode(true));
var pic = 0;
right.onclick = rightMove;
function rightMove() {
if(pic === ulObj.children.length-1){
pic = 0;
ulObj.style.left = 0+'px';
}
pic++;
animate(ulObj,-imgWidth*pic);
if(pic === ulObj.children.length-1){
spanObj[spanObj.length-1].className = '';
spanObj[0].className = 'current'
}else {
for(var i=0;i<spanObj.length;i++){
spanObj[i].removeAttribute('class');
}
spanObj[pic].className = 'current';
}
}
left.onclick = leftMove;
function leftMove() {
if(pic === 0){
pic = 6;
console.log(pic);
ulObj.style.left = -pic*imgWidth+'px'
}
pic--;
animate(ulObj,-imgWidth*pic);
console.log(pic);
for(var i =0;i<spanObj.length;i++){
spanObj[i].removeAttribute('class');
}
spanObj[pic].className = 'current';
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>完整的轮播图</title>
<style>
*{
margin: 0;
padding: 0;
}
.box{
width: 730px;
height: 454px;
margin: 100px auto;
position: relative;
}
.inner{
width:730px ;
height: 454px;
overflow: hidden;
position: relative;
}
ul{
list-style-type: none;
}
.inner ul{
width: 1000%;
position: absolute;
top: 0;
left: 0;
}
.inner li{
float: left;
}
.square{
position: absolute;
right: 10px;
bottom: 10px;
}
.square span{
width: 16px;
height: 16px;
background-color: #ffffff;
display: inline-block;
text-align: center;
line-height: 16px;
cursor: pointer;
}
.square span.current{
background-color: orange;
color: #ffffff;
}
#foucslr{
display: none;
}
#foucslr span{
width: 40px;
height: 40px;
line-height: 40px;
position: absolute;
left: 5px;
top: 50%;
margin-top: -20px;
background-color: #000000;
text-align: center;
color: #ffffff;
font-size: 30px;
cursor: pointer;
border: 1px solid #cccccc;
opacity: 0.3;
}
#foucslr #right{
right: 5px;
left: auto;
}
</style>
</head>
<body>
<div class="box" id="box">
<div class="inner">
<ul>
<li><img src="../img1/images/1.jpg" alt="" width="730" height="454"></li>
<li><img src="../img1/images/2.jpg" alt=""></li>
<li><img src="../img1/images/3.jpg" alt=""></li>
<li><img src="../img1/images/4.jpg" alt=""></li>
<li><img src="../img1/images/5.jpg" alt=""></li>
<li><img src="../img1/images/6.jpg" alt=""></li>
</ul>
<div class="square">
<span class="current">1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
</div>
</div>
<div id="foucslr">
<span id="left"><</span>
<span id="right">></span>
</div>
</div>
<script src="完整的轮播图.js"></script>
</body>
</html>