轮播图案例
<head>
# css样式
<style type="text/css">
* {
padding: 0;
margin: 0;
list-style: none;
border: 0;
}
.all {
width: 500px;
height: 200px;
padding: 7px;
border: 1px solid #ccc;
margin: 100px auto;
position: relative;
}
.screen {
width: 500px;
height: 200px;
overflow: hidden;
position: relative;
}
.screen li {
width: 500px;
height: 200px;
overflow: hidden;
float: left;
}
.screen ul {
position: absolute;
left: 0;
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;
}
#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;
}
</style>
</head>
<!-- html布局 -->
<div class="all" id="box">
<!-- 轮播图可视区大盒子-->
<div class="screen">
<!-- 图片-->
<ul>
<li><img src="../../images/girl (1).jpg" width="500" height="200" /></li>
<li><img src="../../images/girl (10).jpg" width="500" height="200" /></li>
<li><img src="../../images/girl (11).jpg" width="500" height="200" /></li>
<li><img src="../../images/girl (7).jpg" width="500" height="200" /></li>
<li><img src="../../images/girl (8).jpg" width="500" height="200" /></li>
<li><img src="../../images/girl (1).jpg" width="500" height="200" /></li>
</ul>
<!-- 页码-->
<ol>
<li class="current">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ol>
</div>
<!-- 按钮 -->
<div id="arr"><span id="left"><</span><span id="right">></span></div>
</div>
<script>
var box=document.querySelector('#box');
var screen=document.querySelector('.screen');
var ul=document.querySelector('.screen>ul');
var ol=document.querySelector('.screen>ol');
var arrow=document.querySelector('#arr');
var left=document.querySelector('#left');
var right=document.querySelector('#right');
var timeID = setInterval(function () {
textpath();
}, 2000)
var index=0;
box.onmouseover=function(){
arrow.style.display='block';
clearInterval(timeID);
};
box.onmouseout=function(){
arrow.styel.display='none';
timeID = setInterval(function () {
textpath();
}, 2000)
};
right.onclick=function(){
textpath();
}
function textpath(){
if(index==ul.children.length-1){
ul.style.left='0px';
index=0;
}
index++;
animationMove(ul,-screen.offsetLeft*index);
for(var i=0;i<ol.children.length;i++){
if(index==i){
ol.children[i].className='currnet'
}else{
ol.children[i].className='';
}
}
if (index == ul.children.length - 1) {
ol.children[0].className = 'current';
};
}
left.onclick=function(){
if(index==0){
ul.style.left=(ul.children.length-1)*-screen.offsetWidth+'px';
index=ul.children.length-1;
}
index--;
animationMove(ul,-screen.offsetLeft*index);
for(var i=0;i<ol.children.length;i++){
if(index==i){
ol.children[i].className='currnet'
}else{
ol.children[i].className='';
}
}
if (index == ul.children.length - 1) {
ol.children[0].className = 'current';
};
}
for(var i=0;i<ol.children.length;i++){
ol.children[i].setAttribute('yemaindex', i);
ol.children[i].onclick=function(){
var click_here=this.getAttribute('yemaindex');
index=click_here;
animationMove(ul,-screen.offsetLeft*index);
for(var j=0;j<ol.children.length;j++){
if(j==click_here){
ol.children[j].className='current'
}else{
ol.children[i].className='';
}
}
}
}
</script>