bootstrap-carousel.js 4.9 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* ==========================================================
Jacob Thornton's avatar
Jacob Thornton committed
2
 * bootstrap-carousel.js v2.0.4
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)
31
    this.options = options
32
    this.options.slide && this.slide(this.options.slide)
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))
45
      return this
46
47
    }

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  , 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]))
    }

69
70
  , pause: function (e) {
      if (!e) this.paused = true
71
      clearInterval(this.interval)
72
      this.interval = null
73
      return this
74
75
    }

Jacob Thornton's avatar
Jacob Thornton committed
76
  , next: function () {
77
      if (this.sliding) return
78
      return this.slide('next')
79
80
    }

Jacob Thornton's avatar
Jacob Thornton committed
81
  , prev: function () {
82
      if (this.sliding) return
83
      return this.slide('prev')
Jacob Thornton's avatar
Jacob Thornton committed
84
    }
85

86
  , slide: function (type, next) {
Jacob Thornton's avatar
Jacob Thornton committed
87
      var $active = this.$element.find('.active')
88
        , $next = next || $active[type]()
89
        , isCycling = this.interval
Jacob Thornton's avatar
Jacob Thornton committed
90
91
92
        , direction = type == 'next' ? 'left' : 'right'
        , fallback  = type == 'next' ? 'first' : 'last'
        , that = this
93
        , e = $.Event('slide')
Jacob Thornton's avatar
Jacob Thornton committed
94

95
96
      this.sliding = true

97
98
      isCycling && this.pause()

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

Jacob Thornton's avatar
Jacob Thornton committed
101
102
      if ($next.hasClass('active')) return

103
      if ($.support.transition && this.$element.hasClass('slide')) {
104
105
        this.$element.trigger(e)
        if (e.isDefaultPrevented()) return
Jacob Thornton's avatar
Jacob Thornton committed
106
107
108
109
110
111
112
        $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(' '))
113
          that.sliding = false
114
          setTimeout(function () { that.$element.trigger('slid') }, 0)
Jacob Thornton's avatar
Jacob Thornton committed
115
        })
116
      } else {
117
118
        this.$element.trigger(e)
        if (e.isDefaultPrevented()) return
119
120
121
122
        $active.removeClass('active')
        $next.addClass('active')
        this.sliding = false
        this.$element.trigger('slid')
Jacob Thornton's avatar
Jacob Thornton committed
123
      }
124
125
126
127

      isCycling && this.cycle()

      return this
128
129
    }

Jacob Thornton's avatar
Jacob Thornton committed
130
131
132
133
134
135
  }


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

136
  $.fn.carousel = function (option) {
Jacob Thornton's avatar
Jacob Thornton committed
137
138
    return this.each(function () {
      var $this = $(this)
139
        , data = $this.data('carousel')
140
        , options = $.extend({}, $.fn.carousel.defaults, typeof option == 'object' && option)
141
      if (!data) $this.data('carousel', (data = new Carousel(this, options)))
142
143
      if (typeof option == 'number') data.to(option)
      else if (typeof option == 'string' || (option = options.slide)) data[option]()
144
      else if (options.interval) data.cycle()
Jacob Thornton's avatar
Jacob Thornton committed
145
146
147
    })
  }

148
  $.fn.carousel.defaults = {
149
    interval: 5000
150
  , pause: 'hover'
151
152
  }

153
  $.fn.carousel.Constructor = Carousel
Jacob Thornton's avatar
Jacob Thornton committed
154

Jacob Thornton's avatar
Jacob Thornton committed
155
156
157
158
159
160

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

  $(function () {
    $('body').on('click.carousel.data-api', '[data-slide]', function ( e ) {
Jacob Thornton's avatar
Jacob Thornton committed
161
162
      var $this = $(this), href
        , $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
163
164
        , options = !$target.data('modal') && $.extend({}, $target.data(), $this.data())
      $target.carousel(options)
Jacob Thornton's avatar
Jacob Thornton committed
165
      e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
166
167
168
    })
  })

169
}(window.jQuery);