bootstrap-carousel.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* ==========================================================
  2. * bootstrap-carousel.js v2.3.2
  3. * http://twitter.github.com/bootstrap/javascript.html#carousel
  4. * ==========================================================
  5. * Copyright 2012 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 ($) {
  20. "use strict"; // jshint ;_;
  21. /* CAROUSEL CLASS DEFINITION
  22. * ========================= */
  23. var Carousel = function (element, options) {
  24. this.$element = $(element)
  25. this.$indicators = this.$element.find('.carousel-indicators')
  26. this.options = options
  27. this.options.pause == 'hover' && this.$element
  28. .on('mouseenter', $.proxy(this.pause, this))
  29. .on('mouseleave', $.proxy(this.cycle, this))
  30. }
  31. Carousel.prototype = {
  32. cycle: function (e) {
  33. if (!e) this.paused = false
  34. if (this.interval) clearInterval(this.interval);
  35. this.options.interval
  36. && !this.paused
  37. && (this.interval = setInterval($.proxy(this.next, this), this.options.interval))
  38. return this
  39. }
  40. , getActiveIndex: function () {
  41. this.$active = this.$element.find('.item.active')
  42. this.$items = this.$active.parent().children()
  43. return this.$items.index(this.$active)
  44. }
  45. , to: function (pos) {
  46. var activeIndex = this.getActiveIndex()
  47. , that = this
  48. if (pos > (this.$items.length - 1) || pos < 0) return
  49. if (this.sliding) {
  50. return this.$element.one('slid', function () {
  51. that.to(pos)
  52. })
  53. }
  54. if (activeIndex == pos) {
  55. return this.pause().cycle()
  56. }
  57. return this.slide(pos > activeIndex ? 'next' : 'prev', $(this.$items[pos]))
  58. }
  59. , pause: function (e) {
  60. if (!e) this.paused = true
  61. if (this.$element.find('.next, .prev').length && $.support.transition.end) {
  62. this.$element.trigger($.support.transition.end)
  63. this.cycle(true)
  64. }
  65. clearInterval(this.interval)
  66. this.interval = null
  67. return this
  68. }
  69. , next: function () {
  70. if (this.sliding) return
  71. return this.slide('next')
  72. }
  73. , prev: function () {
  74. if (this.sliding) return
  75. return this.slide('prev')
  76. }
  77. , slide: function (type, next) {
  78. var $active = this.$element.find('.item.active')
  79. , $next = next || $active[type]()
  80. , isCycling = this.interval
  81. , direction = type == 'next' ? 'left' : 'right'
  82. , fallback = type == 'next' ? 'first' : 'last'
  83. , that = this
  84. , e
  85. this.sliding = true
  86. isCycling && this.pause()
  87. $next = $next.length ? $next : this.$element.find('.item')[fallback]()
  88. e = $.Event('slide', {
  89. relatedTarget: $next[0]
  90. , direction: direction
  91. })
  92. if ($next.hasClass('active')) return
  93. if (this.$indicators.length) {
  94. this.$indicators.find('.active').removeClass('active')
  95. this.$element.one('slid', 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. this.$element.one($.support.transition.end, function () {
  108. $next.removeClass([type, direction].join(' ')).addClass('active')
  109. $active.removeClass(['active', direction].join(' '))
  110. that.sliding = false
  111. setTimeout(function () { that.$element.trigger('slid') }, 0)
  112. })
  113. } else {
  114. this.$element.trigger(e)
  115. if (e.isDefaultPrevented()) return
  116. $active.removeClass('active')
  117. $next.addClass('active')
  118. this.sliding = false
  119. this.$element.trigger('slid')
  120. }
  121. isCycling && this.cycle()
  122. return this
  123. }
  124. }
  125. /* CAROUSEL PLUGIN DEFINITION
  126. * ========================== */
  127. var old = $.fn.carousel
  128. $.fn.carousel = function (option) {
  129. return this.each(function () {
  130. var $this = $(this)
  131. , data = $this.data('carousel')
  132. , options = $.extend({}, $.fn.carousel.defaults, typeof option == 'object' && option)
  133. , action = typeof option == 'string' ? option : options.slide
  134. if (!data) $this.data('carousel', (data = new Carousel(this, options)))
  135. if (typeof option == 'number') data.to(option)
  136. else if (action) data[action]()
  137. else if (options.interval) data.pause().cycle()
  138. })
  139. }
  140. $.fn.carousel.defaults = {
  141. interval: 5000
  142. , pause: 'hover'
  143. }
  144. $.fn.carousel.Constructor = Carousel
  145. /* CAROUSEL NO CONFLICT
  146. * ==================== */
  147. $.fn.carousel.noConflict = function () {
  148. $.fn.carousel = old
  149. return this
  150. }
  151. /* CAROUSEL DATA-API
  152. * ================= */
  153. $(document).on('click.carousel.data-api', '[data-slide], [data-slide-to]', function (e) {
  154. var $this = $(this), href
  155. , $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
  156. , options = $.extend({}, $target.data(), $this.data())
  157. , slideIndex
  158. $target.carousel(options)
  159. if (slideIndex = $this.attr('data-slide-to')) {
  160. $target.data('carousel').pause().to(slideIndex).cycle()
  161. }
  162. e.preventDefault()
  163. })
  164. }(window.jQuery);