js轮播

 <!DOCTYPE html>
 <html>
 <head>
 <meta charset="utf-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <title>轮播图</title>
 <link rel="stylesheet" href="">
 <style type="text/css" media="screen">
 *{
 margin: 0;
 padding: 0;
 }
 .clear:after{
 content: '';
 clear: both;
 display: block;
 overflow: hidden;
 height: 0px;
 }
 .box{
 width: 490px;
 height: 170px;
 margin: 100px auto;
 position: relative;
 overflow: hidden;
 }
 .container{
 width: 3430px;
 height: 170px;
 position: absolute;
 top: 0px;
 left: 0px;
 }
 .container>img{
 /*display: block;*/
 float: left;
 }
 .indicator{
 position: absolute;
 bottom: 0px;
 right: 0px;
 }
 li{
 list-style: none;
 float: left;
 width: 30px;
 height: 30px;
 text-align: center;
 line-height: 30px;
 margin-left: 5px;
 background-color: rgba(0,0,0,0.6);
 color: white;
 }
 .indicator .active{
 background-color: red;
 color: yellow;
 }
  
 </style>
 </head>
 <body>
 <div class="box">
 <div class="container clear">
 <img src="images/img1.jpg" alt="">
 <img src="images/img2.jpg" alt="">
 <img src="images/img3.jpg" alt="">
 <img src="images/img4.jpg" alt="">
 <img src="images/img5.jpg" alt="">
 <img src="images/img6.jpg" alt="">
 <img src="images/img1.jpg" alt="">
 </div>
 <div class="indicator">
 <ul class="clear">
 <li>1</li>
 <li>2</li>
 <li>3</li>
 <li>4</li>
 <li>5</li>
 <li>6</li>
 </ul>
 </div>
 </div>
 <script>
 window.onload = function(){
 var containerElement = document.getElementsByClassName('container')[0];
 var liList = document.getElementsByTagName('li');
 var showIndex = 0;
 var innerTimer = null;
 var outerTimer = setInterval(doMove,1000);
 // 以动画的方式把container向左移动490px;
 function doMove(){
 showIndex++;
 // 高亮显示showIndex 对应的li
 showActive(showIndex);
 // var currentLeft = containerElement.offsetLeft;
 // currentLeft -= 490;
 // containerElement.style.left = currentLeft + 'px';
 var endPos = containerElement.offsetLeft - 490 ;
 clearInterval(innerTimer);
 innerTimer = setInterval(function(){
 var currentLeft = containerElement.offsetLeft;
 currentLeft -= 10;
 containerElement.style.left = currentLeft + 'px';
 if(currentLeft <= endPos){
 clearInterval(innerTimer);
 // 如果是最后一张,需要重置为初始的状态
 if(showIndex >= 6){
 showIndex = 0;
 showActive(showIndex);
 // 把container的left 重置为0
 containerElement.style.left = '0px';
 }
 }
 },10);
 }
 function showActive(index){
 for(var i = 0; i < liList.length; i++){
 if( i == showIndex){
 liList[i].className = 'active';
 }
 else{
 liList[i].className = '';
 }
 }
 }
 // 对每一个li元素添加onmouseover 和 onmouseout 的操作
 for(var i = 0; i < liList.length; i++){
 liList[i].index = i;
 liList[i].onmouseover = function(){
 clearInterval(innerTimer);
 clearInterval(outerTimer);
 // 移动容器container, 显示当前li对应的图片,
 showIndex = this.index;
 containerElement.style.left = (-showIndex *490) + 'px';
 showActive(showIndex);
 }
 liList[i].onmouseout = function(){
 outerTimer = setInterval(doMove,2000);
 }
 }
 }
 </script>
 </body>
 </html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值