carousel.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* ========================================================================
  2. * Bootstrap: carousel.js v3.0.3
  3. * http://getbootstrap.com/javascript/#carousel
  4. * ========================================================================
  5. * Copyright 2013 Twitter, Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ======================================================================== */
  19. +function ($) { "use strict";
  20. // CAROUSEL CLASS DEFINITION
  21. // =========================
  22. var Carousel = function (element, options) {
  23. this.$element = $(element)
  24. this.$indicators = this.$element.find('.carousel-indicators')
  25. this.options = options
  26. this.paused =
  27. this.sliding =
  28. this.interval =
  29. this.$active =
  30. this.$items = null
  31. this.options.pause == 'hover' && this.$element
  32. .on('mouseenter', $.proxy(this.pause, this))
  33. .on('mouseleave', $.proxy(this.cycle, this))
  34. }
  35. Carousel.DEFAULTS = {
  36. interval: 5000
  37. , pause: 'hover'
  38. , wrap: true
  39. }
  40. Carousel.prototype.cycle = function (e) {
  41. e || (this.paused = false)
  42. this.interval && clearInterval(this.interval)
  43. this.options.interval
  44. && !this.paused
  45. && (this.interval = setInterval($.proxy(this.next, this), this.options.interval))
  46. return this
  47. }
  48. Carousel.prototype.getActiveIndex = function () {
  49. this.$active = this.$element.find('.item.active')
  50. this.$items = this.$active.parent().children()
  51. return this.$items.index(this.$active)
  52. }
  53. Carousel.prototype.to = function (pos) {
  54. var that = this
  55. var activeIndex = this.getActiveIndex()
  56. if (pos > (this.$items.length - 1) || pos < 0) return
  57. if (this.sliding) return this.$element.one('slid.bs.carousel', function () { that.to(pos) })
  58. if (activeIndex == pos) return this.pause().cycle()
  59. return this.slide(pos > activeIndex ? 'next' : 'prev', $(this.$items[pos]))
  60. }
  61. Carousel.prototype.pause = function (e) {
  62. e || (this.paused = true)
  63. if (this.$element.find('.next, .prev').length && $.support.transition.end) {
  64. this.$element.trigger($.support.transition.end)
  65. this.cycle(true)
  66. }
  67. this.interval = clearInterval(this.interval)
  68. return this
  69. }
  70. Carousel.prototype.next = function () {
  71. if (this.sliding) return
  72. return this.slide('next')
  73. }
  74. Carousel.prototype.prev = function () {
  75. if (this.sliding) return
  76. return this.slide('prev')
  77. }
  78. Carousel.prototype.slide = function (type, next) {
  79. var $active = this.$element.find('.item.active')
  80. var $next = next || $active[type]()
  81. var isCycling = this.interval
  82. var direction = type == 'next' ? 'left' : 'right'
  83. var fallback = type == 'next' ? 'first' : 'last'
  84. var that = this
  85. if (!$next.length) {
  86. if (!this.options.wrap) return
  87. $next = this.$element.find('.item')[fallback]()
  88. }
  89. this.sliding = true
  90. isCycling && this.pause()
  91. var e = $.Event('slide.bs.carousel', { relatedTarget: $next[0], direction: direction })
  92. if ($next.hasClass('active')) return
  93. if (this.$indicators.length) {
  94. this.$indicators.find('.active').removeClass('active')
  95. this.$element.one('slid.bs.carousel', function () {
  96. var $nextIndicator = $(that.$indicators.children()[that.getActiveIndex()])
  97. $nextIndicator && $nextIndicator.addClass('active')
  98. })
  99. }
  100. if ($.support.transition && this.$element.hasClass('slide')) {
  101. this.$element.trigger(e)
  102. if (e.isDefaultPrevented()) return
  103. $next.addClass(type)
  104. $next[0].offsetWidth // force reflow
  105. $active.addClass(direction)
  106. $next.addClass(direction)
  107. $active
  108. .one($.support.transition.end, function () {
  109. $next.removeClass([type, direction].join(' ')).addClass('active')
  110. $active.removeClass(['active', direction].join(' '))
  111. that.sliding = false
  112. setTimeout(function () { that.$element.trigger('slid.bs.carousel') }, 0)
  113. })
  114. .emulateTransitionEnd(600)
  115. } else {
  116. this.$element.trigger(e)
  117. if (e.isDefaultPrevented()) return
  118. $active.removeClass('active')
  119. $next.addClass('active')
  120. this.sliding = false
  121. this.$element.trigger('slid.bs.carousel')
  122. }
  123. isCycling && this.cycle()
  124. return this
  125. }
  126. // CAROUSEL PLUGIN DEFINITION
  127. // ==========================
  128. var old = $.fn.carousel
  129. $.fn.carousel = function (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. $.fn.carousel.Constructor = Carousel
  142. // CAROUSEL NO CONFLICT
  143. // ====================
  144. $.fn.carousel.noConflict = function () {
  145. $.fn.carousel = old
  146. return this
  147. }
  148. // CAROUSEL DATA-API
  149. // =================
  150. $(document).on('click.bs.carousel.data-api', '[data-slide], [data-slide-to]', function (e) {
  151. var $this = $(this), href
  152. var $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
  153. var options = $.extend({}, $target.data(), $this.data())
  154. var slideIndex = $this.attr('data-slide-to')
  155. if (slideIndex) options.interval = false
  156. $target.carousel(options)
  157. if (slideIndex = $this.attr('data-slide-to')) {
  158. $target.data('bs.carousel').to(slideIndex)
  159. }
  160. e.preventDefault()
  161. })
  162. $(window).on('load', function () {
  163. $('[data-ride="carousel"]').each(function () {
  164. var $carousel = $(this)
  165. $carousel.carousel($carousel.data())
  166. })
  167. })
  168. }(jQuery);