我正在尝试使用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() );
});
这有点丑陋,但这是一个开始,即使我确定他们是更好的选择。