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

js仿小米官網(wǎng)圖片輪播特效

 更新時(shí)間:2016年09月29日 14:43:30   作者:西風(fēng)駿馬  
這篇文章主要為大家詳細(xì)介紹了js仿小米官網(wǎng)圖片輪播特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

小米官網(wǎng)給我的感覺是大氣、干凈。很多特效的加入讓人覺得耳目一新,big滿滿。 看到他們首頁(yè)的輪播圖實(shí)現(xiàn)挺有意思,于是自己模仿著寫了一個(gè)。

大致的感覺出來(lái)了,貼個(gè)圖先:

通過前端神器chrom的F12觀察小米官網(wǎng)的html代碼,不難看到他們使用5個(gè)div包裹圖片并使用了定位通過z-index來(lái)控制div層級(jí),通過控制每個(gè)div的opacity屬性和display屬性進(jìn)行元素的顯示、隱藏。

截圖如下(紅框內(nèi)的opacity屬性):

好的,實(shí)現(xiàn)的手段知道了,那么頁(yè)面布局先搞出來(lái)。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
 <title>Mking_js_</title>
 <script type="text/javascript" src="js/moveopacity_1.js"></script>
 <style type="text/css">
  #warp{ width:800px; height: 400px; margin: 20px auto; 
   position: relative;}
  #imgWarp{ width:600px; height: 400px;
   position: absolute; top:0; left: 100px; display: inline-block;
  }
  
  .btn:hover{cursor: pointer;}
  .btn{line-height: 30px; text-align: center;font-size: 25px; font-weight: bold;
   color: #fff;width:80px; height: 30px;top:185px;background: #5f7c8a;position: absolute;
   border-radius: 3px;
  }
  .btn:nth-of-type(1){
    left: 0; }
  .btn:nth-of-type(2) {
   right: 0; }

  img
  {
   position: absolute;
   width:600px; height: 400px;
   background-size:contain;
   opacity: 0;
   display: none;
   z-index: 0;
  }
  img:nth-of-type(1) {
    opacity: 1;
    display: block;
   z-index:5;
  }
  .links{ width:300px; height: 30px;
   position: absolute; right:100px; bottom: 10px; z-index: 10; }
  em{font-style: normal; display: inline-block; height: 30px; width: 30px;
   margin-right: 15px; border: 1px solid #000000; vertical-align: top;
   line-height: 30px; font-size: 20px; text-align: center;color: #fff;
   font-weight: bold; border-radius: 50%; background-color: #008000;
   border-color: #c0c0c0;
   box-sizing: border-box;
  }
   em.active{
    background-color: #fff;
    color: #000000;
   }
 </style>
</head>
<body>
 <div id="warp">
  <div id="imgWarp" >
   <img id="img1" src="product/1.jpg" TITLE="img1" />
   <img id="img2" src="product/2.jpg" TITLE="img2"/>
   <img id="img3" src="product/3.jpg" TITLE="img3"/>
   <img id="img4" src="product/4.jpg" TITLE="img4"/>
   <img id="img5" src="product/5.jpg" TITLE="img5" />
   </div>
  <span class="btn" id="left">left</span>
  <span class="btn" id="right">right</span>
  <div class="links">
   <em class="active"></em>
   <em></em>
   <em></em>
   <em></em>
   <em></em>
  </div>
 </div>
</body>
</html>

結(jié)構(gòu)已經(jīng)有了下面就是添加js讓它動(dòng)起來(lái)。

頁(yè)面中的js:

window.onload = function(){
    var oLeft = document.getElementById("left");
    var oRight = document.getElementById("right");

    var oWarp = document.getElementById("imgWarp");
    var aImg = oWarp.getElementsByTagName("img");
    var aEm = document.getElementsByTagName("em"); 
    var i = 5;
    btn = true;
    oLeft.onclick = function(){
     clearInterval(autoTimer);
     if(btn){ 
      btn=false;
      fnAutoLeft();
     }
    };
    function fnAutoLeft(){
     var a = i % 5;
     i-=1;
     var b = i % 5; 
     startOpacMove(aImg[a],0,aImg[b],100);
     setStyle(b);
    }

    oRight.onclick = function(){
      if(btn){ //單位時(shí)間內(nèi)只能觸發(fā)一次元素的顯示隱藏
      clearInterval(autoTimer);
      btn=false;
      fnAutoRight();
      }
    };
    var count =0;
    function fnAutoRight(){
     var a = i % 5;
     i+=1;
     var b = i % 5;
     
     startOpacMove(aImg[a],0,aImg[b],100); 
     setStyle(b);
     if(i==10){
      i=5;

      for(var j = 0;j<aImg.length;j++){
       if(j==0){
        aImg[0].style.cssText ="opacity: 1; z-index: 5; display: block;";
       }
       else{
        aImg[j].style.cssText ="opacity: 0; z-index: 0; display: none;";
       }
      }
     }
    }
 
    var autoTimer = setInterval(fnAutoRight,5000);
    
    function setStyle(a){
     for(var i=0;i<aEm.length;i++){
       aEm[i].className="";
     } 
     aEm[a].className="active";
    }
   }

運(yùn)動(dòng)的js:

function startOpacMove(obj1,tag1,obj2,tag2)
 { 
  var iCur1 = 0;
  var iCur2 = 0; 
  var iTimer = null;
  var iSpeed1 = -2;
  var iSpeed2 = 2;
  clearInterval(iTimer); 
  iTimer = setInterval(function(){

   var iBtn = true;

   iCur1 = css(obj1,'opacity')*100;
   iCur2 = css(obj2,'opacity')*100;
   iCur1 = Math.floor(iCur1);
   iCur2 = Math.floor(iCur2); 

   if(iCur1 != tag1 && iCur2 != tag2){
    iBtn = false;
    
    obj1.style.opacity = (iCur1+iSpeed1)/100;
    // console.log("隱藏元素時(shí)候的透明度值:"+(iCur1+iSpeed1)/100);
    obj1.style.filter = 'alpha(opacity='+(iCur1+iSpeed1)+')';
    obj1.style.zIndex = 0;
    
    obj2.style.opacity = (iCur2+iSpeed2)/100;
    obj2.style.filter = 'alpha(opacity='+(iCur2+iSpeed2)+')';
    obj2.style.zIndex = 5;
    obj2.style.display ="block";
   }

   if(iBtn){
    clearInterval(iTimer);
    obj1.style.display ="none";
    obj1.style.opacity =0;
    obj2.style.opacity =1; 
    btn = true;
   }
  },10);
}
    
function css(obj,attr){
 if(obj.currentStyle){ //當(dāng)前瀏覽器下存在currentStyle屬性
  return obj.currentStyle[attr];
 }else{
  return getComputedStyle(obj,false)[attr];
 }
} 

看下js代碼是如何讓圖畫動(dòng)起來(lái)的

思考一個(gè)問題:畫面是如何向左向右切換的,完成這一步的必要條件是什么。假設(shè)當(dāng)前顯示的第一張圖,向右切換時(shí)隱藏第一張圖顯示第二張圖,那么需要做的就是讓索引為0的圖片隱藏,索引為1的圖片顯示,再次點(diǎn)擊向右按鈕索引為1的圖片隱藏索引為2的圖片顯示,后面的情況依次類推,需要獲取的索引為0,1,2,3,4。最后注意下右側(cè)的邊界問題就OK了。

好的,貼代碼:

var i = 5; 
function fnAutoRight(){
  var a = i % 5;
  i+=1;
  var b = i % 5;
}

用變量 i 對(duì)5取余,得到的值是0-4的數(shù),剛好是我們想要的結(jié)果,對(duì)應(yīng)的a、b也剛好是想要隱藏、顯示的圖片的索引。再調(diào)用寫好的運(yùn)動(dòng)js、下方顯示當(dāng)前圖片位置函數(shù),那么向右這一塊就OK了。

圖片向左移動(dòng),向下我們需要隱藏顯示的圖片索引是什么,假設(shè)當(dāng)前是第一張圖片點(diǎn)擊向左的時(shí)候,隱藏的圖片索引為0,顯示的圖片索引為4,再次點(diǎn)擊隱藏的索引為4顯示的為3。顯而易見,我們需要的索引為0、4、3、2、1。

代碼如下:

var i=5;
function fnAutoLeft(){
  var a = i % 5;
  i-=1;
  var b = i % 5; 
}

同樣是i=5,然后對(duì)5取余,得到的a為當(dāng)前需要隱藏的圖片索引,讓i減一之后再對(duì)5取余得到的是需要顯示的圖片索引。這樣向左的問題也就解決了。

在最后部分,寫個(gè)setInterval定時(shí)播放的函數(shù),然后設(shè)置一個(gè)間隔調(diào)用的時(shí)間,就完成了自動(dòng)播放的功能。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論