关于轮播图
- 原生JS
- 兼容IE7+等各大浏览器
- 左右箭头切换
- 点击索引切换
- 只需修改图片路径
- 项目依赖 animate.js

html
<div class="all" id="box">
<div class="screen" id="screen">
<ul>
<li>
<img src="./lun1_03.png" width="500" height="300" />
</li>
<li>
<img src="./lun1_03.png" width="500" height="300" />
</li>
<li>
<img src="./lun1_03.png" width="500" height="300" />
</li>
<li>
<img src="./lun1_03.png" width="500" height="300" />
</li>
<li>
<img src="./lun1_03.png" width="500" height="300" />
</li>
</ul>
<ol></ol>
</div>
<div id="arr">
<span id="left"><</span>
<span id="right">></span>
</div>
</div>
样式
* {
padding: 0;
margin: 0;
list-style: none;
border: 0;
}
.all {
width: 500px;
height: 300px;
padding: 7px;
border: 1px solid #ccc;
margin: 100px auto;
position: relative;
}
.screen {
width: 500px;
height: 300px;
overflow: hidden;
position: relative;
}
.screen li {
width: 500px;
height: 300px;
overflow: hidden;
float: left;
}
.screen ul {
position: absolute;
left: 0px;
top: 0px;
width: 3000px;
}
.all ol {
position: absolute;
right: 10px;
bottom: 10px;
line-height: 20px;
text-align: center;
}
.all ol li {
float: left;
width: 20px;
height: 20px;
background: #fff;
border: 1px solid #ccc;
margin-left: 10px;
cursor: pointer;
}
.all ol li.current {
background: yellow;
}
#arr {
display: none;
z-index: 1000;
}
#arr span {
width: 40px;
height: 40px;
position: absolute;
left: 5px;
top: 50%;
margin-top: -20px;
background: #000;
cursor: pointer;
line-height: 40px;
text-align: center;
font-weight: bold;
font-family: '黑体';
font-size: 30px;
color: #fff;
opacity: 0.3;
border: 1px solid #fff;
}
#arr #right {
right: 5px;
left: auto;
}
js
```javascript
var screen = document.getElementById('screen')
var ul = screen.getElementsByTagName('ul')[0]
var lis = ul.children
var ol = screen.getElementsByTagName('ol')[0]
var box = document.getElementById('box')
var arr = document.getElementById('arr')
var right = document.getElementById('right')
var indexGlobal = 0 //用于存储当前展示的图片的下标
// 1. 动态创建数字按钮 - >根据ul中,li的个数,动态的创建ol中的li
for (var i = 0; i < lis.length; i++) {
var li = document.createElement('li')
if (i == 0) {
li.className='current'
}
li.id = i
li.onclick = fn
li.innerText = i + 1
ol.appendChild(li)
}
//给ol里的li注册点击事件
function fn() {
var id = this.id
console.log(id);
var target = id * screen.offsetWidth
animate(ul, -target, 7)
// for (var i = 0; i < ol.children.length; i++) {
// ol.children[i].classList.remove('current')
// }
// this.className='current'
$(this).addClass('current').siblings().removeClass('current')
indexGlobal=id
}
var last = lis[0].cloneNode(true)
ul.appendChild(last)
box.onmouseenter = function () {
arr.style.display = 'block'
}
box.onmouseleave = function () {
arr.style.display = 'none'
}
right.onclick = function() {
if(indexGlobal !=ol.children.length-1) {
indexGlobal++
ol.children[indexGlobal].click()
}else{
indexGlobal++
var target = indexGlobal*screen.offsetWidth
animate(ul,-target,20,function() {
ul.style.left = 0
})
indexGlobal=0
// for(var i = 0 ; i <ol.children.length;i++) {
// ol.children[i].classList.remove('current')
// }
$('ol li').removeClass('current')
ol.children[indexGlobal].className='current'
}
}
left.onclick = function() {
if(indexGlobal != 0 ){
indexGlobal--
ol.children[indexGlobal].click()
}else{
indexGlobal=ol.children.length
var target = indexGlobal*screen.offsetWidth
ul.style.left = -target+'px'
indexGlobal--
// ul.style.left = -target+'px'
// animate(ul,)
ol.children[indexGlobal].click()
}
}