1. 程式人生 > 程式設計 >js實現自動播放勻速輪播圖

js實現自動播放勻速輪播圖

本文例項為大家分享了js實現自動播放勻速輪播圖的具體程式碼,供大家參考,具體內容如下

函式封裝: ( 勻速運動函式)

function animate(obj,target,step,speed){
  clearInterval(obj.timer);
 var absStep = Math.abs(step); 
 step = target > obj.offsetLeft ? absStep : -absStep;
 obj.timer = setInterval(function(){
  var distance = Math.abs(target - obj.offsetLeft);
  obj.style.left = obj.offsetLeft + step + 'px';
  if(distance < absStep){
  clearInterval(obj.timer);
  obj.style.left = target + 'px';
  }
 },speed);
 }

物件可以動態生成屬性,用物件的timer,避免了全域性變數的使用,

實現步驟:

1.動態生成ol導航條,並將導航條放入all中使其成為孩子節點
2.根據ul中圖片數量動態生成li標籤,使li成為ol的子節點,
3.給第0個li標籤加上顏色(新增屬性current)
4.用設定的屬性的值去操作圖片使圖片移動,達到滑鼠放上去移動到該圖片效果,排它原理實現樣式效果
5.深度克隆ul中的第一張圖片並將圖片放在ul的末尾
6.加入自動播放函式使其自動播放,設定兩個變數key,squre,key的值用來計算圖片的序號,squre用來計算當前li的序號
7.自動播放函式中排他原理設定當前li標籤樣式
8.在設定onmouseover和onmouseout事件滑鼠放在盒子上暫停,滑鼠離開盒子,繼續運動

9.在滑鼠放在li標籤時讓key等於當前圖片的index屬性值 ,並把key的值賦給squre。

實現細節:

1.動態給ul克隆出第0張圖片補到末尾,以實現自動輪播是無縫的效果,
2.克隆分深克隆和淺克隆,深克隆克隆帶標籤內的所有內容,
3.淺克隆只克隆外部標籤,深克隆引數為true

效果:

js實現自動播放勻速輪播圖

程式碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>勻速輪播動畫</title>
<style type="text/css">
 *{ padding:0; margin:0; list-style:none; border:0;}
 .all{
 width:500px;
 height:200px;
 padding:7px;
 border:1px solid #ccc;
 margin:100px auto;
 position:relative;
 }
 .screen{
 width:500px;
 height:200px;
 overflow:hidden; 
 position:relative;
 }

 .screen li{ 
 width:500px; 
 height:200px; 
 overflow:hidden; 
 float:left;
 }
 .screen ul{ 
 position:absolute;
 left:0; 
 top:0px;
 width:3000px;
 }
 .all ol{ 
 position:absolute; 
 right:10px; 
 bottom:10px;
 line-height:20px;
 text-align:center;
 }
 .all ol li{ 
 float:left; 
 width:20px; 
 height:20px; 
 background:#fff; 
 border:1px solid #ccc; 
 margin-left:10px; 
 cursor:pointer;
 }
 .all ol li.current{ 
 background:yellow;
 }
</style>
<script src="js/勻速運動.js"></script>
<script>
 function $(id){
  return document.getElementById(id);
 }
 window.onload = function(){
  var ul = $('ul');
  var all = $('all');
  var imgs = ul.getElementsByTagName('img');
  var ol = document.createElement('ol');
  all.appendChild(ol);
  for(var i=0;i<imgs.length;i++){
   var li = document.createElement('li');
   li.innerHTML = i+1;
   li.setAttribute('index',i);
   ol.appendChild(li);
  }
  ol.childNodes[0].className = 'current';
 
  var olLis = ol.children;
  for(var i=0;i<olLis.length;i++){
   olLis[i].onmouseover = function(){
    for(var j=0;j<olLis.length;j++){
     olLis[j].className = '';
    }
    this.className = 'current';
    var index = -500*this.getAttribute('index');
    animate(ul,index,20,10);
    key=this.getAttribute('index');
    squre = key;
   }
  }

 ul.appendChild(ul.children[0].cloneNode(true));

  var timer=null;
  var key=0;
  var squre = 0;
  timer=setInterval(autoPlay,1000);
   function autoPlay(){
   key++; //記錄圖片
   squre++;//記錄導航條
   if(key>olLis.length){
    key=1;
    ul.style.left = 0 + 'px';
   }
   if(squre>=olLis.length){
    squre = 0;
   }
   animate(ul,-500*key,10);
   for(var i=0;i<olLis.length;i++){
    olLis[i].className = '';
   }
   olLis[squre].className = 'current';
   }
   all.onmouseover = function(){
   clearInterval(timer);
   }
   all.onmouseout = function(){
  timer=setInterval(autoPlay,1000);
   }
 }
</script>
</head>
<body>
<div class="all" id='all'>
 <div class="screen">
  <ul id="ul">
   <li><img src="images/taobao/1.jpg" width="500" height="200" /></li>
   <li><img src="images/taobao/2.jpg" width="500" height="200" /></li>
   <li><img src="images/taobao/3.jpg" width="500" height="200" /></li>
   <li><img src="images/taobao/4.jpg" width="500" height="200" /></li>
   <li><img src="images/taobao/5.jpg" width="500" height="200" /></li>
  </ul>
 </div>

</div>
</body>
</html>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。