OLD | NEW |
(Empty) | |
| 1 /* ======================================================================== |
| 2 * Bootstrap: carousel.js v3.2.0 |
| 3 * http://getbootstrap.com/javascript/#carousel |
| 4 * ======================================================================== |
| 5 * Copyright 2011-2014 Twitter, Inc. |
| 6 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) |
| 7 * ======================================================================== */ |
| 8 |
| 9 |
| 10 +function ($) { |
| 11 'use strict'; |
| 12 |
| 13 // CAROUSEL CLASS DEFINITION |
| 14 // ========================= |
| 15 |
| 16 var Carousel = function (element, options) { |
| 17 this.$element = $(element).on('keydown.bs.carousel', $.proxy(this.keydown
, this)) |
| 18 this.$indicators = this.$element.find('.carousel-indicators') |
| 19 this.options = options |
| 20 this.paused = |
| 21 this.sliding = |
| 22 this.interval = |
| 23 this.$active = |
| 24 this.$items = null |
| 25 |
| 26 this.options.pause == 'hover' && this.$element |
| 27 .on('mouseenter.bs.carousel', $.proxy(this.pause, this)) |
| 28 .on('mouseleave.bs.carousel', $.proxy(this.cycle, this)) |
| 29 } |
| 30 |
| 31 Carousel.VERSION = '3.2.0' |
| 32 |
| 33 Carousel.DEFAULTS = { |
| 34 interval: 5000, |
| 35 pause: 'hover', |
| 36 wrap: true |
| 37 } |
| 38 |
| 39 Carousel.prototype.keydown = function (e) { |
| 40 switch (e.which) { |
| 41 case 37: this.prev(); break |
| 42 case 39: this.next(); break |
| 43 default: return |
| 44 } |
| 45 |
| 46 e.preventDefault() |
| 47 } |
| 48 |
| 49 Carousel.prototype.cycle = function (e) { |
| 50 e || (this.paused = false) |
| 51 |
| 52 this.interval && clearInterval(this.interval) |
| 53 |
| 54 this.options.interval |
| 55 && !this.paused |
| 56 && (this.interval = setInterval($.proxy(this.next, this), this.options.int
erval)) |
| 57 |
| 58 return this |
| 59 } |
| 60 |
| 61 Carousel.prototype.getItemIndex = function (item) { |
| 62 this.$items = item.parent().children('.item') |
| 63 return this.$items.index(item || this.$active) |
| 64 } |
| 65 |
| 66 Carousel.prototype.to = function (pos) { |
| 67 var that = this |
| 68 var activeIndex = this.getItemIndex(this.$active = this.$element.find('.item
.active')) |
| 69 |
| 70 if (pos > (this.$items.length - 1) || pos < 0) return |
| 71 |
| 72 if (this.sliding) return this.$element.one('slid.bs.carousel', functio
n () { that.to(pos) }) // yes, "slid" |
| 73 if (activeIndex == pos) return this.pause().cycle() |
| 74 |
| 75 return this.slide(pos > activeIndex ? 'next' : 'prev', $(this.$items[pos])) |
| 76 } |
| 77 |
| 78 Carousel.prototype.pause = function (e) { |
| 79 e || (this.paused = true) |
| 80 |
| 81 if (this.$element.find('.next, .prev').length && $.support.transition) { |
| 82 this.$element.trigger($.support.transition.end) |
| 83 this.cycle(true) |
| 84 } |
| 85 |
| 86 this.interval = clearInterval(this.interval) |
| 87 |
| 88 return this |
| 89 } |
| 90 |
| 91 Carousel.prototype.next = function () { |
| 92 if (this.sliding) return |
| 93 return this.slide('next') |
| 94 } |
| 95 |
| 96 Carousel.prototype.prev = function () { |
| 97 if (this.sliding) return |
| 98 return this.slide('prev') |
| 99 } |
| 100 |
| 101 Carousel.prototype.slide = function (type, next) { |
| 102 var $active = this.$element.find('.item.active') |
| 103 var $next = next || $active[type]() |
| 104 var isCycling = this.interval |
| 105 var direction = type == 'next' ? 'left' : 'right' |
| 106 var fallback = type == 'next' ? 'first' : 'last' |
| 107 var that = this |
| 108 |
| 109 if (!$next.length) { |
| 110 if (!this.options.wrap) return |
| 111 $next = this.$element.find('.item')[fallback]() |
| 112 } |
| 113 |
| 114 if ($next.hasClass('active')) return (this.sliding = false) |
| 115 |
| 116 var relatedTarget = $next[0] |
| 117 var slideEvent = $.Event('slide.bs.carousel', { |
| 118 relatedTarget: relatedTarget, |
| 119 direction: direction |
| 120 }) |
| 121 this.$element.trigger(slideEvent) |
| 122 if (slideEvent.isDefaultPrevented()) return |
| 123 |
| 124 this.sliding = true |
| 125 |
| 126 isCycling && this.pause() |
| 127 |
| 128 if (this.$indicators.length) { |
| 129 this.$indicators.find('.active').removeClass('active') |
| 130 var $nextIndicator = $(this.$indicators.children()[this.getItemIndex($next
)]) |
| 131 $nextIndicator && $nextIndicator.addClass('active') |
| 132 } |
| 133 |
| 134 var slidEvent = $.Event('slid.bs.carousel', { relatedTarget: relatedTarget,
direction: direction }) // yes, "slid" |
| 135 if ($.support.transition && this.$element.hasClass('slide')) { |
| 136 $next.addClass(type) |
| 137 $next[0].offsetWidth // force reflow |
| 138 $active.addClass(direction) |
| 139 $next.addClass(direction) |
| 140 $active |
| 141 .one('bsTransitionEnd', function () { |
| 142 $next.removeClass([type, direction].join(' ')).addClass('active') |
| 143 $active.removeClass(['active', direction].join(' ')) |
| 144 that.sliding = false |
| 145 setTimeout(function () { |
| 146 that.$element.trigger(slidEvent) |
| 147 }, 0) |
| 148 }) |
| 149 .emulateTransitionEnd($active.css('transition-duration').slice(0, -1) *
1000) |
| 150 } else { |
| 151 $active.removeClass('active') |
| 152 $next.addClass('active') |
| 153 this.sliding = false |
| 154 this.$element.trigger(slidEvent) |
| 155 } |
| 156 |
| 157 isCycling && this.cycle() |
| 158 |
| 159 return this |
| 160 } |
| 161 |
| 162 |
| 163 // CAROUSEL PLUGIN DEFINITION |
| 164 // ========================== |
| 165 |
| 166 function Plugin(option) { |
| 167 return this.each(function () { |
| 168 var $this = $(this) |
| 169 var data = $this.data('bs.carousel') |
| 170 var options = $.extend({}, Carousel.DEFAULTS, $this.data(), typeof option
== 'object' && option) |
| 171 var action = typeof option == 'string' ? option : options.slide |
| 172 |
| 173 if (!data) $this.data('bs.carousel', (data = new Carousel(this, options))) |
| 174 if (typeof option == 'number') data.to(option) |
| 175 else if (action) data[action]() |
| 176 else if (options.interval) data.pause().cycle() |
| 177 }) |
| 178 } |
| 179 |
| 180 var old = $.fn.carousel |
| 181 |
| 182 $.fn.carousel = Plugin |
| 183 $.fn.carousel.Constructor = Carousel |
| 184 |
| 185 |
| 186 // CAROUSEL NO CONFLICT |
| 187 // ==================== |
| 188 |
| 189 $.fn.carousel.noConflict = function () { |
| 190 $.fn.carousel = old |
| 191 return this |
| 192 } |
| 193 |
| 194 |
| 195 // CAROUSEL DATA-API |
| 196 // ================= |
| 197 |
| 198 $(document).on('click.bs.carousel.data-api', '[data-slide], [data-slide-to]',
function (e) { |
| 199 var href |
| 200 var $this = $(this) |
| 201 var $target = $($this.attr('data-target') || (href = $this.attr('href')) &&
href.replace(/.*(?=#[^\s]+$)/, '')) // strip for ie7 |
| 202 if (!$target.hasClass('carousel')) return |
| 203 var options = $.extend({}, $target.data(), $this.data()) |
| 204 var slideIndex = $this.attr('data-slide-to') |
| 205 if (slideIndex) options.interval = false |
| 206 |
| 207 Plugin.call($target, options) |
| 208 |
| 209 if (slideIndex) { |
| 210 $target.data('bs.carousel').to(slideIndex) |
| 211 } |
| 212 |
| 213 e.preventDefault() |
| 214 }) |
| 215 |
| 216 $(window).on('load', function () { |
| 217 $('[data-ride="carousel"]').each(function () { |
| 218 var $carousel = $(this) |
| 219 Plugin.call($carousel, $carousel.data()) |
| 220 }) |
| 221 }) |
| 222 |
| 223 }(jQuery); |
OLD | NEW |