如何控制自举轮播在商品中滑动的速度?


Answers:


114

速度无法由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文件。


1
谢谢回复!我尝试将后者放在我的.html主页面样式中,然后将.6更改为.1,使用chrome似乎没有任何效果。
genxgeek 2013年

不清楚...您尝试修改引导文件还是只是将这些内容添加到自己的CSS中?编辑引导程序文件更安全。否则,您需要确保您的样式最后加载并覆盖引导样式。
德米特里·埃菲缅科2013年

2
只是想指出您不必修改任何javaScript(现在),代码中的那一行现在拉出.css('transition-duration')值,因此您只需要如上所述添加一些CSS,并且而已。(另外,请确保轮播项目具有“幻灯片”类,但不确定文档中是否提到了
该类

5
在Bootstrap 3(v3.3.5,无论如何)中,JS不再从CSS中提取该持续时间值。无需像这样修改Bootstrap即可轻松设置:jQuery.fn.carousel.Constructor.TRANSITION_DURATION = 1000;
Walf 2015年

1
jQuery.fn.carousel.Constructor.TRANSITION_DURATION返回未定义。一个如何改变呢?过渡有效,并且滑动缓慢,但是滑出的图像得到显示:太快了。

93

只需data-intervaldiv包含轮播的内容中写出:

<div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="500">

w3schools为例。


请尝试以更适当的方式来格式化您的问题。顺便说一句。仅链接不被视为好答案。
ckruczek '16

1
警告:“数据间隔”以毫秒为单位。500 = 0,5秒(非常快)。如果要在幻灯片上显示一些文本,则10000(10秒)更好。工作正常。我认为这应该是公认的答案。
Renascienza

19
这个答案是不正确的data-interval确定新幻灯片移动的时间跨度。它不能确定一张幻灯片移动多少时间。
user31782 '17

1
谢谢老兄,它挽救了我的一天。+1,应将此作为正确答案进行检查,而不要编辑bootstrap.css文件。
FONGOH MARTIN

2
我一直在寻找这个确切的答案...但是在我意识到这与幻灯片速度有关之前,我尝试了另一个答案,而这个答案涉及幻灯片之间的间隔。因此,感谢您提供此答案,因为这正是我想要的!(尽管500太快了,但这是另外
一回事了

12

您需要在主DIV中将间隔设置为data-interval标签。它将正常工作,您可以给不同的幻灯片以不同的时间。

<div class="carousel" data-interval="5000">

你能举个例子吗?我需要为差异幻灯片提供不同的时间间隔,这些在我的api调用返回的控制器中进行配置。我们也可以使用滑块显示网页吗?
Archna

1
数据间隔确定新幻灯片移动的时间跨度,而不是动画持续时间。
里卡多·维加蒂

11

对于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)

克里斯,太棒了。谢谢!
genxgeek 2013年

8

我注意到的一件事是,Bootstrap 3正在将样式添加为a.6s0.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;
    }

1
对我不起作用:(它仍然可以像一辆没有改变的汽车一样快速滚动(我改为1.6)
Andiana

7

对于Bootstrap 4,只需使用以下CSS:

.carousel .carousel-item {
    transition-duration: 3s;
}

更改3s为您选择的时间。


1
非常非常好:)
Mohammed Baashar

5

对我来说,我想在我的观点末尾添加:

<script type="text/javascript">
$(document).ready(function(){
     $("#myCarousel").carousel({
         interval : 8000,
         pause: false
     });
});
</script>

轮播间隔为8秒


2

如果您需要通过编程来更改(例如)基于某些条件的速度(也许只是许多轮播之一),则可以执行以下操作:

如果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(...)以包括其他浏览器。


2

包含bootstrap.min.js或未压缩后,您可以将interval作为如下参数添加 jQuery("#numbers").carousel({'interval':900 });对我有用


2

如果您不想更改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” ..
TryingToImprove

更改为200后,我再也看不到平滑的动画了,图像将在过渡过程中
消失-TomSawyer

@TomSawyer,您好-动画是通过CSS维护的,但重要的是TRANSITON_DURATION足够长,可以覆盖整个过渡。在javascript中,它只是定义了何时设置in类以及何时将其再次删除。如果要缩短动画,请检查较少文件中定义的位置。
Andreas


1

什么工作对我来说是改变间隔bootstrap.js

  Carousel.DEFAULTS = {
    interval: 2000,      // <----- change this
    pause: 'hover',
    wrap: true,
    keyboard: true
  }

1
它控制动画的频率,而不是单个动画的速度。
Walf 2015年

1

为了补充先前的答案,在编辑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
}

1

如果您要编辑引导程序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);

1

对于引导4 SCSS,可以覆盖配置变量$carousel-transition-duration在你_variables.scss这样的:

$carousel-transition-duration: 2s;

或为元素个别持续时间设置

.carousel-item {
    transition-duration: 2s;
}

css / scss中特定元素的位置。



0

在您的CSS中:

.carousel-item {
    transition-duration: 1.5s, 1.5s;
}

请注意,时间已包含在为轮播定义的数据间隔中。

希望能帮助到你... :)


0

在您的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'),
                }
            }
        });
    });


-1

如果使用ngCarousel模块,请像下面这样编辑@ ng-bootstrap / ng-bootstrap / carousel-config.js文件中的interval变量:

var NgbCarouselConfig = /** @class */ (function () {
function NgbCarouselConfig() {
    this.interval = 10000;
    this.wrap = true;
    ...
}
...

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.