bootstrap 获得轮播中的索引 getActiveIndex
2016-05-23 00:26
483 查看
今天想用bootstrap做一个轮播,当轮播滚到每张图的时候,在页面下面就显示相对应的内容,那么问题来了:我肯定需要知道当前活动(显示图片)的索引号,那么bootstrap的轮播组件要怎么获得这个索引号呢~查了bootstrap文档,并没有看到,又看了b的js源码,找到个getActiveIndex()的方法,正是我需要的:
------适用于bootsrap v3.0.2其他版本不确定--------
具体用法如下:
1.html代码:(与bootsrap文档中的一毛一样。)
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel"> <!-- Indicators --> <ol class="carousel-indicators"> <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li> <li data-target="#carousel-example-generic" data-slide-to="1"></li> <li data-target="#carousel-example-generic" data-slide-to="2"></li> </ol> <!-- Wrapper for slides --> <div class="carousel-inner"> <div class="item active" id="t1"> <img src="../imgs/0fool.jpg" alt="..."> <div class="carousel-caption"> <h3>标题1</h3> <p>para1</p> </div> </div> <div class="item" id="t2"> <img src="../imgs/1magician.jpg" alt="..."> <div class="carousel-caption"> <h3>标题2</h3> <p>para2</p> </div> </div> <div class="item" id="t3"> <img src="../imgs/2high-priestess.jpg" alt="..."> <div class="carousel-caption"> <h3>标题3</h3> <p>para3</p> </div> </div> </div> <!-- Controls --> <a class="left carousel-control" href="#carousel-example-generic" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left"></span> </a> <a class="right carousel-control" href="#carousel-example-generic" data-slide="next"> <span class="glyphicon glyphicon-chevron-right"></span> </a> </div>
2。js代码:
监听slide.bs.carousel事件。每当轮播图滚动一次,便触发一次:
$('#carousel-example-generic').on('slide.bs.carousel', function () {
var carouselData = $(this).data('bs.carousel');
var currentIndex = carouselData.getActiveIndex();//当前图片的索引,注意:这并不是下一张图的索引号
var items = carouselData.$items;//所有图片的包裹div的数组
});
注意:
(1).这个索引是从0开始的。
(2).getActiveIndex()获得索引,是当前的索引,并不是下一张图的索引:例如:我从第一张图滑到第二张图时,会触发这个事件,然后显示的index是0
(3).items获得的是一个数组,数组元素是class为.item的div元素。你可以打印出来看看
相关文章推荐
- Bootstrap中datetimepicker使用
- bootstrap实现导航等
- Bootstrap 一个案例应用
- 小白分享学习成果,大神勿喷。用bootstrap做的,引用就不再多说了,哪里写的不好,您多指点
- 基于Bootstrap和Knockout.js的ASP.NET MVC开发实战
- bootstrap固定响应式导航
- 全面解析bootstrap格子布局
- Bootstrap模块dropdown实现下拉框响应
- 基于Bootstrap实现图片轮播效果
- Bootstrap源码分析之nav、collapse
- Bootstrap源码分析之dropdown
- Markdown+Bootstrap图片自适应属性详解
- springmvc4 mybatis 整合 框架源码 bootstrap
- bootstrap模态框配置
- bootstrap 学习片段
- Bootstrap-datetimepicker控件使用
- bootstrap笔记
- Bootstrap3学习笔记(三)之表格
- Bootstrap3学习笔记(二)之排版
- 基于Bootstrap的网页开发