]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
add ability to cycle carousel to a particular frame
authorJacob Thornton <jacobthornton@gmail.com>
Sun, 22 Jan 2012 07:02:29 +0000 (23:02 -0800)
committerJacob Thornton <jacobthornton@gmail.com>
Sun, 22 Jan 2012 07:02:29 +0000 (23:02 -0800)
docs/javascript.html
js/bootstrap-carousel.js

index 1b8af662af68109722a5f2c4ffe9ffd9292a6e13..7f589de4c21eb1fc77e98a97524a1ded81341e54 100644 (file)
@@ -1323,6 +1323,8 @@ $('.myCarousel').carousel({
           <p>Cycles through the carousel items from left to right.</p>
           <h4>.carousel('pause')</h4>
           <p>Stops the carousel from cycling through items.</p>
+          <h4>.carousel(number)</h4>
+          <p>Cycles the carousel to a particular frame (0 based, similar to an array).</p>
           <h4>.carousel('prev')</h4>
           <p>Cycles to the previous item.</p>
           <h4>.carousel('next')</h4>
index f2d5110a4e3eb698ec00b2531c6ed0eb0c9c8317..4e12e31b546dcd1e9cb0fe8b9a9dbfc68408fbba 100644 (file)
       return this
     }
 
+  , 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]))
+    }
+
   , pause: function () {
       clearInterval(this.interval)
       return this
@@ -53,9 +74,9 @@
       return this.slide('prev')
     }
 
-  , slide: function (type) {
+  , slide: function (type, next) {
       var $active = this.$element.find('.active')
-        , $next = $active[type]()
+        , $next = next || $active[type]()
         , isCycling = this.interval
         , direction = type == 'next' ? 'left' : 'right'
         , fallback  = type == 'next' ? 'first' : 'last'
@@ -71,8 +92,8 @@
         this.$element.trigger('slide')
         $active.removeClass('active')
         $next.addClass('active')
-        this.$element.trigger('slid')
         this.sliding = false
+        this.$element.trigger('slid')
       } else {
         $next.addClass(type)
         $next[0].offsetWidth // force reflow
         this.$element.one($.support.transition.end, function () {
           $next.removeClass([type, direction].join(' ')).addClass('active')
           $active.removeClass(['active', direction].join(' '))
-          that.$element.trigger('slid')
           that.sliding = false
+          setTimeout(function () { that.$element.trigger('slid') }, 0)
         })
       }
 
         , data = $this.data('carousel')
         , options = typeof option == 'object' && option
       if (!data) $this.data('carousel', (data = new Carousel(this, options)))
-      if (typeof option == 'string' || (option = options.slide)) data[option]()
+      if (typeof option == 'number') data.to(option)
+      else if (typeof option == 'string' || (option = options.slide)) data[option]()
       else data.cycle()
     })
   }