You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 line
7.0KB

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