bootstrap-carousel.js 4.46 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* ==========================================================
Jacob Thornton's avatar
2.0.1    
Jacob Thornton committed
2
 * bootstrap-carousel.js v2.0.1
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
21
22
23
24
25
26
27
 *
 * 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.
 * ========================================================== */


!function( $ ){

  "use strict"

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

28
  var Carousel = function (element, options) {
29
    this.$element = $(element)
30
31
    this.options = $.extend({}, $.fn.carousel.defaults, options)
    this.options.slide && this.slide(this.options.slide)
Jacob Thornton's avatar
Jacob Thornton committed
32
33
34
35
  }

  Carousel.prototype = {

36
    cycle: function () {
37
      this.interval = setInterval($.proxy(this.next, this), this.options.interval)
38
      return this
39
40
    }

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  , to: function (pos) {
      var $active = this.$element.find('.active')
        , children = $active.parent().children()
        , activePos = children.index($active)
        , that = this

      if (pos > (children.length - 1) || pos < 0) return

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

      if (activePos == pos) {
        return this.pause().cycle()
      }

      return this.slide(pos > activePos ? 'next' : 'prev', $(children[pos]))
    }

62
63
  , pause: function () {
      clearInterval(this.interval)
64
      this.interval = null
65
      return this
66
67
    }

Jacob Thornton's avatar
Jacob Thornton committed
68
  , next: function () {
69
      if (this.sliding) return
70
      return this.slide('next')
71
72
    }

Jacob Thornton's avatar
Jacob Thornton committed
73
  , prev: function () {
74
      if (this.sliding) return
75
      return this.slide('prev')
Jacob Thornton's avatar
Jacob Thornton committed
76
    }
77

78
  , slide: function (type, next) {
Jacob Thornton's avatar
Jacob Thornton committed
79
      var $active = this.$element.find('.active')
80
        , $next = next || $active[type]()
81
        , isCycling = this.interval
Jacob Thornton's avatar
Jacob Thornton committed
82
83
84
85
        , direction = type == 'next' ? 'left' : 'right'
        , fallback  = type == 'next' ? 'first' : 'last'
        , that = this

Jacob Thornton's avatar
Jacob Thornton committed
86
87
      if (!$next.length) return

88
89
      this.sliding = true

90
91
      isCycling && this.pause()

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

      if (!$.support.transition && this.$element.hasClass('slide')) {
95
        this.$element.trigger('slide')
Jacob Thornton's avatar
Jacob Thornton committed
96
97
        $active.removeClass('active')
        $next.addClass('active')
98
        this.sliding = false
99
        this.$element.trigger('slid')
Jacob Thornton's avatar
Jacob Thornton committed
100
101
102
103
104
      } else {
        $next.addClass(type)
        $next[0].offsetWidth // force reflow
        $active.addClass(direction)
        $next.addClass(direction)
105
        this.$element.trigger('slide')
Jacob Thornton's avatar
Jacob Thornton committed
106
107
108
        this.$element.one($.support.transition.end, function () {
          $next.removeClass([type, direction].join(' ')).addClass('active')
          $active.removeClass(['active', direction].join(' '))
109
          that.sliding = false
110
          setTimeout(function () { that.$element.trigger('slid') }, 0)
Jacob Thornton's avatar
Jacob Thornton committed
111
112
        })
      }
113
114
115
116

      isCycling && this.cycle()

      return this
117
118
    }

Jacob Thornton's avatar
Jacob Thornton committed
119
120
121
122
123
124
125
126
127
  }


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

  $.fn.carousel = function ( option ) {
    return this.each(function () {
      var $this = $(this)
128
        , data = $this.data('carousel')
129
130
        , options = typeof option == 'object' && option
      if (!data) $this.data('carousel', (data = new Carousel(this, options)))
131
132
      if (typeof option == 'number') data.to(option)
      else if (typeof option == 'string' || (option = options.slide)) data[option]()
133
      else data.cycle()
Jacob Thornton's avatar
Jacob Thornton committed
134
135
136
    })
  }

137
  $.fn.carousel.defaults = {
138
    interval: 5000
139
140
  }

141
  $.fn.carousel.Constructor = Carousel
Jacob Thornton's avatar
Jacob Thornton committed
142

Jacob Thornton's avatar
Jacob Thornton committed
143
144
145
146
147
148

 /* CAROUSEL DATA-API
  * ================= */

  $(function () {
    $('body').on('click.carousel.data-api', '[data-slide]', function ( e ) {
Jacob Thornton's avatar
Jacob Thornton committed
149
150
      var $this = $(this), href
        , $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
151
152
        , options = !$target.data('modal') && $.extend({}, $target.data(), $this.data())
      $target.carousel(options)
Jacob Thornton's avatar
Jacob Thornton committed
153
      e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
154
155
156
    })
  })

157
}( window.jQuery );