1
0

carousel.js 5.9 KB

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