轮播图我们在网站中经常看到,主要就是为了在有限的空间内展示出更多的内容,同时也增加了界面的特效,让页面不是那么死版,让界面动起来,看着更加舒服,话不多说,代码如下: 效果:
HTML代码: <div class="current"><img src="images/0.jpg" alt=""></div> <div><img src="images/1.jpg" alt=""></div> <div><img src="images/2.jpg" alt=""></div> <div><img src="images/3.jpg" alt=""></div> <div><img src="images/4.jpg" alt=""></div> <li class="current"></li> <a class="arrow arrow-left" href="#"><img src="images/arr-left.png" alt=""></a> <a class="arrow arrow-right" href="#"><img src="images/arr-right.png" alt=""></a>
CSS样式: #slide{width: 500px; height: 313px; margin: 30px auto; position: relative;}//轮播区 #pic div{width: 500px; height: 313px; display:none;}//轮播图 #pic div.current{display: block;}//当前显示轮播 #control{list-style: none; position: absolute; left: 50%; margin-left: -50px; bottom: 15px;}//控制点 #control li{width: 12px; height: 12px; margin-right: 10px; background: #ddd; border-radius: 50%;float: left;} #control li.current{background: #f00;}//当前控制点样式 #control li:hover{cursor: pointer;} #arrow{position: absolute; top: 50%;width: 100%; display: none;}//箭头指示器 #arrow .arrow{display: inline-block; width: 32px; height: 32px; background: rgba(0,0,0,0.3);} #arrow .arrow.arrow-right{float: right;}
JS代码: window.οnlοad=function(){ var slide=document.getElementById('slide'); var pic=document.getElementById('pic').getElementsByTagName('div'); var lis=document.getElementById('control').getElementsByTagName('li'); var arrows=document.getElementById('arrow'); var arrs=arrows.getElementsByClassName('arrow'); var timer=setInterval(move,1000); pic[index].className='current'; lis[index].className='current'; for (var i=0; i<lis.length; i++){ lis[i].οnclick=function(){ this.className='current'; pic[this._index].className='current'; arrs[0].οnclick=function(){ arrs[1].οnclick=function(){ slide.οnmοuseοver=function(){ arrows.style.display='inline-block'; slide.οnmοuseοut=function(){ timer=setInterval(move,1000); arrows.style.display='none'; pic[index].className='current'; lis[index].className='current';
简单的轮播就这么完成了,并没有想象中的那么难,需要的拿去,不谢
|