TShopping

 找回密碼
 註冊
搜索
查看: 7312|回復: 1

[教學] Bootstrap 輪播

  [複製鏈接]
發表於 2014-9-14 17:06:06 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
描述

在本教程中,您將看到如何使用 Bootstrap 創建輪播。這將幫您創建內容滑塊,圖像畫廊等等。


用法
  1. <div id="myCarousel" class="carousel slide">
  2.   <!-- Carousel items -->
  3.   <div class="carousel-inner">
  4.     <div class="active item">…</div>
  5.     <div class="item">…</div>
  6.     <div class="item">…</div>
  7.   </div>
  8.   <!-- Carousel nav -->
  9.   <a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
  10.   <a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
  11. </div>
複製代碼
所以,您把想要呈現的條目(比如 images)以循環順序放置在 "carousel-inner" div 中,通過 "<!-- Carousel nav -->" 創建條目的導航。它使用定制的 data 屬性 "data-slide" 來導航到上一個和下一個條目。

您必須在您要創建輪播的 HTML 文件引用 jquery.js 和 bootstrap-carousel.js 文件。

Bootstrap 輪播實例
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Twitter Bootstrap pager with next and previous example</title>
  6. <meta name="description" content="Twitter Bootstrap pager with next and previous example">
  7. <link href="/twitter-bootstrap/twitter-bootstrap-v2/docs/assets/css/bootstrap.css" rel="stylesheet">
  8. <style type="text/css">
  9. body {
  10. margin: 50px;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15.   <ul class="pager">
  16.   <li>
  17.     <a href="#">Previous</a>
  18.   </li>
  19.   <li>
  20.     <a href="#">Next</a>
  21.   </li>
  22. </ul>
  23. </body>
  24. </html>
複製代碼
在線查看實例。

帶有 old 和 new 的翻頁實例
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.   <head>
  4.     <meta charset="utf-8">
  5.     <title>Example of carousal with Twitter Bootstrap</title>
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <meta name="description" content="Example of carousal with Twitter Bootstrap version 2.0 from w3resource.com">
  8.      <!-- Le styles -->
  9.     <link href="twitter-bootstrap-v2/docs/assets/css/bootstrap.css" rel="stylesheet">
  10.         <link href="twitter-bootstrap-v2/docs/assets/css/example-fixed-layout.css" rel="stylesheet">
  11.     <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
  12.     <!--[if lt IE 9]>
  13.       <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
  14.     <![endif]-->

  15.     <!-- Le fav and touch icons -->
  16.     <link rel="shortcut icon" href="twitter-bootstrap-v2/docs/examples/images/favicon.ico">
  17.     <link rel="apple-touch-icon" href="twitter-bootstrap-v2/docs/examples/images/apple-touch-icon.png">
  18.     <link rel="apple-touch-icon" sizes="72x72" href="twitter-bootstrap-v2/docs/examples/images/apple-touch-icon-72x72.png">
  19.     <link rel="apple-touch-icon" sizes="114x114" href="twitter-bootstrap-v2/docs/examples/images/apple-touch-icon-114x114.png">
  20.   </head>

  21.   <body>

  22.     <div class="navbar navbar-fixed-top">
  23.       <div class="navbar-inner">
  24.         <div class="container">
  25.           <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
  26.             <span class="icon-bar"></span>
  27.             <span class="icon-bar"></span>
  28.             <span class="icon-bar"></span>
  29.           </a>
  30.           <a class="brand" href="#"><img src="/images/w3r.png" width="111" height="30" alt="w3resource logo" /></a>
  31.           <div class="nav-collapse">
  32.             <ul class="nav">
  33.               <li class="active"><a href="#">Home</a></li>
  34.               <li><a href="#about">About</a></li>
  35.               <li><a href="#contact">Contact</a></li>
  36.             </ul>
  37.           </div><!--/.nav-collapse -->
  38.         </div>
  39.       </div>
  40.     </div>

  41.     <div class="container">
  42.       <!-- Example row of columns -->
  43.       <div class="row">
  44.         <div class="span4">
  45.           <h2>HTML5 and JS Apps</h2>
  46.                   <p> </p>
  47.           <div id="myCarousel" class="carousel slide">
  48.                           <!-- Carousel items -->
  49.                   <div class="carousel-inner">
  50.             <div class="active item"><img src="/update-images/html5_logo.png" alt="HTML5 logo" width="500" height="99" /></div>
  51.             <div class="item"><img src="/update-images/javascript-logo.png" alt="JS logo" width="500" height="99" /></div>
  52.             <div class="item"><img src="/update-images/schema.png" alt="Schema.org logo" width="500" height="99" /></div>
  53.                 <div class="item"><img src="/update-images/json.gif" alt="JSON logo" width="500" height="99" /></div>
  54.   </div>
  55.   <!-- Carousel nav -->
  56.   <a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
  57.   <a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
  58. </div>
  59. </div>
  60. </div>

  61.       <hr>

  62.       <footer>
  63.         <p>c Company 2012</p>
  64.       </footer>

  65.     </div> <!-- /container -->

  66.     <!-- Le javascript
  67.     ================================================== -->
  68.     <!-- Placed at the end of the document so the pages load faster -->
  69.     <script src="twitter-bootstrap-v2/docs/assets/js/jquery.js"></script>
  70.     <script src="twitter-bootstrap-v2/docs/assets/js/bootstrap-carousel.js"></script>


  71.   </body>
  72. </html>
複製代碼
在線查看實例。

使用 Javascript

您可以使用下面的 JavaScript 代碼來創建輪播。
  1. $('.carousel').carousel()
複製代碼
下面是您可以使用的選項
•interval: 規定幻燈片輪換的等待時間,以毫秒為單位。值的類型為 number,默認值是 5000。如果為 false,輪播將不會自動開始循環。
•pause: 規定當鼠標停留在幻燈片區域即暫停輪播,鼠標離開即啟動輪播。值的類型為 string,默認值是 'hover'。

下面是您可以使用的輪播方法
•.carousel(options): 初始化輪播組件,接受一個可選的 object 類型的 options 參數,並開始幻燈片循環。
  1. $('.carousel').carousel({
  2.   interval: 2000 // in milliseconds
  3. })
複製代碼
•.carousel('cycle'): 從左到右循環各幀。
  1. $('.carousel').carousel('cycle');
複製代碼
•.carousel('pause'): 停止輪播。
  1. $('#myCarousel').hover(function () {
  2.   $(this).carousel('pause')
  3. }
複製代碼
•.carousel(number): 將輪播定位到指定的幀上(幀下標以0開始,類似數組)。
  1. $("#carousel_nav").click(function(){
  2. var item = 4;
  3. $('#home_carousel').carousel(item);
  4. return false;
  5. });
複製代碼
•.carousel('prev'): 將輪播轉到上一幀。
•.carousel('next'): 將輪播轉到下一幀。

這裡有兩個事件用來增強輪播的功能。
•slide: 當 slide 實例方法被調用之後,此事件被立即觸發。
•slid: 當所有幻燈片播放完之後,此事件被觸發。

點擊這裡,下載本教程中使用到的所有 HTML、CSS、JS 和圖片文件。

 

臉書網友討論
發表於 2015-4-30 16:26:04 | 顯示全部樓層



  知道了 ~~~

版主招募中

*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-3-28 21:42 , Processed in 0.154633 second(s), 23 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表