插入js及css支持:
<link href="https://www.jb51.net/css/bootstrap.min.css"/> <script src="https://www.jb51.net/js/jquery-1.9.1.min.js"></script> <script src="https://www.jb51.net/js/bootstrap.min.js"></script>
HTML代码:
<div> <div> <!-- 轮播(Carousel)指标 --> <ol> <li data-target="#myCarousel" data-slide-to="0"></li> <li data-target="#myCarousel" data-slide-to="1"></li> <li data-target="#myCarousel" data-slide-to="2"></li> </ol> <!-- 轮播(Carousel)项目 --> <div> <div> <img src="https://www.jb51.net/images/gf.jpg" alt="First slide"> </div> <div> <img src="https://www.jb51.net/images/psb.jpg" alt="Second slide"> </div> <div> <img src="https://www.jb51.net/images/uyt.jpg" alt="Third slide"> </div> </div> <!-- 轮播(Carousel)导航 --> <a href="#myCarousel" data-slide="prev">‹</a> <a href="#myCarousel" data-slide="next">›</a> </div> </div>
示例二:
用法
<div>
<!-- Carousel items -->
<div>
<div>…</div>
<div>…</div>
<div>…</div>
</div>
<!-- Carousel nav -->
<a href="#myCarousel" data-slide="prev">‹</a>
<a 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 content="Twitter Bootstrap pager with next and previous example"> <link href="https://www.jb51.net/twitter-bootstrap/twitter-bootstrap-v2/docs/assets/css/bootstrap.css"> <style type="text/css"> body { margin: 50px; } </style> </head> <body> <ul> <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 content="width=device-width, initial-scale=1.0"> <meta content="Example of carousal with Twitter Bootstrap version 2.0 from w3resource.com"> <!-- Le styles --> <link href="https://www.jb51.net/twitter-bootstrap-v2/docs/assets/css/bootstrap.css"> <link href="https://www.jb51.net/twitter-bootstrap-v2/docs/assets/css/example-fixed-layout.css"> <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements --> <!--[if lt IE 9]> <script src="https://html5shim.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <!-- Le fav and touch icons --> <link href="https://www.jb51.net/twitter-bootstrap-v2/docs/examples/images/favicon.ico"> <link href="https://www.jb51.net/twitter-bootstrap-v2/docs/examples/images/apple-touch-icon.png"> <link sizes="72x72" href="https://www.jb51.net/twitter-bootstrap-v2/docs/examples/images/apple-touch-icon-72x72.png"> <link sizes="114x114" href="https://www.jb51.net/twitter-bootstrap-v2/docs/examples/images/apple-touch-icon-114x114.png"> </head> <body> <div> <div> <div> <a data-toggle="collapse" data-target=".nav-collapse"> <span></span> <span></span> <span></span> </a> <a href="#"><img src="https://www.jb51.net/images/w3r.png" alt="w3resource logo" /></a> <div> <ul> <li><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> <!-- Example row of columns --> <div> <div> <h2>HTML5 and JS Apps</h2> <p> </p> <div> <!-- Carousel items --> <div> <div><img src="https://www.jb51.net/update-images/html5_logo.png" alt="HTML5 logo" /></div> <div><img src="https://www.jb51.net/update-images/javascript-logo.png" alt="JS logo" /></div> <div><img src="https://www.jb51.net/update-images/schema.png" alt="Schema.org logo" /></div> <div><img src="https://www.jb51.net/update-images/json.gif" alt="JSON logo" /></div> </div> <!-- Carousel nav --> <a href="#myCarousel" data-slide="prev">‹</a> <a 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="https://www.jb51.net/twitter-bootstrap-v2/docs/assets/js/jquery.js"></script> <script src="https://www.jb51.net/twitter-bootstrap-v2/docs/assets/js/bootstrap-carousel.js"></script> </body> </html>
使用 Javascript
您可以使用下面的 JavaScript 代码来创建轮播。
$('.carousel').carousel()
下面是您可以使用的选项
interval: 规定幻灯片轮换的等待时间,以毫秒为单位。值的类型为 number,默认值是 5000。如果为 false,轮播将不会自动开始循环。
pause: 规定当鼠标停留在幻灯片区域即暂停轮播,鼠标离开即启动轮播。值的类型为 string,默认值是 'hover'。
下面是您可以使用的轮播方法