1 使用方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 101 Template</title>
<link href="bootstrap.css" rel="stylesheet">
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
</head>
<body>
<div class="container">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!--小圆点-->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" class="active"></li>
<li data-target="#carousel-example-generic"></li>
<li data-target="#carousel-example-generic" ></li>
</ol>
<!--图片和文字说明-->
<div class="carousel-inner">
<div class="item active">
<img src="img/1.jpg" alt="...">
<div class="carousel-caption">
<!--图片1-->
</div>
</div>
<div class="item">
<img src="img/2.jpg" alt="...">
<div class="carousel-caption">
<!--图片2-->
</div>
</div>
<div class="item">
<img src="img/3.jpg" alt="...">
<div class="carousel-caption">
<!--图片3-->
</div>
</div>
</div>
<!--两个箭头-->
<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>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="bootstrap.js"></script>
<script>
$('.carousel').carousel({
interval: 2000
})
</script>
</body>
</html>
2 整体结构
(function ($) {
// 构造函数
var Carousel = function (element, options) {};
//静态变量
Carousel.VERSION = '3.3.7';
//原型方法
Carousel.prototype.keydown = function (e) {};
Carousel.prototype.cycle = function (e) {};
Carousel.prototype.getItemIndex = function (item) {};
Carousel.prototype.getItemForDirection = function (direction, active) {};
Carousel.prototype.to = function (pos) {};
Carousel.prototype.pause = function (e) {};
Carousel.prototype.next = function () {};
Carousel.prototype.prev = function () {};
Carousel.prototype.slide = function (type, next) {};
// 插件入口
function Plugin(option) {}
// 防冲突处理
$.fn.carousel.noConflict = function () {};
// data-api实现
var clickHandler = function (e) {};//点击处理工具函数
//两个箭头绑定事件
$(document).on('click.bs.carousel.data-api', '[data-slide]', clickHandler);
$(window).on('load', function () {});
})(jQuery);
3 插件入口
function Plugin(option) {
return this.each(function () {
var $this = $(this);
var data = $this.data('bs.carousel');//获取自定义数据
// 合并参数:默认参数,所有自定义数据,传入的参数
var options = $.extend({}, Carousel.DEFAULTS, $this.data(), typeof option == 'object' && option);
//字符串参数
var action = typeof option == 'string' ? option : options.slide;
//第一次实例化,并传入自定义数据
if (!data) $this.data('bs.carousel', (data = new Carousel(this, options)));
//如果传入参数
if (typeof option == 'number') data.to(option);
//执行方法
else if (action) data[action]();
else if (options.interval) data.pause().cycle()
})
}
var old = $.fn.carousel;
//暴露接口
$.fn.carousel = Plugin;
$.fn.carousel.Constructor = Carousel;
$.fn.carousel.noConflict = function () {
$.fn.carousel = old;
return this
};
4 data-api实现
//点击处理函数
var clickHandler = function (e) {
var href;
var $this = $(this);
//找到轮播的容器的选择器
var $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, ''));
if (!$target.hasClass('carousel')) return;//判断类名
// 合并参数:容器的data数据+箭头的data数据
var options = $.extend({}, $target.data(), $this.data());
//保存data-slide-to数据
var slideIndex = $this.attr('data-slide-to');
if (slideIndex) options.interval = false;
//执行方法
Plugin.call($target, options);
if (slideIndex) {
$target.data('bs.carousel').to(slideIndex)
}
//取消链接跳转的默认行为
e.preventDefault()
};
$(document)
// 箭头绑定事件
.on('click.bs.carousel.data-api', '[data-slide]', clickHandler)
.on('click.bs.carousel.data-api', '[data-slide-to]', clickHandler);
//开始所有的轮播
$(window).on('load', function () {
$('[data-ride="carousel"]').each(function () {
var $carousel = $(this);
Plugin.call($carousel, $carousel.data())
})
})
5 构造函数+静态变量
var Carousel = function (element, options) {
this.$element = $(element);
this.$indicators = this.$element.find('.carousel-indicators');//小圆点
this.options = options;//选项
this.paused = null;//暂停吗
this.sliding = null;//
this.interval = null;//多长时间轮播
this.$active = null;//激活
this.$items = null;//选项
//响应键盘事件
this.options.keyboard && this.$element.on('keydown.bs.carousel', $.proxy(this.keydown, this));
//悬浮停止
this.options.pause == 'hover' && !('ontouchstart' in document.documentElement) && this.$element
.on('mouseenter.bs.carousel', $.proxy(this.pause, this))
.on('mouseleave.bs.carousel', $.proxy(this.cycle, this))
};
Carousel.VERSION = '3.3.7';//插件版本
Carousel.TRANSITION_DURATION = 600;//动画时间
Carousel.DEFAULTS = {//默认参数
interval: 5000,
pause: 'hover',
wrap: true,
keyboard: true
};
6 原型方法
- 短路口诀:||F2TT,&&T2FF
当条件①为正要执行②时,用&&。当条件①为负要执行②时,用||。
var willWrap = (direction == 'prev' && activeIndex === 0)
|| (direction == 'next' && activeIndex == (this.$items.length - 1));
//响应键盘左右箭头
Carousel.prototype.keydown = function (e) {
if (/input|textarea/i.test(e.target.tagName)) return;
switch (e.which) {
case 37: this.prev(); break;//左箭头
case 39: this.next(); break;//右箭头
default: return
}
//取消链接的跳转
e.preventDefault()
};
Carousel.prototype.cycle = function (e) {
//如果没有e,就修改paused状态
e || (this.paused = false);
//设置了interval就取消定时器;
this.interval && clearInterval(this.interval);
//设置了interval,this.paused为负,设置定时循环
this.options.interval
&& !this.paused
&& (this.interval = setInterval($.proxy(this.next, this), this.options.interval));
return this
};
Carousel.prototype.getItemIndex = function (item) {
//包裹图片的容器
this.$items = item.parent().children('.item');
//传入item,获得在同辈中的索引值
return this.$items.index(item || this.$active)
};
Carousel.prototype.getItemForDirection = function (direction, active) {
//获取索引值
var activeIndex = this.getItemIndex(active);
//如果是prev激活表示为0,如果是next激活表示为个数-1
var willWrap = (direction == 'prev' && activeIndex === 0)
|| (direction == 'next' && activeIndex == (this.$items.length - 1));
if (willWrap && !this.options.wrap) return active;
//-1或者1
var delta = direction == 'prev' ? -1 : 1;
var itemIndex = (activeIndex + delta) % this.$items.length;
return this.$items.eq(itemIndex)
};
Carousel.prototype.to = function (pos) {
var that = this;
//获取激活的索引值
var activeIndex = this.getItemIndex(this.$active = this.$element.find('.item.active'));
//超出范围不处理
if (pos > (this.$items.length - 1) || pos < 0) return;
if (this.sliding) return this.$element.one('slid.bs.carousel', function () { that.to(pos) }) // yes, "slid"
if (activeIndex == pos) return this.pause().cycle()
return this.slide(pos > activeIndex ? 'next' : 'prev', this.$items.eq(pos))
};
Carousel.prototype.pause = function (e) {
// e为空修改标识
e || (this.paused = true);
if (this.$element.find('.next, .prev').length && $.support.transition) {
this.$element.trigger($.support.transition.end);
this.cycle(true)
}
//清除定时器
this.interval = clearInterval(this.interval);
return this
};
Carousel.prototype.next = function () {
if (this.sliding) return
return this.slide('next')
};
Carousel.prototype.prev = function () {
if (this.sliding) return
return this.slide('prev')
};
Carousel.prototype.slide = function (type, next) {
//显示的那个图片
var $active = this.$element.find('.item.active');
//下一个图片
var $next = next || this.getItemForDirection(type, $active);
var isCycling = this.interval;
//方向
var direction = type == 'next' ? 'left' : 'right';
var that = this;
if ($next.hasClass('active')) return (this.sliding = false);
var relatedTarget = $next[0];
var slideEvent = $.Event('slide.bs.carousel', {
relatedTarget: relatedTarget,
direction: direction
});
this.$element.trigger(slideEvent);
if (slideEvent.isDefaultPrevented()) return;
this.sliding = true;
isCycling && this.pause();
if (this.$indicators.length) {
this.$indicators.find('.active').removeClass('active');
var $nextIndicator = $(this.$indicators.children()[this.getItemIndex($next)]);
$nextIndicator && $nextIndicator.addClass('active')
}
var slidEvent = $.Event('slid.bs.carousel', { relatedTarget: relatedTarget, direction: direction }); // yes, "slid"
if ($.support.transition && this.$element.hasClass('slide')) {
$next.addClass(type);
$next[0].offsetWidth ;// force reflow
$active.addClass(direction);
$next.addClass(direction);
$active
.one('bsTransitionEnd', function () {
$next.removeClass([type, direction].join(' ')).addClass('active');
$active.removeClass(['active', direction].join(' '));
that.sliding = false;
setTimeout(function () {
that.$element.trigger(slidEvent)
}, 0)
})
.emulateTransitionEnd(Carousel.TRANSITION_DURATION)
} else {
$active.removeClass('active');
$next.addClass('active');
this.sliding = false;
this.$element.trigger(slidEvent)
}
isCycling && this.cycle();
return this
};
https://blog.csdn.net/yjd19970908/article/details/80387734
网友评论