利用Js+Css實現摺紙動態導航效果執行個體源碼,js摺紙

來源:互聯網
上載者:User

利用Js+Css實現摺紙動態導航效果執行個體源碼,js摺紙

先來看看第一種實現方式

如下:

不再採用ul li的布局方式

-webkit-transform-style:preserve-3d只對子項目有作用,所以每個div都加。

執行個體源碼

<!doctype html><html><head><meta charset="utf-8"><title>無標題文檔</title><style>.wrap{margin:30px auto;width:302px;-webkit-perspective:800px; -webkit-transform-style:preserve-3d; position:relative;}.wrap div{ position:absolute; top:52px;left:0;-webkit-transform-style:preserve-3d; -webkit-transform-origin:top;}.wrap span{ display:block;width:300px;border:1px solid #000;height:50px; font:16px/50px "宋體"; background:#ccc;}</style></head><body><input type="button" value="展開" /><input type="button" value="收縮" /><div class="wrap" id="list"> <span>第一項</span> <div>  <span>第二項</span>  <div>   <span>第三項</span>   <div>    <span>第四項</span>    <div>    <span>第五項</span>     <div>      <span>第六項</span>      <div>       <span>第七項</span>      </div>     </div>    </div>   </div>  </div> </div></div><script>window.onload=function(){ var oList=document.getElementById("list"); var aDiv=oList.getElementsByTagName("div"); var aBtn=document.getElementsByTagName("input"); aBtn[1].onclick=function() { for(var i=0;i<aDiv.length;i++) { aDiv[i].style.transition="0.5s "+(aDiv.length-i)*100+"ms"; aDiv[i].style.WebkitTransform="rotateX(60deg)"; } }; aBtn[0].onclick=function() { for(var i=0;i<aDiv.length;i++) { aDiv[i].style.transition="0.5s "+i*100+"ms"; aDiv[i].style.WebkitTransform="rotateX(0deg)"; } };};</script></body></html>

第二種實現方式

如下:

這個原先是隱藏的,點擊後才展開。

通過主要畫面格控制每個div的展開狀態,當要展開的時候給每個div添加className,但是這個className不是一下子全部添加上去的,而是有延時的,所以用到了定時器。

執行個體源碼

<!doctype html><html><head><meta charset="utf-8"><title>無標題文檔</title><style>@-webkit-keyframes open{ 0% { -webkit-transform:rotateX(-120deg); }  40% { -webkit-transform:rotateX(30deg); } 60% { -webkit-transform:rotateX(-20deg); } 80% { -webkit-transform:rotateX(10deg); } 100% { -webkit-transform:rotateX(0deg); }}.wrap{margin:30px auto;width:300px;-webkit-perspective:800px;position:relative;}.wrap h2{ height:50px;background:#F03; text-align:center; font:16px/50px "微軟雅黑"; color:#fff; position:relative;z-index:2;}.wrap div{ position:absolute; top:32px;left:0;-webkit-transform-style:preserve-3d; -webkit-transform-origin:top; -webkit-transform:rotateX(-120deg); transition:.5s;}.wrap>div{ top:50px;}.wrap .open{-webkit-animation:open 2s;-webkit-transform:rotateX(0deg);}.wrap span{ display:block;width:300px;height:30px; font:14px/30px "宋體"; background:#6F3; text-indent:15px; color:#fff; transition:.5s; box-shadow:inset 0 0 30px 20px rgba(0,0,0,1);}.wrap .open>span{box-shadow:inset 0 0 30px 10px rgba(0,0,0,0);}.wrap span:hover{ background:#FF0;text-indent:30px;}</style></head><body><input type="button" value="展開" /><input type="button" value="收縮" /><div class="wrap" id="list"> <h2>標題</h2> <div>  <span>第一項</span>  <div>   <span>第二項</span>   <div>    <span>第三項</span>    <div>     <span>第四項</span>     <div>      <span>第五項</span>      <div>       <span>第六項</span>       <div>        <span>第七項</span>       </div>      </div>     </div>    </div>   </div>  </div> </div> </div><script>window.onload=function(){ var oList=document.getElementById("list"); var aDiv=oList.getElementsByTagName("div"); var aBtn=document.getElementsByTagName("input"); var oTimer=null; aBtn[1].onclick=function() { var i=aDiv.length-1; clearInterval(oTimer); oTimer=setInterval(function(){ aDiv[i].className=""; i--; if(i<0) { clearInterval(oTimer); } },50);  }; aBtn[0].onclick=function() { var i=0; clearInterval(oTimer); oTimer=setInterval(function(){ aDiv[i].className="open"; i++; if(i==aDiv.length) { clearInterval(oTimer); } },200); };};</script></body></html>

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的協助,如果有疑問大家可以留言交流。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.