carousel.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* ========================================================================
  2. * Bootstrap: carousel.js v3.4.1
  3. * https://getbootstrap.com/docs/3.4/javascript/#carousel
  4. * ========================================================================
  5. * Copyright 2011-2019 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 = null
  17. this.sliding = null
  18. this.interval = null
  19. this.$active = null
  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.4.1'
  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. if (typeof $next === 'object' && $next.length) {
  114. $next[0].offsetWidth // force reflow
  115. }
  116. $active.addClass(direction)
  117. $next.addClass(direction)
  118. $active
  119. .one('bsTransitionEnd', function () {
  120. $next.removeClass([type, direction].join(' ')).addClass('active')
  121. $active.removeClass(['active', direction].join(' '))
  122. that.sliding = false
  123. setTimeout(function () {
  124. that.$element.trigger(slidEvent)
  125. }, 0)
  126. })
  127. .emulateTransitionEnd(Carousel.TRANSITION_DURATION)
  128. } else {
  129. $active.removeClass('active')
  130. $next.addClass('active')
  131. this.sliding = false
  132. this.$element.trigger(slidEvent)
  133. }
  134. isCycling && this.cycle()
  135. return this
  136. }
  137. // CAROUSEL PLUGIN DEFINITION
  138. // ==========================
  139. function Plugin(option) {
  140. return this.each(function () {
  141. var $this = $(this)
  142. var data = $this.data('bs.carousel')
  143. var options = $.extend({}, Carousel.DEFAULTS, $this.data(), typeof option == 'object' && option)
  144. var action = typeof option == 'string' ? option : options.slide
  145. if (!data) $this.data('bs.carousel', (data = new Carousel(this, options)))
  146. if (typeof option == 'number') data.to(option)
  147. else if (action) data[action]()
  148. else if (options.interval) data.pause().cycle()
  149. })
  150. }
  151. var old = $.fn.carousel
  152. $.fn.carousel = Plugin
  153. $.fn.carousel.Constructor = Carousel
  154. // CAROUSEL NO CONFLICT
  155. // ====================
  156. $.fn.carousel.noConflict = function () {
  157. $.fn.carousel = old
  158. return this
  159. }
  160. // CAROUSEL DATA-API
  161. // =================
  162. var clickHandler = function (e) {
  163. var $this = $(this)
  164. var href = $this.attr('href')
  165. if (href) {
  166. href = href.replace(/.*(?=#[^\s]+$)/, '') // strip for ie7
  167. }
  168. var target = $this.attr('data-target') || href
  169. var $target = $(document).find(target)
  170. if (!$target.hasClass('carousel')) return
  171. var options = $.extend({}, $target.data(), $this.data())
  172. var slideIndex = $this.attr('data-slide-to')
  173. if (slideIndex) options.interval = false
  174. Plugin.call($target, options)
  175. if (slideIndex) {
  176. $target.data('bs.carousel').to(slideIndex)
  177. }
  178. e.preventDefault()
  179. }
  180. $(document)
  181. .on('click.bs.carousel.data-api', '[data-slide]', clickHandler)
  182. .on('click.bs.carousel.data-api', '[data-slide-to]', clickHandler)
  183. $(window).on('load', function () {
  184. $('[data-ride="carousel"]').each(function () {
  185. var $carousel = $(this)
  186. Plugin.call($carousel, $carousel.data())
  187. })
  188. })
  189. }(jQuery);