CSS3實現輪播轉場效果

來源:互聯網
上載者:User

標籤:

   實現輪播的一般思路為在一個大盒子中對無限個元素進行切換操作,大盒子固定大小,超出盒子範圍進行隱藏,而裡面無限個元素可以任何堆疊,按照一定的步驟進行位置變換,已達到在可視地區呈現我們想要的效果。

  看一個執行個體:

 HTML代碼:

 1 <div class="trans_box"> 2 <div id="transImageBox" class="trans_image_box"> 3     <div class="trans_image" style="width:300px;height:200px;background:blue;">111</div> 4     <div class="trans_image" style="width:300px;height:200px;background:red;">222</div> 5     <div class="trans_image" style="width:300px;height:200px;background:orange;">333</div> 6 </div> 7 <div id="transImageTrigger" class="trans_image_trigger"> 8     <a href="#1">圖片1</a> <a href="#2">圖片2</a> <a href="#3">圖片3</a> 9 </div>10 </div>

CSS代碼:

 1 .trans_box { 2     width:300px; 3     overflow:hidden; 4 } 5 .trans_image_box { 6     width:20000px; 7     height:200px; 8     -webkit-transition: all 1s ease-in-out; 9        -moz-transition: all 1s ease-in-out;10          -o-transition: all 1s ease-in-out;11             transition: all 1s ease-in-out;12 }13 .trans_image_box .trans_image {14     float:left;15     width:200px;16 }17 .trans_image_trigger {18     padding-top: 10px;19     text-align: center;20 }

JS代碼:

 1 var $ = function(id) { 2     return document.getElementById(id); 3 }; 4 var $box = $("transImageBox"), 5      $oTrigger = $("transImageTrigger").getElementsByTagName("a"), 6      $len = $oTrigger.length; 7 for(var i = 0; i < $len; i++) { 8       $oTrigger[i].onclick = function(){ 9             var index = Number(this.href.replace(/.*#/g,‘‘)) || 1;10             $box.style.marginLeft = (1 - index) * 300 + "px";11             return false;12       };13 }

     點擊查看示範效果

 

CSS3實現輪播轉場效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.