首页 > 编程技术 > js

js仿小米官网图片轮播特效

发布时间:2016-10-2 16:18

小米官网给我的感觉是大气、干净。很多特效的加入让人觉得耳目一新,big满满。 看到他们首页的轮播图实现挺有意思,于是自己模仿着写了一个。

大致的感觉出来了,贴个图先:

通过前端神器chrom的F12观察小米官网的html代码,不难看到他们使用5个div包裹图片并使用了定位通过z-index来控制div层级,通过控制每个div的opacity属性和display属性进行元素的显示、隐藏。

截图如下(红框内的opacity属性):

好的,实现的手段知道了,那么页面布局先搞出来。

<!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"" width=100% 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"" width=100% src="product/1.jpg" TITLE="img1" />
   <img id="img2"" width=100% src="product/2.jpg" TITLE="img2"/>
   <img id="img3"" width=100% src="product/3.jpg" TITLE="img3"/>
   <img id="img4"" width=100% src="product/4.jpg" TITLE="img4"/>
   <img id="img5"" width=100% 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>

结构已经有了下面就是添加js让它动起来。

页面中的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){ //单位时间内只能触发一次元素的显示隐藏
      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";
    }
   }

运动的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("隐藏元素时候的透明度值:"+(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){ //当前浏览器下存在currentStyle属性
  return obj.currentStyle[attr];
 }else{
  return getComputedStyle(obj,false)[attr];
 }
} 

看下js代码是如何让图画动起来的

思考一个问题:画面是如何向左向右切换的,完成这一步的必要条件是什么。假设当前显示的第一张图,向右切换时隐藏第一张图显示第二张图,那么需要做的就是让索引为0的图片隐藏,索引为1的图片显示,再次点击向右按钮索引为1的图片隐藏索引为2的图片显示,后面的情况依次类推,需要获取的索引为0,1,2,3,4。最后注意下右侧的边界问题就OK了。

好的,贴代码:

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

用变量 i 对5取余,得到的值是0-4的数,刚好是我们想要的结果,对应的a、b也刚好是想要隐藏、显示的图片的索引。再调用写好的运动js、下方显示当前图片位置函数,那么向右这一块就OK了。

图片向左移动,向下我们需要隐藏显示的图片索引是什么,假设当前是第一张图片点击向左的时候,隐藏的图片索引为0,显示的图片索引为4,再次点击隐藏的索引为4显示的为3。显而易见,我们需要的索引为0、4、3、2、1。

代码如下:

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

同样是i=5,然后对5取余,得到的a为当前需要隐藏的图片索引,让i减一之后再对5取余得到的是需要显示的图片索引。这样向左的问题也就解决了。

在最后部分,写个setInterval定时播放的函数,然后设置一个间隔调用的时间,就完成了自动播放的功能。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

标签:[!--infotagslink--]

您可能感兴趣的文章: