bootstrap-carousel.js 5.67 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* ==========================================================
Mark Otto's avatar
Mark Otto committed
2
 * bootstrap-carousel.js v2.2.3
Jacob Thornton's avatar
Jacob Thornton committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * http://twitter.github.com/bootstrap/javascript.html#carousel
 * ==========================================================
 * Copyright 2012 Twitter, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================== */


21
22
23
!function ($) {

  "use strict"; // jshint ;_;
Jacob Thornton's avatar
Jacob Thornton committed
24
25
26
27
28
29
30


 /* CAROUSEL CLASS DEFINITION
  * ========================= */

  var Carousel = function (element, options) {
    this.$element = $(element)
fat's avatar
fat committed
31
    this.$indicators = this.$element.find('.carousel-indicators')
32
    this.options = options
33
34
35
    this.options.pause == 'hover' && this.$element
      .on('mouseenter', $.proxy(this.pause, this))
      .on('mouseleave', $.proxy(this.cycle, this))
Jacob Thornton's avatar
Jacob Thornton committed
36
37
38
39
  }

  Carousel.prototype = {

40
41
    cycle: function (e) {
      if (!e) this.paused = false
42
      this.options.interval
43
        && !this.paused
44
        && (this.interval = setInterval($.proxy(this.next, this), this.options.interval))
Jacob Thornton's avatar
Jacob Thornton committed
45
46
47
      return this
    }

fat's avatar
fat committed
48
49
50
51
52
53
  , getActiveIndex: function () {
      this.$active = this.$element.find('.item.active')
      this.$items = this.$active.parent().children()
      return this.$items.index(this.$active)
    }

Jacob Thornton's avatar
Jacob Thornton committed
54
  , to: function (pos) {
fat's avatar
fat committed
55
      var activeIndex = this.getActiveIndex()
Jacob Thornton's avatar
Jacob Thornton committed
56
57
        , that = this

fat's avatar
fat committed
58
      if (pos > (this.$items.length - 1) || pos < 0) return
Jacob Thornton's avatar
Jacob Thornton committed
59
60
61
62
63
64
65

      if (this.sliding) {
        return this.$element.one('slid', function () {
          that.to(pos)
        })
      }

fat's avatar
fat committed
66
      if (activeIndex == pos) {
Jacob Thornton's avatar
Jacob Thornton committed
67
68
69
        return this.pause().cycle()
      }

fat's avatar
fat committed
70
      return this.slide(pos > activeIndex ? 'next' : 'prev', $(this.$items[pos]))
Jacob Thornton's avatar
Jacob Thornton committed
71
72
    }

73
74
  , pause: function (e) {
      if (!e) this.paused = true
75
      if (this.$element.find('.next, .prev').length && $.support.transition.end) {
76
77
78
        this.$element.trigger($.support.transition.end)
        this.cycle()
      }
Jacob Thornton's avatar
Jacob Thornton committed
79
      clearInterval(this.interval)
80
      this.interval = null
Jacob Thornton's avatar
Jacob Thornton committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
      return this
    }

  , next: function () {
      if (this.sliding) return
      return this.slide('next')
    }

  , prev: function () {
      if (this.sliding) return
      return this.slide('prev')
    }

  , slide: function (type, next) {
Mark Otto's avatar
Mark Otto committed
95
      var $active = this.$element.find('.item.active')
Jacob Thornton's avatar
Jacob Thornton committed
96
97
98
99
100
        , $next = next || $active[type]()
        , isCycling = this.interval
        , direction = type == 'next' ? 'left' : 'right'
        , fallback  = type == 'next' ? 'first' : 'last'
        , that = this
Jacob Thornton's avatar
Jacob Thornton committed
101
        , e
Jacob Thornton's avatar
Jacob Thornton committed
102
103
104
105
106
107
108

      this.sliding = true

      isCycling && this.pause()

      $next = $next.length ? $next : this.$element.find('.item')[fallback]()

Jacob Thornton's avatar
Jacob Thornton committed
109
110
111
112
      e = $.Event('slide', {
        relatedTarget: $next[0]
      })

Jacob Thornton's avatar
remake    
Jacob Thornton committed
113
114
      if ($next.hasClass('active')) return

fat's avatar
fat committed
115
116
117
      if (this.$indicators.length) {
        this.$indicators.find('.active').removeClass('active')
        this.$element.one('slid', function () {
fat's avatar
fat committed
118
          var $nextIndicator = $(that.$indicators.children()[that.getActiveIndex()])
fat's avatar
fat committed
119
120
121
122
          $nextIndicator && $nextIndicator.addClass('active')
        })
      }

123
      if ($.support.transition && this.$element.hasClass('slide')) {
124
125
        this.$element.trigger(e)
        if (e.isDefaultPrevented()) return
Jacob Thornton's avatar
Jacob Thornton committed
126
127
128
129
130
131
132
133
134
135
        $next.addClass(type)
        $next[0].offsetWidth // force reflow
        $active.addClass(direction)
        $next.addClass(direction)
        this.$element.one($.support.transition.end, function () {
          $next.removeClass([type, direction].join(' ')).addClass('active')
          $active.removeClass(['active', direction].join(' '))
          that.sliding = false
          setTimeout(function () { that.$element.trigger('slid') }, 0)
        })
136
      } else {
137
138
        this.$element.trigger(e)
        if (e.isDefaultPrevented()) return
139
140
141
142
        $active.removeClass('active')
        $next.addClass('active')
        this.sliding = false
        this.$element.trigger('slid')
Jacob Thornton's avatar
Jacob Thornton committed
143
144
145
146
147
148
149
150
151
152
153
154
155
      }

      isCycling && this.cycle()

      return this
    }

  }


 /* CAROUSEL PLUGIN DEFINITION
  * ========================== */

156
157
  var old = $.fn.carousel

158
  $.fn.carousel = function (option) {
Jacob Thornton's avatar
Jacob Thornton committed
159
160
161
    return this.each(function () {
      var $this = $(this)
        , data = $this.data('carousel')
162
        , options = $.extend({}, $.fn.carousel.defaults, typeof option == 'object' && option)
Jacob Thornton's avatar
Jacob Thornton committed
163
        , action = typeof option == 'string' ? option : options.slide
Jacob Thornton's avatar
Jacob Thornton committed
164
165
      if (!data) $this.data('carousel', (data = new Carousel(this, options)))
      if (typeof option == 'number') data.to(option)
Jacob Thornton's avatar
Jacob Thornton committed
166
      else if (action) data[action]()
167
      else if (options.interval) data.cycle()
Jacob Thornton's avatar
Jacob Thornton committed
168
169
170
171
172
    })
  }

  $.fn.carousel.defaults = {
    interval: 5000
173
  , pause: 'hover'
Jacob Thornton's avatar
Jacob Thornton committed
174
175
176
177
178
  }

  $.fn.carousel.Constructor = Carousel


179
180
181
182
183
184
185
186
 /* CAROUSEL NO CONFLICT
  * ==================== */

  $.fn.carousel.noConflict = function () {
    $.fn.carousel = old
    return this
  }

Jacob Thornton's avatar
Jacob Thornton committed
187
188
189
 /* CAROUSEL DATA-API
  * ================= */

190
191
192
  $(document).on('click.carousel.data-api', '[data-slide]', function (e) {
    var $this = $(this), href
      , $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
fat's avatar
fat committed
193
      , options = $.extend({}, $target.data(), $this.data())
194
195
    $target.carousel(options)
    e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
196
197
  })

198
}(window.jQuery);