欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

原生JS實現(xiàn)輪播圖效果

 更新時間:2018年10月12日 08:36:54   作者:p豬  
這篇文章主要為大家詳細介紹了原生JS實現(xiàn)輪播圖效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

學習前端也有一小段時間了,當初在學習javascript的時候,練手的一個輪播圖實例,輪播圖也是挺常見的了。

著是通過獲取圖片偏移量實現(xiàn)的,也實現(xiàn)了無縫切換,還有一點問題就是沒有加上圖片切換的時候的延遲了。

html:

<div id="container">
  <div id="list" style="left: -600px;">
   <img src="../image/1.jpg" alt="5">
   <img src="../image/1.jpg" alt="1">
   <img src="../image/2.jpg" alt="2">
   <img src="../image/3.jpg" alt="3">
   <img src="../image/4.jpg" alt="4">
   <img src="../image/5.jpg" alt="5">
   <img src="../image/1.jpg" alt="1">
  </div>
  <div id="buttons">
    <span class="on"></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
  </div>
  <a href="javascript:;" id="prev" class="arrow">&lt;</a>
  <a href="javascript:;" id="next" class="arrow">&gt;</a>
 </div>

js:

window.onload = function(){
 //獲取元素
 var container = document.getElementById('container');
 var list = this.document.getElementById('list');
 var buttons = document.getElementById('buttons').getElementsByTagName('span');
 var prev = document.getElementById('prev');
 var next = document.getElementById('next');
 var index = 1;//默認第一個小圓點亮

 //小圓點的點亮
 function showButton() {
  //遍歷小圓點的個數(shù),當觸發(fā)onclick事件后,className為‘on'的變?yōu)椤?。
  for(var i = 0;i < buttons.length; i++){
   if(buttons[i].className == 'on'){
    buttons[i].className = '';
    break;
   }
  }
  buttons[index - 1].className = 'on'; //原始第一個小圓點點亮,onclick事件觸發(fā)后,index+1
 }

 function animate (offset) {
  //獲取從第一張圖片開始發(fā)生的偏移量
  var newLift = parseInt(list.style.left) + offset; 
  list.style.left = newLift + 'px';
  if(newLift > -600){ 
   //如果偏移量的位置大于-600的時候,圖片跳轉(zhuǎn)到第五張圖片
   list.style.left = -3000 + 'px';
  }
  if(newLift < -3000){ 
   //如果偏移量的位置大于-3000的時候,圖片跳轉(zhuǎn)到第一張圖片
   list.style.left = -600 + 'px';
  }
 }
 next.onclick = function () {
  //如果button的index為5的時候,再點擊next按鈕會返回 1;
  if(index == 5){
   index = 1;
  }else{
   index += 1;
  }
  showButton();
  animate(-600);
 }
 prev.onclick = function () {
  if(index == 1){
   index = 5;
  }else{
   index -= 1;
  }
  showButton();
  animate(600);
 }
}

更多關(guān)于輪播圖效果的專題,請點擊下方鏈接查看學習

javascript圖片輪播效果匯總

jquery圖片輪播效果匯總

Bootstrap輪播特效匯總

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論