bootstrap-carousel.js 4.6 KB

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