bootstrap-carousel.js 5.91 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* ==========================================================
Mark Otto's avatar
Mark Otto committed
2
 * bootstrap-carousel.js v2.3.0
Jon Stevens's avatar
Jon Stevens committed
3
 * http://twitter.github.com/bootstrap/javascript.html#carousel
Jacob Thornton's avatar
Jacob Thornton committed
4
 * ==========================================================
Mark Otto's avatar
Mark Otto committed
5
 * Copyright 2012 Twitter, Inc.
Jacob Thornton's avatar
Jacob Thornton committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * 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


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

29
  var Carousel = function (element, options) {
30
    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
fat's avatar
fat committed
42
      if (this.interval) clearInterval(this.interval);
43
      this.options.interval
44
        && !this.paused
45
        && (this.interval = setInterval($.proxy(this.next, this), this.options.interval))
46
      return this
47
48
    }

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

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

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

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

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

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

74
75
  , pause: function (e) {
      if (!e) this.paused = true
76
      if (this.$element.find('.next, .prev').length && $.support.transition.end) {
77
78
79
        this.$element.trigger($.support.transition.end)
        this.cycle()
      }
80
      clearInterval(this.interval)
81
      this.interval = null
82
      return this
83
84
    }

Jacob Thornton's avatar
Jacob Thornton committed
85
  , next: function () {
86
      if (this.sliding) return
87
      return this.slide('next')
88
89
    }

Jacob Thornton's avatar
Jacob Thornton committed
90
  , prev: function () {
91
      if (this.sliding) return
92
      return this.slide('prev')
Jacob Thornton's avatar
Jacob Thornton committed
93
    }
94

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

104
105
      this.sliding = true

106
107
      isCycling && this.pause()

Jacob Thornton's avatar
Jacob Thornton committed
108
109
      $next = $next.length ? $next : this.$element.find('.item')[fallback]()

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

Jacob Thornton's avatar
Jacob Thornton committed
115
116
      if ($next.hasClass('active')) return

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

125
      if ($.support.transition && this.$element.hasClass('slide')) {
126
127
        this.$element.trigger(e)
        if (e.isDefaultPrevented()) return
Jacob Thornton's avatar
Jacob Thornton committed
128
129
130
131
132
133
134
        $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(' '))
135
          that.sliding = false
136
          setTimeout(function () { that.$element.trigger('slid') }, 0)
Jacob Thornton's avatar
Jacob Thornton committed
137
        })
138
      } else {
139
140
        this.$element.trigger(e)
        if (e.isDefaultPrevented()) return
141
142
143
144
        $active.removeClass('active')
        $next.addClass('active')
        this.sliding = false
        this.$element.trigger('slid')
Jacob Thornton's avatar
Jacob Thornton committed
145
      }
146
147
148
149

      isCycling && this.cycle()

      return this
150
151
    }

Jacob Thornton's avatar
Jacob Thornton committed
152
153
154
155
156
157
  }


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

158
159
  var old = $.fn.carousel

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

173
  $.fn.carousel.defaults = {
174
    interval: 5000
175
  , pause: 'hover'
176
177
  }

178
  $.fn.carousel.Constructor = Carousel
Jacob Thornton's avatar
Jacob Thornton committed
179

Jacob Thornton's avatar
Jacob Thornton committed
180

181
182
183
184
185
186
187
188
 /* CAROUSEL NO CONFLICT
  * ==================== */

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

Jacob Thornton's avatar
Jacob Thornton committed
189
190
191
 /* CAROUSEL DATA-API
  * ================= */

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

198
    $target.carousel(options)
199
200
201
202
203

    if (slideIndex = $this.attr('data-slide-to')) {
      $target.data('carousel').pause().to(slideIndex).cycle()
    }

204
    e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
205
206
  })

207
}(window.jQuery);