<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Bootstrap輪播圖</title> <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css"> <style> #slidershow{ width: 300px; height: 220px; margin: 50px auto; } img{ width: 300px; height: 220px; } </style> </head> <body> <!--#slidershow 層添加 slide 樣式,使用圖片與圖片切換效果有平滑感--> <div id="slidershow" class="carousel slide" data-ride="carousel" data-wrap="true" data-pause="hover" data-interval="2000"> <!-- 設置圖片輪播的順序carousel-indicators --> <ol class="carousel-indicators"> <li class="active" data-toggle="#slideshow" data-side-to="0"></li> <li data-toggle="#slideshow" data-side-to="1"></li> <li data-toggle="#slideshow" data-side-to="2"></li> <li data-toggle="#slideshow" data-side-to="3"></li> </ol> <!--設置輪播圖片carousel-inner--> <div class="carousel-inner"> <div class="item active"> <a href="#"><img src="images/1.jpeg" alt="熊本熊"></a> <!--圖片描述carousel-caption--> <div> <h3 class="carousel-caption">熊本熊</h3> <p>超可愛的熊熊</p> </div> </div> <div class="item"> <a href="#"><img src="images/2.jpg" alt="熊本熊"></a> </div> <div class="item"> <a href="#"><img src="images/3.jpeg" alt="熊本熊"></a> </div> <div class="item"> <a href="#"><img src="images/4.jpg" alt="熊本熊"></a> </div> </div> <!--設置輪播圖控制器carousel-control--> <a class="left carousel-control" href="#slidershow" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left"></span> </a> <a class="right carousel-control" href="#slidershow" data-slide="next"> <span class="glyphicon glyphicon-chevron-right"></span> </a> </div> <script src="js/jquery-1.11.3.js" type="text/javascript"></script> <script src="js/bootstrap.min.js" type="text/javascript"></script> </body> </html>
以上使用聲明式的方法實現輪播javascript
聲明式方法是經過定義 data 屬性來實現,data 屬性能夠很容易地控制輪播的位置。其主要包括如下幾種:css
若是沒有定義 data-ride 屬性,能夠經過 JavaScript 方法來觸發輪播圖片切換:html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Bootstrap輪播圖</title> <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css"> <style> #slidershow{ width: 300px; height: 220px; margin: 50px auto; } img{ width: 300px; height: 220px; } </style> </head> <body> <!--#slidershow 層添加 slide 樣式,使用圖片與圖片切換效果有平滑感--> <!--<div id="slidershow" class="carousel slide" data-ride="carousel" data-wrap="true" data-pause="hover" data-interval="1000">--> <!--注意,下面這句代碼比上有變更--> <div id="slidershow" class="carousel slide"> <!-- 設置圖片輪播的順序carousel-indicators --> <ol class="carousel-indicators"> <li class="active" data-toggle="#slideshow" data-side-to="0"></li> <li data-toggle="#slideshow" data-side-to="1"></li> <li data-toggle="#slideshow" data-side-to="2"></li> <li data-toggle="#slideshow" data-side-to="3"></li> </ol> <!--設置輪播圖片carousel-inner--> <div class="carousel-inner"> <div class="item active"> <a href="#"><img src="images/1.jpeg" alt="熊本熊"></a> <!--圖片描述carousel-caption--> <div> <h3 class="carousel-caption">熊本熊</h3> <p>超可愛的熊熊</p> </div> </div> <div class="item"> <a href="#"><img src="images/2.jpg" alt="熊本熊"></a> </div> <div class="item"> <a href="#"><img src="images/3.jpeg" alt="熊本熊"></a> </div> <div class="item"> <a href="#"><img src="images/4.jpg" alt="熊本熊"></a> </div> </div> <!--設置輪播圖控制器carousel-control--> <a class="left carousel-control" href="#slidershow" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left"></span> </a> <a class="right carousel-control" href="#slidershow" data-slide="next"> <span class="glyphicon glyphicon-chevron-right"></span> </a> </div> <script> $(function () { $("#slidershow").carousel({ interval:2000 }); $("#slidershow a.left").click(function () { $(".carousel").carousel("prev"); }); $("#slidershow a.right").click(function () { $(".carousel").carousel("next"); }) }) </script> <script src="js/jquery-1.11.3.js" type="text/javascript"></script> <script src="js/bootstrap.min.js" type="text/javascript"></script> </body> </html>