bootstrap-carousel.js 4.9 KB

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