bootstrap-carousel.js 3.69 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* ==========================================================
 * bootstrap-carousel.js v2.0.0
 * http://twitter.github.com/bootstrap/javascript.html#alerts
 * ==========================================================
 * Copyright 2011 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.
 * ========================================================== */


!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
    }

  , pause: function () {
      clearInterval(this.interval)
43
      return this
44
45
    }

Jacob Thornton's avatar
Jacob Thornton committed
46
  , next: function () {
47
      if (this.sliding) return
48
      return this.slide('next')
49
50
    }

Jacob Thornton's avatar
Jacob Thornton committed
51
  , prev: function () {
52
      if (this.sliding) return
53
      return this.slide('prev')
Jacob Thornton's avatar
Jacob Thornton committed
54
    }
55

Jacob Thornton's avatar
Jacob Thornton committed
56
57
58
  , slide: function (type) {
      var $active = this.$element.find('.active')
        , $next = $active[type]()
59
        , isCycling = this.interval
Jacob Thornton's avatar
Jacob Thornton committed
60
61
62
63
        , direction = type == 'next' ? 'left' : 'right'
        , fallback  = type == 'next' ? 'first' : 'last'
        , that = this

64
65
      this.sliding = true

66
67
      isCycling && this.pause()

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

      if (!$.support.transition && this.$element.hasClass('slide')) {
71
        this.$element.trigger('slide')
Jacob Thornton's avatar
Jacob Thornton committed
72
73
        $active.removeClass('active')
        $next.addClass('active')
74
75
        this.$element.trigger('slid')
        this.sliding = false
Jacob Thornton's avatar
Jacob Thornton committed
76
77
78
79
80
      } else {
        $next.addClass(type)
        $next[0].offsetWidth // force reflow
        $active.addClass(direction)
        $next.addClass(direction)
81
        this.$element.trigger('slide')
Jacob Thornton's avatar
Jacob Thornton committed
82
83
84
        this.$element.one($.support.transition.end, function () {
          $next.removeClass([type, direction].join(' ')).addClass('active')
          $active.removeClass(['active', direction].join(' '))
85
86
          that.$element.trigger('slid')
          that.sliding = false
Jacob Thornton's avatar
Jacob Thornton committed
87
88
        })
      }
89
90
91
92

      isCycling && this.cycle()

      return this
93
94
    }

Jacob Thornton's avatar
Jacob Thornton committed
95
96
97
98
99
100
101
102
103
  }


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

  $.fn.carousel = function ( option ) {
    return this.each(function () {
      var $this = $(this)
104
        , data = $this.data('carousel')
105
106
107
108
        , options = typeof option == 'object' && option
      if (!data) $this.data('carousel', (data = new Carousel(this, options)))
      if (typeof option == 'string' || (option = options.slide)) data[option]()
      else data.cycle()
Jacob Thornton's avatar
Jacob Thornton committed
109
110
111
    })
  }

112
  $.fn.carousel.defaults = {
113
    interval: 5000
114
115
  }

116
  $.fn.carousel.Constructor = Carousel
Jacob Thornton's avatar
Jacob Thornton committed
117

Jacob Thornton's avatar
Jacob Thornton committed
118
119
120
121
122
123
124
125

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

  $(function () {
    $('body').on('click.carousel.data-api', '[data-slide]', function ( e ) {
      var $this = $(this)
        , $target = $($this.attr('data-target') || $this.attr('href'))
126
127
        , options = !$target.data('modal') && $.extend({}, $target.data(), $this.data())
      $target.carousel(options)
Jacob Thornton's avatar
Jacob Thornton committed
128
129
130
    })
  })

131
}( window.jQuery )