carousel.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. +function ($) {
  9. 'use strict';
  10. // CAROUSEL CLASS DEFINITION
  11. // =========================
  12. var Carousel = function (element, options) {
  13. this.$element = $(element).on('keydown.bs.carousel', $.proxy(this.keydown, this))
  14. this.$indicators = this.$element.find('.carousel-indicators')
  15. this.options = options
  16. this.paused =
  17. this.sliding =
  18. this.interval =
  19. this.$active =
  20. this.$items = null
  21. this.options.pause == 'hover' && this.$element
  22. .on('mouseenter.bs.carousel', $.proxy(this.pause, this))
  23. .on('mouseleave.bs.carousel', $.proxy(this.cycle, this))
  24. }
  25. Carousel.VERSION = '3.2.0'
  26. Carousel.DEFAULTS = {
  27. interval: 5000,
  28. pause: 'hover',
  29. wrap: true
  30. }
  31. Carousel.prototype.keydown = function (e) {
  32. switch (e.which) {
  33. case 37: this.prev(); break
  34. case 39: this.next(); break
  35. default: return
  36. }
  37. e.preventDefault()
  38. }
  39. Carousel.prototype.cycle = function (e) {
  40. e || (this.paused = false)
  41. this.interval && clearInterval(this.interval)
  42. this.options.interval
  43. && !this.paused
  44. && (this.interval = setInterval($.proxy(this.next, this), this.options.interval))
  45. return this
  46. }
  47. Carousel.prototype.getItemIndex = function (item) {
  48. this.$items = item.parent().children('.item')
  49. return this.$items.index(item || this.$active)
  50. }
  51. Carousel.prototype.to = function (pos) {
  52. var that = this
  53. var activeIndex = this.getItemIndex(this.$active = this.$element.find('.item.active'))
  54. if (pos > (this.$items.length - 1) || pos < 0) return
  55. if (this.sliding) return this.$element.one('slid.bs.carousel', function () { that.to(pos) }) // yes, "slid"
  56. if (activeIndex == pos) return this.pause().cycle()
  57. return this.slide(pos > activeIndex ? 'next' : 'prev', $(this.$items[pos]))
  58. }
  59. Carousel.prototype.pause = function (e) {
  60. e || (this.paused = true)
  61. if (this.$element.find('.next, .prev').length && $.support.transition) {
  62. this.$element.trigger($.support.transition.end)
  63. this.cycle(true)
  64. }
  65. this.interval = clearInterval(this.interval)
  66. return this
  67. }
  68. Carousel.prototype.next = function () {
  69. if (this.sliding) return
  70. return this.slide('next')
  71. }
  72. Carousel.prototype.prev = function () {
  73. if (this.sliding) return
  74. return this.slide('prev')
  75. }
  76. Carousel.prototype.slide = function (type, next) {
  77. var $active = this.$element.find('.item.active')
  78. var $next = next || $active[type]()
  79. var isCycling = this.interval
  80. var direction = type == 'next' ? 'left' : 'right'
  81. var fallback = type == 'next' ? 'first' : 'last'
  82. var that = this
  83. if (!$next.length) {
  84. if (!this.options.wrap) return
  85. $next = this.$element.find('.item')[fallback]()
  86. }
  87. if ($next.hasClass('active')) return (this.sliding = false)
  88. var relatedTarget = $next[0]
  89. var slideEvent = $.Event('slide.bs.carousel', {
  90. relatedTarget: relatedTarget,
  91. direction: direction
  92. })
  93. this.$element.trigger(slideEvent)
  94. if (slideEvent.isDefaultPrevented()) return
  95. this.sliding = true
  96. isCycling && this.pause()
  97. if (this.$indicators.length) {
  98. this.$indicators.find('.active').removeClass('active')
  99. var $nextIndicator = $(this.$indicators.children()[this.getItemIndex($next)])
  100. $nextIndicator && $nextIndicator.addClass('active')
  101. }
  102. var slidEvent = $.Event('slid.bs.carousel', { relatedTarget: relatedTarget, direction: direction }) // yes, "slid"
  103. if ($.support.transition && this.$element.hasClass('slide')) {
  104. $next.addClass(type)
  105. $next[0].offsetWidth // force reflow
  106. $active.addClass(direction)
  107. $next.addClass(direction)
  108. $active
  109. .one('bsTransitionEnd', function () {
  110. $next.removeClass([type, direction].join(' ')).addClass('active')
  111. $active.removeClass(['active', direction].join(' '))
  112. that.sliding = false
  113. setTimeout(function () {
  114. that.$element.trigger(slidEvent)
  115. }, 0)
  116. })
  117. .emulateTransitionEnd($active.css('transition-duration').slice(0, -1) * 1000)
  118. } else {
  119. $active.removeClass('active')
  120. $next.addClass('active')
  121. this.sliding = false
  122. this.$element.trigger(slidEvent)
  123. }
  124. isCycling && this.cycle()
  125. return this
  126. }
  127. // CAROUSEL PLUGIN DEFINITION
  128. // ==========================
  129. function Plugin(option) {
  130. return this.each(function () {
  131. var $this = $(this)
  132. var data = $this.data('bs.carousel')
  133. var options = $.extend({}, Carousel.DEFAULTS, $this.data(), typeof option == 'object' && option)
  134. var action = typeof option == 'string' ? option : options.slide
  135. if (!data) $this.data('bs.carousel', (data = new Carousel(this, options)))
  136. if (typeof option == 'number') data.to(option)
  137. else if (action) data[action]()
  138. else if (options.interval) data.pause().cycle()
  139. })
  140. }
  141. var old = $.fn.carousel
  142. $.fn.carousel = Plugin
  143. $.fn.carousel.Constructor = Carousel
  144. // CAROUSEL NO CONFLICT
  145. // ====================
  146. $.fn.carousel.noConflict = function () {
  147. $.fn.carousel = old
  148. return this
  149. }
  150. // CAROUSEL DATA-API
  151. // =================
  152. $(document).on('click.bs.carousel.data-api', '[data-slide], [data-slide-to]', function (e) {
  153. var href
  154. var $this = $(this)
  155. var $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) // strip for ie7
  156. if (!$target.hasClass('carousel')) return
  157. var options = $.extend({}, $target.data(), $this.data())
  158. var slideIndex = $this.attr('data-slide-to')
  159. if (slideIndex) options.interval = false
  160. Plugin.call($target, options)
  161. if (slideIndex) {
  162. $target.data('bs.carousel').to(slideIndex)
  163. }
  164. e.preventDefault()
  165. })
  166. $(window).on('load', function () {
  167. $('[data-ride="carousel"]').each(function () {
  168. var $carousel = $(this)
  169. Plugin.call($carousel, $carousel.data())
  170. })
  171. })
  172. }(jQuery);