bootstrap-carousel.js 4.5 KB

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