Bootstrap的圖片輪播範例程式碼,

來源:互聯網
上載者:User

Bootstrap的圖片輪播範例程式碼,

樣本一:

插入js及css支援:

<link rel="stylesheet" href="css/bootstrap.min.css"/>  <script src="js/jquery-1.9.1.min.js"></script><script src="js/bootstrap.min.js"></script>

HTML代碼:

<div id="pictures" class="item">      <div id="myCarousel" class="carousel slide">        <!-- 輪播(Carousel)指標 -->        <ol class="carousel-indicators">         <li data-target="#myCarousel" data-slide-to="0" class="active"></li>         <li data-target="#myCarousel" data-slide-to="1"></li>         <li data-target="#myCarousel" data-slide-to="2"></li>        </ol>          <!-- 輪播(Carousel)項目 -->        <div class="carousel-inner">         <div class="item active">           <img src="images/gf.jpg" class="img-responsive" alt="First slide">         </div>         <div class="item">           <img src="images/psb.jpg" class="img-responsive" alt="Second slide">         </div>         <div class="item">           <img src="images/uyt.jpg" class="img-responsive" alt="Third slide">         </div>        </div>        <!-- 輪播(Carousel)導航 -->        <a class="carousel-control left" href="#myCarousel"          data-slide="prev">‹</a>        <a class="carousel-control right" href="#myCarousel"          data-slide="next">›</a>      </div>     </div>

樣本二:

用法

<div id="myCarousel" class="carousel slide"> <!-- Carousel items --> <div class="carousel-inner">  <div class="active item">…</div>  <div class="item">…</div>  <div class="item">…</div> </div> <!-- Carousel nav --> <a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a> <a class="carousel-control right" href="#myCarousel" data-slide="next">›</a></div>

所以,您把想要呈現的條目(比如 images)以迴圈順序放置在 "carousel-inner" div 中,通過 "<!-- Carousel nav -->" 建立條目的導航。它使用定製的 data 屬性 "data-slide" 來導航到上一個和下一個條目。

您必須在您要建立輪播的 HTML 檔案引用 jquery.js 和 bootstrap-carousel.js 檔案。

Bootstrap 輪播執行個體

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Twitter Bootstrap pager with next and previous example</title> <meta name="description" content="Twitter Bootstrap pager with next and previous example"><link href="/twitter-bootstrap/twitter-bootstrap-v2/docs/assets/css/bootstrap.css" rel="stylesheet"><style type="text/css">body {margin: 50px;}</style></head><body> <ul class="pager"> <li>  <a href="#">Previous</a> </li> <li>  <a href="#">Next</a> </li></ul></body></html>

 帶有 old 和 new 的翻頁執行個體           

<!DOCTYPE html><html lang="en"> <head>  <meta charset="utf-8">  <title>Example of carousal with Twitter Bootstrap</title>  <meta name="viewport" content="width=device-width, initial-scale=1.0">  <meta name="description" content="Example of carousal with Twitter Bootstrap version 2.0 from w3resource.com">   <!-- Le styles -->  <link href="twitter-bootstrap-v2/docs/assets/css/bootstrap.css" rel="stylesheet"> <link href="twitter-bootstrap-v2/docs/assets/css/example-fixed-layout.css" rel="stylesheet">  <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->  <!--[if lt IE 9]>   <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>  <![endif]-->  <!-- Le fav and touch icons -->  <link rel="shortcut icon" href="twitter-bootstrap-v2/docs/examples/images/favicon.ico">  <link rel="apple-touch-icon" href="twitter-bootstrap-v2/docs/examples/images/apple-touch-icon.png">  <link rel="apple-touch-icon" sizes="72x72" href="twitter-bootstrap-v2/docs/examples/images/apple-touch-icon-72x72.png">  <link rel="apple-touch-icon" sizes="114x114" href="twitter-bootstrap-v2/docs/examples/images/apple-touch-icon-114x114.png"> </head> <body>  <div class="navbar navbar-fixed-top">   <div class="navbar-inner">    <div class="container">     <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">      <span class="icon-bar"></span>      <span class="icon-bar"></span>      <span class="icon-bar"></span>     </a>     <a class="brand" href="#"><img src="/images/w3r.png" width="111" height="30" alt="w3resource logo" /></a>     <div class="nav-collapse">      <ul class="nav">       <li class="active"><a href="#">Home</a></li>       <li><a href="#about">About</a></li>       <li><a href="#contact">Contact</a></li>      </ul>     </div><!--/.nav-collapse -->    </div>   </div>  </div>  <div class="container">   <!-- Example row of columns -->   <div class="row">    <div class="span4">     <h2>HTML5 and JS Apps</h2>  <p> </p>     <div id="myCarousel" class="carousel slide">   <!-- Carousel items -->  <div class="carousel-inner">   <div class="active item"><img src="/update-images/html5_logo.png" alt="HTML5 logo" width="500" height="99" /></div>   <div class="item"><img src="/update-images/javascript-logo.png" alt="JS logo" width="500" height="99" /></div>   <div class="item"><img src="/update-images/schema.png" alt="Schema.org logo" width="500" height="99" /></div> <div class="item"><img src="/update-images/json.gif" alt="JSON logo" width="500" height="99" /></div> </div> <!-- Carousel nav --> <a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a> <a class="carousel-control right" href="#myCarousel" data-slide="next">›</a></div></div></div>   <hr>   <footer>    <p>© Company 2012</p>   </footer>  </div> <!-- /container -->  <!-- Le javascript  ================================================== -->  <!-- Placed at the end of the document so the pages load faster -->  <script src="twitter-bootstrap-v2/docs/assets/js/jquery.js"></script>  <script src="twitter-bootstrap-v2/docs/assets/js/bootstrap-carousel.js"></script> </body></html>

使用 Javascript

您可以使用下面的 JavaScript 代碼來建立輪播。

$('.carousel').carousel()

下面是您可以使用的選項

interval: 規定投影片輪換的等待時間,以毫秒為單位。值的類型為 number,預設值是 5000。如果為 false,輪播將不會自動開始迴圈。

pause: 規定當滑鼠停留在投影片地區即暫停輪播,滑鼠離開即啟動輪播。值的類型為 string,預設值是 'hover'。

下面是您可以使用的輪播方法

.carousel(options): 初始化輪播組件,接受一個可選的 object 類型的 options 參數,並開始投影片迴圈。$('.carousel').carousel({ interval: 2000 // in milliseconds}).carousel('cycle'): 從左至右迴圈各幀。$('.carousel').carousel('cycle');.carousel('pause'): 停止輪播。$('#myCarousel').hover(function () {  $(this).carousel('pause') }.carousel(number): 將輪播定位到指定的幀上(幀下標以0開始,類似數組)。$("#carousel_nav").click(function(){ var item = 4; $('#home_carousel').carousel(item); return false; });

.carousel('prev'): 將輪播轉到上一幀。

.carousel('next'): 將輪播轉到下一幀。

這裡有兩個事件用來增強輪播的功能。

slide: 當 slide 執行個體方法被調用之後,此事件被立即觸發。

slid: 當所有投影片播放完之後,此事件被觸發。

聯繫我們

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