点击(此处)折叠或打开

  1. <!DOCTYPE html>
  2. <html lang="zh-ch">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>轮播特效</title>
  6.     <style type="text/css">
  7. *{
  8.     border: 0;
  9.     margin: 0;
  10.     }
  11. .carousel{
  12.   width:500px;
  13.   height:334px;
  14.   position: fixed;
  15.   overflow: hidden;
  16.     }
  17. .view{
  18.   width:2000px;
  19.   height:334px;
  20.   position:relative;
  21.     }
  22. .view img{
  23.   float: left;
  24. }
  25. .view ul{
  26.   position:fixed;
  27.   top:300px;
  28.   left: 200px;
  29. }
  30. .view ul li{
  31.   height: 12px;
  32.   width: 12px;
  33.   background:white;
  34.   list-style: none;
  35.   display: inline-block;
  36.   margin: 10px;
  37. }
  38. .view ul li.color{
  39.   background:#60b860;
  40. }
  41.     </style>
  42. <script type="text/javascript" src="jquery.js"></script>
  43. </head>
  44. <body>
  45. <div class="carousel">
  46.   <div class="view">

  47.     <img src="images/1.jpg" alt="第一张轮播图">
  48.     <img src="images/2.jpg" alt="第一张轮播图">
  49.     <img src="images/3.jpg" alt="第一张轮播图">
  50.     <img src="images/4.jpg" alt="第一张轮播图">
  51.         <ul>
  52.                 <li class="color"></li>
  53.                 <li></li>
  54.                 <li></li>
  55.                 <li></li>
  56.     </ul>
  57.     </div>
  58. </div>
  59. </body>
  60. <script>
  61. //左右滚动轮播器
  62. $(document).ready(function(){
  63.    var n=0;//设置计数器
  64. //封装冗余函数
  65.    function fn(n){
  66.     $(".view ul li").removeClass("color");
  67.     $(".view ul li").eq(n).addClass("color");
  68.     $(".view").animate({right:n*500+'px'},300) //设置动画
  69.    }
  70. //设置鼠标悬停于li时事件
  71.   $(".view ul li").hover(function(){
  72.     n=$(".view ul li").index(this);//把当前索引号赋予n;
  73.     fn(n);
  74.   })
  75. function start(){
  76.           n++;
  77.     if(n>=$(".view ul li").length){
  78.         n=0;
  79.     }
  80.       fn(n);
  81. }
  82. var auto=setInterval(start,2000);//设置定时器
  83. //设置鼠标悬停在该轮播器时事件
  84. $(".view").hover(function(){
  85.     clearInterval(auto);//清除定时器
  86. },function(){
  87.     auto=setInterval(start,2000);//启动定时器
  88. })
  89. });
  90. </script>
  91. </html>

11-10 20:11