bootstrap-carousel.js 4.4 KB

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