HTML代码块
<body>
<div id="outer">
<ul id="imgList">
<li><img src="img/1.jpg" /></li>
<li><img src="img/2.jpg" /></li>
<li><img src="img/3.jpg" /></li>
<li><img src="img/4.jpg" /></li>
<li><img src="img/5.jpg" /></li>
<li><img src="img/6.jpg" /></li>
<li><img src="img/1.jpg" /></li>
</ul>
<div id="navDiv">
<a href="javascipt:;"></a>
<a href="javascipt:;"></a>
<a href="javascipt:;"></a>
<a href="javascipt:;"></a>
<a href="javascipt:;"></a>
<a href="javascipt:;"></a>
</ul>
</div>
</div>
</body>
Css代码块
* {
margin: 0;
padding: 0;
}
#outer {
width: 340px;
height: 480px;
margin: 50px auto;
background-color: yellowgreen;
padding: 10px 0;
position: relative;
overflow: hidden;
}
#imgList {
list-style: none;
position: absolute;
}
#imgList li {
float: left;
margin: 0 10px;
}
#navDiv {
position: absolute;
bottom: 15px;
}
#navDiv a {
float: left;
width: 15px;
height: 15px;
background-color: red;
margin: 0 5px;
border-radius: 50%;
opacity: 0.5;
}
#navDiv a:hover {
background-color: black;
}
js代码块
var imgList = document.getElementById('imgList');
var imgArr = document.getElementsByTagName('img');
var oldValue = imgList.style.width = (imgArr.length) * 340 + 'px';
var outer = document.getElementById('outer');
var navDiv = document.getElementById('navDiv');
navDiv.style.left = (outer.offsetWidth - navDiv.offsetWidth) / 2 + 'px';
var allA = document.getElementsByTagName('a');
var index = 0;
var count;
allA[index].style.backgroundColor = 'black';
outoChange();
for(var i = 0; i < allA.length; i++) {
allA[i].num = i;
allA[i].onclick = function() {
index = this.num;
setA();
clearInterval(count);
move(imgList , 'left',-340*index , 50 ,function(){
outoChange();
imgList.style.left = -340 * index + 'px';
});
}
}
function setA() {
if(index>=imgArr.length-1){
index = 0;
imgList.style.left = 0 + 'px';
}
for(var i = 0; i < allA.length; i++) {
allA[i].style.backgroundColor = 'red';
}
allA[index].style.backgroundColor = 'black';
}
function outoChange(){
count = setInterval(function(){
index++;
index %=imgArr.length;
move(imgList , 'left',-340*index , 50 ,function(){
setA();
});
},3000);
}
tools.js 需要引用进去 或者直接加在最后
function getStyle(obj, name) {
if(window.getComputedStyle) {
return getComputedStyle(obj, null)[name];
} else {
return obj.currentStyle[name];
}
}
function move(obj, attr, target, speed,callback) {
clearInterval(obj.timer);
var count = parseInt(getStyle(obj, "left"));
if(count > target) {
speed = -speed;
}
obj.timer = setInterval(function() {
var oldValue = parseInt(getStyle(obj, attr));
var newValue = oldValue + speed;
if((speed > 0 && newValue > target) || (speed < 0 && newValue < target)) {
newValue = target
}
obj.style[attr] = newValue + 'px';
if(newValue == target) {
clearInterval(obj.timer);
if(callback){
callback();
}else{
callback;
}
}
}, 30);
}