我看到您可以设置间隔,但是我想控制项目滑动的速度?
// Sets interval...what is transition slide speed?
$('#mainCarousel').carousel({
interval: 3000
});
Answers:
速度无法由API控制。尽管您可以修改负责此功能的CSS。在carousel.less
文件中找到
.item {
display: none;
position: relative;
.transition(.6s ease-in-out left);
}
并更改.6s
为您想要的任何内容。
如果您不使用.less,请在bootstrap.css
文件中查找:
.carousel-inner > .item {
position: relative;
display: none;
-webkit-transition: 0.6s ease-in-out left;
-moz-transition: 0.6s ease-in-out left;
-o-transition: 0.6s ease-in-out left;
transition: 0.6s ease-in-out left;
}
并更改0.6s
为您想要的时间。您可能还想在下面的函数调用中编辑时间:
.emulateTransitionEnd(2000)
在bootstrap.js
函数Carousel.prototype.slide
。这样可以同步过渡,并防止滑动在过渡结束前消失。
编辑7/8/2014
正如@YellowShark指出的那样,不再需要在JS中进行编辑。仅应用CSS更改。
编辑20/8/2015 现在,在编辑CSS文件之后,您只需要编辑CAROUSEL.TRANSITION_DURATION(在bootstrap.js中)或c.TRANSITION_DURATION(如果您使用bootstrap.min.js)并更改其中的值(默认为600)。最终值必须与您放入CSS文件中的值相同(例如,cs中的10s = .js中的10000)
编辑16/01/2018 对于Bootstrap 4,将过渡时间更改为例如2秒,请添加
$(document).ready(function() {
jQuery.fn.carousel.Constructor.TRANSITION_DURATION = 2000 // 2 seconds
});
到您网站的JS文件,以及
.carousel-inner .carousel-item {
transition: -webkit-transform 2s ease;
transition: transform 2s ease;
transition: transform 2s ease, -webkit-transform 2s ease;
}
到您网站的CSS文件。
jQuery.fn.carousel.Constructor.TRANSITION_DURATION = 1000;
只需data-interval
在div
包含轮播的内容中写出:
<div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="500">
以w3schools为例。
data-interval
确定新幻灯片移动的时间跨度。它不能确定一张幻灯片移动多少时间。
对于Twitter Bootstrap 3:
您必须按照其他答案中的说明更改CSS过渡:
.carousel-inner > .item {
position: relative;
display: none;
-webkit-transition: 0.6s ease-in-out left;
-moz-transition: 0.6s ease-in-out left;
-o-transition: 0.6s ease-in-out left;
transition: 0.6s ease-in-out left;
}
从0.6秒到1.5秒(例如)。
而且,还有一些Java脚本需要更改。在bootstrap.js中有一行:
.emulateTransitionEnd(600)
应将其更改为相应的毫秒数。因此,在1.5秒内,您将数字更改为1500:
.emulateTransitionEnd(1500)
我注意到的一件事是,Bootstrap 3正在将样式添加为a.6s
和0.6s
。因此,您可能需要像这样(CSS)明确定义过渡时间
.carousel-inner>.item {
-webkit-transition: 0.9s ease-in-out left;
transition: 0.9s ease-in-out left;
-webkit-transition: 0.9s, ease-in-out, left;
-moz-transition: .9s, ease-in-out, left;
-o-transition: .9s, ease-in-out, left;
transition: .9s, ease-in-out, left;
}
对于Bootstrap 4,只需使用以下CSS:
.carousel .carousel-item {
transition-duration: 3s;
}
更改3s
为您选择的时间。
如果您需要通过编程来更改(例如)基于某些条件的速度(也许只是许多轮播之一),则可以执行以下操作:
如果HTML是这样的:
<div id="theSlidesList" class="carousel-inner" role="listbox">
<div id="Slide_00" class="item active"> ...
<div id="Slide_01" class="item"> ...
...
</div>
JavaScript可能是这样的:
$( "#theSlidesList" ).find( ".item" ).css( "-webkit-transition", "transform 1.9s ease-in-out 0s" ).css( "transition", "transform 1.9s ease-in-out 0s" )
添加更多.css(...)以包括其他浏览器。
如果您不想更改bootstrap的js文件,也可以直接将所需的值注入到jquery插件(bootsrap 3.3.6)中。
当然,这需要通过js而不是直接通过data-ride属性手动激活轮播。
例如:
var interval = 3500;
$.fn.carousel.Constructor.TRANSITION_DURATION = interval - 500;
elem.carousel({
interval : interval
});
bootstrap.js
,并使其“unupdateable” ..
in
类以及何时将其再次删除。如果要缩短动画,请检查较少文件中定义的位置。
无需任何外部代码即可使用 data-interval=""
属性
更多信息,请访问getbootstrap
<div id="carouselExampleCaptions" class="carousel slide" data-ride="carousel" data-interval="2500">
当你想改变速度改变“ 2500”
什么工作对我来说是改变间隔在bootstrap.js
Carousel.DEFAULTS = {
interval: 2000, // <----- change this
pause: 'hover',
wrap: true,
keyboard: true
}
为了补充先前的答案,在编辑CSS文件之后,您只需要编辑CAROUSEL.TRANSITION_DURATION
(在bootstrap.js中)或c.TRANSITION_DURATION
(如果使用bootstrap.min.js)并更改其中的值(默认值为600)。最终值必须与您放入CSS文件的值相同(例如,CSS中的10s = .js中的10000)
Carousel.VERSION = '3.3.2'
Carousel.TRANSITION_DURATION = xxxxx /* Your number here*/
Carousel.DEFAULTS = {
interval: 5000 /* you could change this value too, but to add data-interval="xxxx" to your html it's fine too*/
pause: 'hover',
wrap: true,
keyboard: true
}
如果您要编辑引导程序3.3.5的幻灯片进出速度(不是间隔更改幻灯片之间的时间,称为间隔),加载CDN引导程序样式后,使用以下类在您自己的CSS样式中覆盖样式。1.5是时间变化。
.carousel-inner > .item {
-webkit-transition: 1.5s ease-in-out ;
-o-transition: 1.5s ease-in-out ;
transition: 1.5s ease-in-out ;
}
.carousel-inner > .item {
-webkit-transition: -webkit-transform 1.5s ease-in-out;
-o-transition: -o-transform 1.5s ease-in-out;
transition: transform 1.5s ease-in-out;
}
之后,您将需要替换javascript中的轮播功能。为此,您将在加载后覆盖默认的bootstrap.min.js函数。(我认为直接覆盖引导文件不是一个好主意)。因此,创建一个mynewscript.js并将其加载到bootstrap.min.js之后,并添加新的轮播功能。您要编辑的唯一一行是Carousel.TRANSITION_DURATION =1500。1500是1.5。希望这可以帮助。
+function ($) {
'use strict';
// CAROUSEL CLASS DEFINITION
// =========================
var Carousel = function (element, options) {
this.$element = $(element)
this.$indicators = this.$element.find('.carousel-indicators')
this.options = options
this.paused = null
this.sliding = null
this.interval = null
this.$active = null
this.$items = null
this.options.keyboard && this.$element.on('keydown.bs.carousel', $.proxy(this.keydown, this))
this.options.pause == 'hover' && !('ontouchstart' in document.documentElement) && this.$element
.on('mouseenter.bs.carousel', $.proxy(this.pause, this))
.on('mouseleave.bs.carousel', $.proxy(this.cycle, this))
}
Carousel.VERSION = '3.3.5'
Carousel.TRANSITION_DURATION = 1500
Carousel.DEFAULTS = {
interval: 5000,
pause: 'hover',
wrap: true,
keyboard: true
}
Carousel.prototype.keydown = function (e) {
if (/input|textarea/i.test(e.target.tagName)) return
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 activeIndex = this.getItemIndex(active)
var willWrap = (direction == 'prev' && activeIndex === 0)
|| (direction == 'next' && activeIndex == (this.$items.length - 1))
if (willWrap && !this.options.wrap) return active
var delta = direction == 'prev' ? -1 : 1
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 that = this
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
// =================
var clickHandler = 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()
}
$(document)
.on('click.bs.carousel.data-api', '[data-slide]', clickHandler)
.on('click.bs.carousel.data-api', '[data-slide-to]', clickHandler)
$(window).on('load', function () {
$('[data-ride="carousel"]').each(function () {
var $carousel = $(this)
Plugin.call($carousel, $carousel.data())
})
})
}(jQuery);
所有轮播
<script type="text/javascript">
$(document).ready(function () {
$('.carousel').carousel({
interval: 15000
})
});
</script>
在您的CSS中:
.carousel-item {
transition-duration: 1.5s, 1.5s;
}
请注意,时间已包含在为轮播定义的数据间隔中。
希望能帮助到你... :)
在您的main.js文件或bootstrap.js中,更改autoplayTimeout的值
$('.carousel').each(function () {
$(this).owlCarousel({
nav: $(this).data('nav'),
dots: $(this).data('dots'),
loop: $(this).data('loop'),
margin: $(this).data('space'),
center: $(this).data('center'),
dotsSpeed: $(this).data('speed'),
autoplay: $(this).data('autoplay'),
transitionStyle: $(this).data('transition'),
animateOut: $(this).data('animate-out'),
animateIn: $(this).data('animate-in'),
autoplayTimeout: 3000,
responsive: {
0: {
items: 1,
},
400: {
items: $(this).data('slide-sm'),
},
700: {
items: $(this).data('slide-md'),
},
1000: {
items: $(this).data('slide'),
}
}
});
});
添加数据间隔
data-interval="20000"