1 /* ==========================================================
2 * bootstrap-carousel.js v2.0.4
3 * http://twitter.github.com/bootstrap/javascript.html#carousel
4 * ==========================================================
5 * Copyright 2012 Twitter, Inc.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================================================== */
23 "use strict"; // jshint ;_;
26 /* CAROUSEL CLASS DEFINITION
27 * ========================= */
29 var Carousel = function (element, options) {
30 this.$element = $(element)
31 this.options = options
32 this.options.slide && this.slide(this.options.slide)
33 this.options.pause == 'hover' && this.$element
34 .on('mouseenter', $.proxy(this.pause, this))
35 .on('mouseleave', $.proxy(this.cycle, this))
38 Carousel.prototype = {
41 if (!e) this.paused = false
44 && (this.interval = setInterval($.proxy(this.next, this), this.options.interval))
48 , to: function (pos) {
49 var $active = this.$element.find('.active')
50 , children = $active.parent().children()
51 , activePos = children.index($active)
54 if (pos > (children.length - 1) || pos < 0) return
57 return this.$element.one('slid', function () {
62 if (activePos == pos) {
63 return this.pause().cycle()
66 return this.slide(pos > activePos ? 'next' : 'prev', $(children[pos]))
69 , pause: function (e) {
70 if (!e) this.paused = true
71 clearInterval(this.interval)
77 if (this.sliding) return
78 return this.slide('next')
82 if (this.sliding) return
83 return this.slide('prev')
86 , slide: function (type, next) {
87 var $active = this.$element.find('.active')
88 , $next = next || $active[type]()
89 , isCycling = this.interval
90 , direction = type == 'next' ? 'left' : 'right'
91 , fallback = type == 'next' ? 'first' : 'last'
93 , e = $.Event('slide')
97 isCycling && this.pause()
99 $next = $next.length ? $next : this.$element.find('.item')[fallback]()
101 if ($next.hasClass('active')) return
103 if ($.support.transition && this.$element.hasClass('slide')) {
104 this.$element.trigger(e)
105 if (e.isDefaultPrevented()) return
107 $next[0].offsetWidth // force reflow
108 $active.addClass(direction)
109 $next.addClass(direction)
110 this.$element.one($.support.transition.end, function () {
111 $next.removeClass([type, direction].join(' ')).addClass('active')
112 $active.removeClass(['active', direction].join(' '))
114 setTimeout(function () { that.$element.trigger('slid') }, 0)
117 this.$element.trigger(e)
118 if (e.isDefaultPrevented()) return
119 $active.removeClass('active')
120 $next.addClass('active')
122 this.$element.trigger('slid')
125 isCycling && this.cycle()
133 /* CAROUSEL PLUGIN DEFINITION
134 * ========================== */
136 $.fn.carousel = function (option) {
137 return this.each(function () {
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 if (typeof option == 'number') data.to(option)
143 else if (typeof option == 'string' || (option = options.slide)) data[option]()
144 else if (options.interval) data.cycle()
148 $.fn.carousel.defaults = {
153 $.fn.carousel.Constructor = Carousel
157 * ================= */
160 $('body').on('click.carousel.data-api', '[data-slide]', function ( e ) {
161 var $this = $(this), href
162 , $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
163 , options = !$target.data('modal') && $.extend({}, $target.data(), $this.data())
164 $target.carousel(options)