簡體   English   中英

Bootstrap Carousel的多個輪播指標

[英]Multiple carousel-indicators for Bootstrap Carousel

我正在嘗試用Bootstrap 3.2制作兩個輪播指標,事實證明,相對而言,我的JavaScript初學者比較難。 我嘗試添加兩個輪播指標列表項,但這僅對一組指標有效。 如何為一個輪播使用兩個輪播指示器? 謝謝!

Bootstrap中的JavaScript。

+function ($) {
  'use strict';

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

  var Carousel = function (element, options) {
    this.$element    = $(element).on('keydown.bs.carousel', $.proxy(this.keydown, this))
    this.$indicators = this.$element.find('.carousel-indicators')
    this.options     = options
    this.paused      =
    this.sliding     =
    this.interval    =
    this.$active     =
    this.$items      = null

    this.options.pause == 'hover' && this.$element
      .on('mouseenter.bs.carousel', $.proxy(this.pause, this))
      .on('mouseleave.bs.carousel', $.proxy(this.cycle, this))
  }

  Carousel.VERSION  = '3.2.0'

  Carousel.TRANSITION_DURATION = 600

  Carousel.DEFAULTS = {
    interval: 5000,
    pause: 'hover',
    wrap: true
  }

  Carousel.prototype.keydown = function (e) {
    switch (e.which) {
      case 37: this.prev(); break
      case 39: this.next(); break
      default: return
    }

    e.preventDefault()
  }

  Carousel.prototype.cycle = function (e) {
    e || (this.paused = false)

    this.interval && clearInterval(this.interval)

    this.options.interval
      && !this.paused
      && (this.interval = setInterval($.proxy(this.next, this), this.options.interval))

    return this
  }

  Carousel.prototype.getItemIndex = function (item) {
    this.$items = item.parent().children('.item')
    return this.$items.index(item || this.$active)
  }

  Carousel.prototype.getItemForDirection = function (direction, active) {
    var delta = direction == 'prev' ? -1 : 1
    var activeIndex = this.getItemIndex(active)
    var itemIndex = (activeIndex + delta) % this.$items.length
    return this.$items.eq(itemIndex)
  }

  Carousel.prototype.to = function (pos) {
    var that        = this
    var activeIndex = this.getItemIndex(this.$active = this.$element.find('.item.active'))

    if (pos > (this.$items.length - 1) || pos < 0) return

    if (this.sliding)       return this.$element.one('slid.bs.carousel', function () { that.to(pos) }) // yes, "slid"
    if (activeIndex == pos) return this.pause().cycle()

    return this.slide(pos > activeIndex ? 'next' : 'prev', this.$items.eq(pos))
  }

  Carousel.prototype.pause = function (e) {
    e || (this.paused = true)

    if (this.$element.find('.next, .prev').length && $.support.transition) {
      this.$element.trigger($.support.transition.end)
      this.cycle(true)
    }

    this.interval = clearInterval(this.interval)

    return this
  }

  Carousel.prototype.next = function () {
    if (this.sliding) return
    return this.slide('next')
  }

  Carousel.prototype.prev = function () {
    if (this.sliding) return
    return this.slide('prev')
  }

  Carousel.prototype.slide = function (type, next) {
    var $active   = this.$element.find('.item.active')
    var $next     = next || this.getItemForDirection(type, $active)
    var isCycling = this.interval
    var direction = type == 'next' ? 'left' : 'right'
    var fallback  = type == 'next' ? 'first' : 'last'
    var that      = this

    if (!$next.length) {
      if (!this.options.wrap) return
      $next = this.$element.find('.item')[fallback]()
    }

    if ($next.hasClass('active')) return (this.sliding = false)

    var relatedTarget = $next[0]
    var slideEvent = $.Event('slide.bs.carousel', {
      relatedTarget: relatedTarget,
      direction: direction
    })
    this.$element.trigger(slideEvent)
    if (slideEvent.isDefaultPrevented()) return

    this.sliding = true

    isCycling && this.pause()

    if (this.$indicators.length) {
      this.$indicators.find('.active').removeClass('active')
      var $nextIndicator = $(this.$indicators.children()[this.getItemIndex($next)])
      $nextIndicator && $nextIndicator.addClass('active')
    }

    var slidEvent = $.Event('slid.bs.carousel', { relatedTarget: relatedTarget, direction: direction }) // yes, "slid"
    if ($.support.transition && this.$element.hasClass('slide')){
      $next.addClass(type)
      $next[0].offsetWidth // force reflow
      $active.addClass(direction)
      $next.addClass(direction)
      $active
        .one('bsTransitionEnd', function () {
          $next.removeClass([type, direction].join(' ')).addClass('active')
          $active.removeClass(['active', direction].join(' '))
          that.sliding = false
          setTimeout(function () {
            that.$element.trigger(slidEvent)
          }, 0)
        })
        .emulateTransitionEnd(Carousel.TRANSITION_DURATION)
    } else {
      $active.removeClass('active')
      $next.addClass('active')
      this.sliding = false
      this.$element.trigger(slidEvent)
    }

    isCycling && this.cycle()

    return this
  }


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

  function Plugin(option) {
    return this.each(function () {
      var $this   = $(this)
      var data    = $this.data('bs.carousel')
      var options = $.extend({}, Carousel.DEFAULTS, $this.data(), typeof option == 'object' && option)
      var action  = typeof option == 'string' ? option : options.slide

      if (!data) $this.data('bs.carousel', (data = new Carousel(this, options)))
      if (typeof option == 'number') data.to(option)
      else if (action) data[action]()
      else if (options.interval) data.pause().cycle()
    })
  }

  var old = $.fn.carousel

  $.fn.carousel             = Plugin
  $.fn.carousel.Constructor = Carousel


  // CAROUSEL NO CONFLICT
  // ====================

  $.fn.carousel.noConflict = function () {
    $.fn.carousel = old
    return this
  }


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

  $(document).on('click.bs.carousel.data-api', '[data-slide], [data-slide-to]', function (e) {
    var href
    var $this   = $(this)
    var $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) // strip for ie7
    if (!$target.hasClass('carousel')) return
    var options = $.extend({}, $target.data(), $this.data())
    var slideIndex = $this.attr('data-slide-to')
    if (slideIndex) options.interval = false

    Plugin.call($target, options)

    if (slideIndex) {
      $target.data('bs.carousel').to(slideIndex)
    }

    e.preventDefault()
  })

  $(window).on('load', function () {
    $('[data-ride="carousel"]').each(function () {
      var $carousel = $(this)
      Plugin.call($carousel, $carousel.data())
    })
  })

}(jQuery);

HTML標記

      <div id="slider1" class="carousel slide" data-ride="carousel">
            <!-- Indicators -->
            <ol class="carousel-indicators">
                <li data-target="#slider1" data-slide-to="0" class="active"></li>
                <li data-target="#slider1" data-slide-to="1"></li>
                <li data-target="#slider1" data-slide-to="2"></li>
                <li data-target="#slider1" data-slide-to="3"></li>
                <li data-target="#slider1" data-slide-to="4"></li>
                <li data-target="#slider1" data-slide-to="5"></li>
            </ol>

            <!-- Wrapper for slides -->
            <div class="carousel-inner">
                <div class="item active">
                    <img src="assets/images/slider1image1.png">
                    <div class="carousel-caption">
                        <p class="new-products">NEW PRODUCTS</p>
                        <h2>CYCLONE DRY/WET VACUUM BRAZED CORE DRILL BIT</h2>

                    </div>
                </div>
                <div class="item">
                    <img src="assets/images/slider1image1.png">
                    <div class="carousel-caption">
                        <p class="new-products">NEW PRODUCTS</p>
                        <h2>CYCLONE DRY/WET VACUUM BRAZED CORE DRILL BIT</h2>

                    </div>
                </div>
                <div class="item">
                    <img src="assets/images/slider1image1.png">
                    <div class="carousel-caption">
                        <p class="new-products">NEW PRODUCTS</p>
                        <h2>CYCLONE DRY/WET VACUUM BRAZED CORE DRILL BIT</h2>

                    </div>
                </div>
                <div class="item">
                    <img src="assets/images/slider1image1.png">
                    <div class="carousel-caption">
                        <p class="new-products">NEW PRODUCTS</p>
                        <h2>CYCLONE DRY/WET VACUUM BRAZED CORE DRILL BIT</h2>
                </div>

            </div>

            <!-- Controls -->
            <a class="left carousel-control" href="#slider1" role="button" data-slide="prev">
                <img src="assets/images/slider1-caret-left.png">
            </a>
            <a class="right carousel-control" href="#slider1" role="button" data-slide="next">
                <img src="assets/images/slider1-caret-right.png">
            </a

對於我來說,我的解決方案並不完美,但可能會對您有所幫助:)

我使用的功能:

$('#carousel-example-generic').on('slide.bs.carousel', function () {
 // stuffs
} 

當下一個輪播項目要更改時,將調用此操作。 然后,您可以添加自己的代碼,更改第二組不可更改的指標的CSS。

我的例子:

$('#carousel-example-generic').on('slide.bs.carousel', function () {

    var slide_number = $('.carousel-indicators_custom')
                      .children('.active')
                      .children('.losange').attr('data-slide-to');

    // here slide number need to be parseInt and you'll certainly need to add +1 value

    $('.carousel-indicators_custom-mobile>li').css('color', '#333');
    $('.carousel-indicators_custom-mobile').find('li[data-slide-to=' + slide_number +']').css('color', '#be1522');
    $('.dynamic_puce__text').html( $('.carousel-indicators_custom-mobile').find('li[data-slide-to=' + slide_number +']').html() );
  });

這有點難看,但這是一個開始,即使我確定他們是更好的選擇。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM