bootstrap-carousel.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* ==========================================================
  2. * bootstrap-carousel.js v2.1.1
  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.options = options
  26. this.options.slide && this.slide(this.options.slide)
  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. this.options.interval
  35. && !this.paused
  36. && (this.interval = setInterval($.proxy(this.next, this), this.options.interval))
  37. return this
  38. }
  39. , to: function (pos) {
  40. var $active = this.$element.find('.item.active')
  41. , children = $active.parent().children()
  42. , activePos = children.index($active)
  43. , that = this
  44. if (pos > (children.length - 1) || pos < 0) return
  45. if (this.sliding) {
  46. return this.$element.one('slid', function () {
  47. that.to(pos)
  48. })
  49. }
  50. if (activePos == pos) {
  51. return this.pause().cycle()
  52. }
  53. return this.slide(pos > activePos ? 'next' : 'prev', $(children[pos]))
  54. }
  55. , pause: function (e) {
  56. if (!e) this.paused = true
  57. if (this.$element.find('.next, .prev').length && $.support.transition.end) {
  58. this.$element.trigger($.support.transition.end)
  59. this.cycle()
  60. }
  61. clearInterval(this.interval)
  62. this.interval = null
  63. return this
  64. }
  65. , next: function () {
  66. if (this.sliding) return
  67. return this.slide('next')
  68. }
  69. , prev: function () {
  70. if (this.sliding) return
  71. return this.slide('prev')
  72. }
  73. , slide: function (type, next) {
  74. var $active = this.$element.find('.item.active')
  75. , $next = next || $active[type]()
  76. , isCycling = this.interval
  77. , direction = type == 'next' ? 'left' : 'right'
  78. , fallback = type == 'next' ? 'first' : 'last'
  79. , that = this
  80. , e = $.Event('slide', {
  81. relatedTarget: $next[0]
  82. })
  83. this.sliding = true
  84. isCycling && this.pause()
  85. $next = $next.length ? $next : this.$element.find('.item')[fallback]()
  86. if ($next.hasClass('active')) return
  87. if ($.support.transition && this.$element.hasClass('slide')) {
  88. this.$element.trigger(e)
  89. if (e.isDefaultPrevented()) return
  90. $next.addClass(type)
  91. $next[0].offsetWidth // force reflow
  92. $active.addClass(direction)
  93. $next.addClass(direction)
  94. this.$element.one($.support.transition.end, function () {
  95. $next.removeClass([type, direction].join(' ')).addClass('active')
  96. $active.removeClass(['active', direction].join(' '))
  97. that.sliding = false
  98. setTimeout(function () { that.$element.trigger('slid') }, 0)
  99. })
  100. } else {
  101. this.$element.trigger(e)
  102. if (e.isDefaultPrevented()) return
  103. $active.removeClass('active')
  104. $next.addClass('active')
  105. this.sliding = false
  106. this.$element.trigger('slid')
  107. }
  108. isCycling && this.cycle()
  109. return this
  110. }
  111. }
  112. /* CAROUSEL PLUGIN DEFINITION
  113. * ========================== */
  114. $.fn.carousel = function (option) {
  115. return this.each(function () {
  116. var $this = $(this)
  117. , data = $this.data('carousel')
  118. , options = $.extend({}, $.fn.carousel.defaults, typeof option == 'object' && option)
  119. , action = typeof option == 'string' ? option : options.slide
  120. if (!data) $this.data('carousel', (data = new Carousel(this, options)))
  121. if (typeof option == 'number') data.to(option)
  122. else if (action) data[action]()
  123. else if (options.interval) data.cycle()
  124. })
  125. }
  126. $.fn.carousel.defaults = {
  127. interval: 5000
  128. , pause: 'hover'
  129. }
  130. $.fn.carousel.Constructor = Carousel
  131. /* CAROUSEL DATA-API
  132. * ================= */
  133. $(function () {
  134. $('body').on('click.carousel.data-api', '[data-slide]', function ( e ) {
  135. var $this = $(this), href
  136. , $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
  137. , options = !$target.data('modal') && $.extend({}, $target.data(), $this.data())
  138. $target.carousel(options)
  139. e.preventDefault()
  140. })
  141. })
  142. }(window.jQuery);