国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jQuery實現輪播圖及其原理詳解

2019-11-19 13:14:28
字體:
來源:轉載
供稿:網友

本文實例為大家分享了jQuery實現輪播圖及其原理的具體代碼,供大家參考,具體內容如下

<!DOCTYPE html><html><head> <meta charset="utf-8" name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> <title>JQuery輪播圖</title> <style>  *{   padding:0;   margin:0;  }  .container{   width:600px;   height:400px;   overflow: hidden;   position:relative;   margin:0 auto;  }  .list{   width:3000px;   height:400px;   position:absolute;  }  .list>img{   float:left;   width:600px;   height:400px;  }  .pointer{   position:absolute;   width:100px;   bottom:20px;   left:250px;  }  .pointer>span{   cursor:pointer;   display:inline-block;   width:10px;   height:10px;   background: #7b7d80;   border-radius:50%;   border:1px solid #fff;  }  .pointer .on{   background: #28a4c9;  }  .arrow{   position:absolute;   text-decoration:none;   width:40px;   height:40px;   background: #727d8f;   color:#fff;   font-weight: bold;   line-height:40px;   text-align:center;   top:180px;   display:none;  }  .arrow:hover{   background: #0f0f0f;  }  .left{   left:0;  }  .right{   right:0;  }  .container:hover .arrow{   display:block;  } </style></head><body> <div class="container">  <div class="list" style="left:0px;">   <!--<img src="../static/image/photo1.jpg" alt="5"/>-->   <img src="../static/image/banner.jpg" alt="1"/>   <img src="../static/image/slide1.jpg" alt="2"/>   <img src="../static/image/slide1.jpg" alt="3"/>   <img src="../static/image/slide1.jpg" alt="4"/>   <img src="../static/image/photo1.jpg" alt="5"/>   <!--<img src="../static/image/banner.jpg" alt="1"/>-->  </div>  <div class="pointer">   <span index="1" class="on"></span>   <span index="2"></span>   <span index="3"></span>   <span index="4"></span>   <span index="5"></span>  </div>  <a href="#" rel="external nofollow" rel="external nofollow" class="arrow left">></a>  <a href="#" rel="external nofollow" rel="external nofollow" class="arrow right"><</a> </div> <script src="../static/js/jquery-3.2.1.min.js"></script> <script>  var imgCount = 5;  var index = 1;  var intervalId;  var buttonSpan = $('.pointer')[0].children;//htmlCollection 集合  //自動輪播功能 使用定時器  autoNextPage();  function autoNextPage(){   intervalId = setInterval(function(){    nextPage(true);   },3000);  }  //當鼠標移入 停止輪播  $('.container').mouseover(function(){   console.log('hah');   clearInterval(intervalId);  });  // 當鼠標移出,開始輪播  $('.container').mouseout(function(){   autoNextPage();  });  //點擊下一頁 上一頁的功能  $('.left').click(function(){   nextPage(true);  });  $('.right').click(function(){   nextPage(false);  });  //小圓點的相應功能 事件委托  clickButtons();  function clickButtons(){   var length = buttonSpan.length;   for(var i=0;i<length;i++){    buttonSpan[i].onclick = function(){     $(buttonSpan[index-1]).removeClass('on');     if($(this).attr('index')==1){      index = 5;     }else{      index = $(this).attr('index')-1;     }     nextPage(true);    };   }  }  function nextPage(next){   var targetLeft = 0;   //當前的圓點去掉on樣式   $(buttonSpan[index-1]).removeClass('on');   if(next){//往后走    if(index == 5){//到最后一張,直接跳到第一張     targetLeft = 0;     index = 1;    }else{     index++;     targetLeft = -600*(index-1);    }   }else{//往前走    if(index == 1){//在第一張,直接跳到第五張     index = 5;     targetLeft = -600*(imgCount-1);    }else{     index--;     targetLeft = -600*(index-1);    }   }   $('.list').animate({left:targetLeft+'px'});   //更新后的圓點加上樣式   $(buttonSpan[index-1]).addClass('on');  } </script></body></html>

效果圖:

原理:

頁面結構方面:

將輪播圖容器設置成相對定位,寬度設置成圖片的寬度;容器中分為四部分:輪播的圖片;點擊的小按鈕;前一張;后一張

樣式方面:

  • 輪播圖容器為相對定位,寬度/高度設置成圖片的寬度/高度,設置overflow為hidden;
  • 容器中的每一部分都設置成絕對定位,放到相應的位置;
  • 輪播圖片的容器寬度設置成所有圖片的寬度和,left開始先設置為0;
  • 每張圖片寬度一樣,都設成左浮動,左右圖片都在一行排列,所以輪播圖的實質是裝有圖片的容器的移動,每次移動的距離為一張圖片的寬度,這樣每次就只顯示一張圖片;
  • 前一張/后一張設置成display為none,當鼠標移入總容器時,再設置成display為block

功能方面:

自動輪播為一個定時循環機制setInteval,鼠標移入,循環停止,移除循環繼續;

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凭祥市| 左权县| 涟水县| 海口市| 平定县| 清水县| 南郑县| 泰州市| 宁津县| 紫金县| 仪陇县| 冕宁县| 托里县| 文昌市| 建水县| 溆浦县| 晴隆县| 威远县| 柳江县| 临桂县| 瑞安市| 沽源县| 安陆市| 泊头市| 西林县| 沾益县| 桑日县| 威宁| 栾川县| 綦江县| 兴仁县| 阿瓦提县| 鲁甸县| 永川市| 西乡县| 灵山县| 玉田县| 宜章县| 隆德县| 怀宁县| 南涧|