简单的HTML轮播
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="./css/banner.css"/>
</head>
<body>
<div id="contents" class="content">
<div id="btn_prev" class="btn_ctrl"> < </div>
<div id="btn_next" class="btn_ctrl"> > </div>
<ul id="img_list">
<li><img src="../img/Chrysanthemum.jpg" alt="" /></li>
<li><img src="../img/Desert.jpg" alt="" /></li>
<li><img src="../img/Hydrangeas.jpg" alt="" /></li>
<li><img src="../img/Jellyfish.jpg" alt="" /></li>
<li><img src="../img/Koala.jpg" alt="" /></li>
<li><img src="../img/Lighthouse.jpg" alt="" /></li>
</ul>
<ul id="icon_list">
</ul>
</div>
<script type="text/javascript">
window.onload = function(){
var imgList = document.getElementById('img_list');
var liImg = document.getElementsByTagName('li');
var btnPrev = document.getElementById('btn_prev');
var btnNext = document.getElementById('btn_next');
var iconList = document.getElementById('icon_list');
var liIcon = iconList.children;
liWidth = liImg[0].offsetWidth;
index = 0;
circle = 0;
for(var i = 0,lilen = liImg.length;i<lilen;i++){
var li = document.createElement('li');
li.innerHTML = i + 1;
iconList.appendChild(li);
}
liIcon[0].className = 'clur';
function bannerAnimate(obj,tragetPlace){
clearInterval(obj.timer)
obj.timer = setInterval(function(){
var speed = obj.offsetLeft > tragetPlace ? -60:60;
var result = tragetPlace - obj.offsetLeft;
if(Math.abs(result)>Math.abs(speed)){
obj.style.left = obj.offsetLeft + speed + 'px';
}
else{
obj.style.left = tragetPlace + 'px';
clearInterval(obj.timer)
}
},20)
}
function moveRight(){
index--;
if(i<0){
imgList.style.left = -(liImg.length - 6) * liWidth + 'px';
index = liImg.length/2-1;
}
bannerAnimate(imgList,-index * liWidth);
circle--;
if(circle<0){
circle = liImg.length/2-1;
}
for (var i = 0 ,iconLen = liIcon.length; i<iconLen;i++) {
liIcon[i].className = "";
}
liIcon[0].className = 'clur';
}
function autoLeft(){
index++;
if(index > liImg.length - 7){
imgList.style.left = 0;
index = 1;
}
bannerAnimate(imgList,-index * liWidth);
circle++;
if(circle >= liImg.length - 7){
circle = 0;
}
for (var i = 0 ,iconLen = liIcon.length; i<iconLen;i++) {
liIcon[i].className = "";
}
liIcon[circle].className = 'clur';
}
imgList.addEventListener('mouseenter',function(){
clearInterval(interval)
})
imgList.addEventListener("mouseleave",function(){
interval = setInterval(autoLeft,4000);
})
interval = setInterval(autoLeft,4000);
iconList.addEventListener('click',function(){
clearInterval(interval);
var taget = event.target;
var mytaget = event.currentTarget;
index = taget.innerHTML - 1;
circle = index;
for(var i = 0,iconlen = liIcon.length; i<iconlen;i++ ){
liIcon[i].className ="";
}
liIcon[circle].className = 'clur';
bannerAnimate(imgList,-index * liWidth);
})
btnPrev.addEventListener('click',function(){
clearInterval(interval);
moveRight();
})
btnNext.addEventListener('click',function(){
clearInterval(interval);
autoLeft();
})
}
</script>
</body>
</html>
CSS:
1 *{
2 margin: 0;
3 padding: 0;
4 }
5 #contents{
6 position: relative;
7 width: 1024px;
8 height: 768px;
9 border: 2px solid red;
10 overflow: hidden;
11 margin-left: 300px;
12 }
13 .btn_ctrl{
14 position: absolute;
15 top:48%;
16 font-size: 40px;
17 color: aqua;
18 font-weight: bold;
19 z-index: 3;
20 }
21 #img_list{
22 width: 600%;
23 position: absolute;
24 left: 0;
25 top: 0;
26 }
27 #btn_prev{
28 left: 0;
29 }
30 #btn_next{
31 right: 0;
32 }
33 #img_list li{
34 float: left;
35 list-style: none;
36 }
37
38 .clur{
39 background: rgba(255,255,255,.6);
40 }
41 #icon_list{
42 position: absolute;
43 left: 30%;
44 bottom: 40px;
45 }
46 #icon_list li{
47 float: left;
48 list-style: none;
49 width: 25px;
50 height: 25px;
51 font-size: 24px;
52 line-height: 25px;
53 text-align: center;
54 color: azure;
55 margin-left: 20px;
56 border-radius: 50%;
57 display: inline-block;
58 }