用javascript实现网站首页轮播图效果

  1. <pre name="code" class="html"><!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title>Document</title>  
  6.     <style>  
  7.   *{margin:0;  
  8.     padding:0;  
  9.     list-style:none;}  
  10.   .wrap{height:170px;  
  11.         width:490px;  
  12.         margin:60px auto;  
  13.         overflow: hidden;  
  14.         position: relative;  
  15.         margin:100px auto;}  
  16.   .wrap ul{position:absolute;}   
  17.   .wrap ul li{height:170px;}  
  18.   .wrap ol{position:absolute;  
  19.            right:5px;  
  20.            bottom:10px;}  
  21.   .wrap ol li{height:20px; width: 20px;  
  22.               background:#ccc;  
  23.               border:solid 1px #666;  
  24.               margin-left:5px;  
  25.               color:#000;  
  26.               float:left;  
  27.               line-height:center;  
  28.               text-align:center;  
  29.               cursor:pointer;}  
  30.   .wrap ol .on{background:#E97305;  
  31.                color:#fff;}  
  32.   
  33.   </style>  
  34.   <script type="text/javascript">  
  35.   window.onload=function(){  
  36.     var wrap=document.getElementById('wrap'),  
  37.         pic=document.getElementById('pic'),  
  38.         list=document.getElementById('list').getElementsByTagName('li'),  
  39.         index=0,  
  40.         timer=null;  
  41.   
  42.       // 定义并调用自动播放函数  
  43.       function auto(){  
  44.           timer=setInterval(function(){  
  45.               index++;  
  46.               if(index>=list.length){  
  47.                   index=0;  
  48.               }  
  49.               change(index);  
  50.           },2000);  
  51.       }  
  52.       auto();  
  53.       // 定义图片切换函数  
  54.       function change(curIndex){  
  55.           pic.style.marginTop=-170*curIndex+"px";  
  56.         for(var j=0;j<list.length;j++){  
  57.              list[j].className="";  
  58.          }  
  59.          list[curIndex].className="on";  
  60.          index=curIndex;  
  61.       }  
  62.        
  63.      // 鼠标划过整个容器时停止自动播放  
  64.      wrap.onmouseover=function(){  
  65.         clearInterval(timer);  
  66.      }  
  67.   
  68.      // 鼠标离开整个容器时继续播放至下一张  
  69.      wrap.onmouseout=auto;  
  70.       
  71.      // 遍历所有数字导航实现划过切换至对应的图片  
  72.      for(var i=0;i<list.length;i++){  
  73.          list[i].id=i;  
  74.          list[i].onmouseover=function(){  
  75.              change(this.id);  
  76.          }  
  77.      }  
  78.    }  
  79.   
  80.   </script>     
  81. </head>  
  82. <body>  
  83.   <div class="wrap" id='wrap'>  
  84.     <ul id="pic">  
  85.       <li><img src="http://img.mukewang.com/54111cd9000174cd04900170.jpg" alt=""></li>  
  86.       <li><img src="http://img.mukewang.com/54111dac000118af04900170.jpg" alt=""></li>  
  87.       <li><img src="http://img.mukewang.com/54111d9c0001998204900170.jpg" alt=""></li>  
  88.       <li><img src="http://img.mukewang.com/54111d8a0001f41704900170.jpg" alt=""></li>  
  89.       <li><img src="http://img.mukewang.com/54111d7d00018ba604900170.jpg" alt=""></li>      
  90.     </ul>  
  91.     <ol id="list">  
  92.       <li class="on">1</li>  
  93.       <li>2</li>  
  94.       <li>3</li>  
  95.       <li>4</li>  
  96.       <li>5</li>  
  97.     </ol>  
  98.   </div>  
  99. </body>  
  100. </html>  

猜你喜欢

转载自blog.csdn.net/qq_41813695/article/details/80340153