Posted in Javascript onDecember 08, 2016
Bootstrap 轮播(Carousel)插件是一种灵活的响应式的向站点添加滑块的方式。
0x01 基本实例
Bootstrap实现轮播幻灯片的效果,只需要简单地使用class开发就可以了:
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="../../css/bootstrap.min.css" rel="stylesheet"> <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script> <script src="../../js/bootstrap.min.js"></script> <title>轮播幻灯片</title> </head> <body> <div class="container col-md-4 col-md-offset-4"> <div id="myCarouel" class="carousel slide"> <!--幻灯片切换指标--> <ol class="carousel-indicators"> <li data-toggle="#myCarouel" data-slide-to="0" class="active"></li> <li data-toggle="#myCarouel" data-slide-to="1"></li> <li data-toggle="#myCarouel" data-slide-to="2"></li> </ol> <!--幻灯片项目--> <div class="carousel-inner"> <div class="item active"> <img src="../../img/slide1.png" alt="项目1"> </div> <div class="item"> <img src="../../img/slide2.png" alt="项目2"> </div> <div class="item"> <img src="../../img/slide3.png" alt="项目3"> </div> </div> <!--幻灯片导航--> <a class="carousel-control left" href="#myCarouel" data-slide="prev"><<<</a> <a class="carousel-control right" href="#myCarouel" data-slide="next">>>></a> </div> </div> </body> </html>
效果如下:
几点说明:
(1)属性 data-slide 接受关键字 prev 或 next,用来改变幻灯片相对于当前位置的位置。
(2)使用 data-slide-to 来向轮播传递一个原始滑动索引,data-slide-to="2" 将把滑块移动到一个特定的索引,索引从 0 开始计数。
(3)data-ride="carousel" 属性用于标记轮播在页面加载时就开始动画播放。
0x02 添加标题
可以通过.item内的.carousel-caption 元素向幻灯片添加标题:
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="../../css/bootstrap.min.css" rel="stylesheet"> <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script> <script src="../../js/bootstrap.min.js"></script> <title>添加标题</title> </head> <body> <div class="container col-md-4 col-md-offset-4"> <div id="myCarouel" class="carousel slide"> <!--幻灯片切换指标--> <ol class="carousel-indicators"> <li data-toggle="#myCarouel" data-slide-to="0" class="active"></li> <li data-toggle="#myCarouel" data-slide-to="1"></li> <li data-toggle="#myCarouel" data-slide-to="2"></li> </ol> <!--幻灯片项目--> <div class="carousel-inner"> <div class="item active"> <img src="../../img/slide1.png" alt="项目1"> <div class="carousel-caption">标题1</div> </div> <div class="item"> <img src="../../img/slide2.png" alt="项目2"> <div class="carousel-caption">标题2</div> </div> <div class="item"> <img src="../../img/slide3.png" alt="项目3"> <div class="carousel-caption">标题3</div> </div> </div> <!--幻灯片导航--> <a class="carousel-control left" href="#myCarouel" data-slide="prev"><<<</a> <a class="carousel-control right" href="#myCarouel" data-slide="next">>>></a> </div> </div> </body> </html>
效果如下:
0x03 JS切换
还可以利用JS来实现幻灯片的切换。下面添加一些按钮来实现幻灯片的控制:
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="../../css/bootstrap.min.css" rel="stylesheet"> <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script> <script src="../../js/bootstrap.min.js"></script> <title>JS切换</title> </head> <body> <div class="container col-md-6 col-md-offset-3"> <div id="myCarouel" class="carousel slide"> <!--幻灯片切换指标--> <ol class="carousel-indicators"> <li data-toggle="#myCarouel" data-slide-to="0" class="active"></li> <li data-toggle="#myCarouel" data-slide-to="1"></li> <li data-toggle="#myCarouel" data-slide-to="2"></li> </ol> <!--幻灯片项目--> <div class="carousel-inner"> <div class="item active"> <img src="../../img/slide1.png" alt="项目1"> <div class="carousel-caption">标题1</div> </div> <div class="item"> <img src="../../img/slide2.png" alt="项目2"> <div class="carousel-caption">标题2</div> </div> <div class="item"> <img src="../../img/slide3.png" alt="项目3"> <div class="carousel-caption">标题3</div> </div> </div> <!--幻灯片导航--> <a class="carousel-control left" href="#myCarouel" data-slide="prev"><<<</a> <a class="carousel-control right" href="#myCarouel" data-slide="next">>>></a> <!--控制按钮--> <div style="text-align: center"> <button type="button" class="btn start-slide">开始切换</button> <button type="button" class="btn pause-slide">终止切换</button> <button type="button" class="btn prev-slide">前一个幻灯</button> <button type="button" class="btn next-slide">后一个幻灯</button> <button type="button" class="btn slide-1">幻灯1</button> <button type="button" class="btn slide-2">幻灯2</button> <button type="button" class="btn slide-3">幻灯3</button> </div> </div> </div> <script> $(function () { //启动轮播 $(".start-slide").click(function () { $("#myCarouel").carousel('cycle'); }) //终止轮播 $(".pause-slide").click(function () { $("#myCarouel").carousel('pause'); }) //前一个幻灯 $(".prev-slide").click(function () { $("#myCarouel").carousel('prev'); }) //下一个幻灯 $(".next-slide").click(function () { $("#myCarouel").carousel('next'); }) //切换到指定的幻灯 $(".slide-1").click(function () { $("#mycarouel").carousel(0); }) $(".slide-2").click(function () { $("#mycarouel").carousel(1); }) $(".slide-3").click(function () { $("#mycarouel").carousel(2); }) }) </script> </body> </html>
效果如下:
0x04 事件
同样地,Carousel也定义了一些事件:
(1)slide.bs.carousel
当调用 slide 实例方法时立即触发该事件:
$('#mycarouel').on('slide.bs.carousel', function () { // 执行一些动作... })
(2)slid.bs.carousel
当轮播完成幻灯片过渡效果时触发该事件:
$('#mycarouel').on('slid.bs.carousel', function () { // 执行一些动作... })
看下面的例子:
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="../../css/bootstrap.min.css" rel="stylesheet"> <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script> <script src="../../js/bootstrap.min.js"></script> <title>事件</title> </head> <body> <div class="container col-md-4 col-md-offset-4"> <div id="myCarouel" class="carousel slide"> <!--幻灯片切换指标--> <ol class="carousel-indicators"> <li data-toggle="#myCarouel" data-slide-to="0" class="active"></li> <li data-toggle="#myCarouel" data-slide-to="1"></li> <li data-toggle="#myCarouel" data-slide-to="2"></li> </ol> <!--幻灯片项目--> <div class="carousel-inner"> <div class="item active"> <img src="../../img/slide1.png" alt="项目1"> </div> <div class="item"> <img src="../../img/slide2.png" alt="项目2"> </div> <div class="item"> <img src="../../img/slide3.png" alt="项目3"> </div> </div> <!--幻灯片导航--> <a class="carousel-control left" href="#myCarouel" data-slide="prev"><<<</a> <a class="carousel-control right" href="#myCarouel" data-slide="next">>>></a> </div> </div> <script> $(function () { $("#myCarouel").on('slide.bs.carousel',function () { alert('切换幻灯'); }); }) </script> </body> </html>
效果如下:
如果大家还想深入学习,可以点击这里进行学习,再为大家附3个精彩的专题:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。
Bootstrap基本插件学习笔记之轮播幻灯片(23)
- Author -
kikay声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@