我想为引导4旋转木马上的每一张幻灯片设置一个单独的数据间隔。我尝试过其他一些javascript片段,但是它们似乎不适用于我的代码,比如Bootstrap 4 Carousel-stack overflow
请任何人提出建议,任何帮助都是非常感谢的。
#top-bootstrap-slider{
width: 80%;
margin: auto;
background: rgb(15,36,62);
color: white;
height: 30px;
margin-top: 0;
overflow: hidden;
font-size: 10px;
}
.carousel-item{
display: flex;
align-items: center;
height: 100%;
line-height: 3vw;
text-align: center;
width: 100%;
}<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<div id="top-bootstrap-slider" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
Testimonial 1
</div>
<div class="carousel-item">
Testimonial 2
</div>
<div class="carousel-item">
Testimonial 3
</div>
</div>
</div>
发布于 2018-12-05 16:12:47
我根据Zim的答案做了一个实现:Bootstrap 4 Carousel: Individual data-interval on each slide,除了旋转木马的起始点(即第一张幻灯片在第一次迭代中使用默认间隔)外,它工作得很好。为了使用这个扩展,必须在间隔的毫秒内将data-interval属性添加到其上的每个carousel-item设置中。请查看下一个示例:
$(document).ready(function()
{
// Extend the Bootstrap carousel implementation.
$.fn.carousel.Constructor.prototype.cycle = function (event)
{
if (!event)
this._isPaused = false;
if (this._interval)
{
clearInterval(this._interval);
this._interval = null;
}
if (this._config.interval && !this._isPaused)
{
var item = $('.carousel-item-next');
var newInterval = item.data('interval') || this._config.interval;
this._interval = setInterval(
(document.visibilityState ? this.nextWhenVisible : this.next).bind(this),
newInterval
);
}
};
});#top-bootstrap-slider{
width: 80%;
margin: auto;
background: rgb(15,36,62);
color: white;
height: 30px;
margin-top: 0;
overflow: hidden;
font-size: 10px;
}
.carousel-item{
display: flex;
align-items: center;
height: 100%;
line-height: 3vw;
text-align: center;
width: 100%;
}<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<div id="top-bootstrap-slider" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active" data-interval="1000">
Testimonial 1
</div>
<div class="carousel-item" data-interval="2000">
Testimonial 2
</div>
<div class="carousel-item" data-interval="5000">
Testimonial 3
</div>
</div>
</div>
或者,如果前面的代码不起作用,您可以在包含<script>引导带文件之后将代码包装到和</script>标记中,如下所示:
#top-bootstrap-slider{
width: 80%;
margin: auto;
background: rgb(15,36,62);
color: white;
height: 30px;
margin-top: 0;
overflow: hidden;
font-size: 10px;
}
.carousel-item{
display: flex;
align-items: center;
height: 100%;
line-height: 3vw;
text-align: center;
width: 100%;
}<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script>
$.fn.carousel.Constructor.prototype.cycle = function (event)
{
if (!event)
this._isPaused = false;
if (this._interval)
{
clearInterval(this._interval);
this._interval = null;
}
if (this._config.interval && !this._isPaused)
{
var item = $('.carousel-item-next');
var newInterval = item.data('interval') || this._config.interval;
this._interval = setInterval(
(document.visibilityState ? this.nextWhenVisible : this.next).bind(this),
newInterval
);
}
};
</script>
<div id="top-bootstrap-slider" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active" data-interval="1000">
Testimonial 1
</div>
<div class="carousel-item" data-interval="2000">
Testimonial 2
</div>
<div class="carousel-item" data-interval="5000">
Testimonial 3
</div>
</div>
</div>
支持多个传送带的更新
下一个示例显示如何为支持多个传送带执行正确的实现。基本上,在选择item时,我们需要使用下一行
var item = $(this._element).find('.carousel-item-next');
#top-bootstrap-slider{
width: 80%;
margin: auto;
background: rgb(15,36,62);
color: white;
height: 30px;
margin-top: 0;
overflow: hidden;
font-size: 10px;
}
#top-bootstrap-slider2{
width: 80%;
margin: auto;
background: skyblue;
color: white;
height: 50px;
margin-top: 25px;
overflow: hidden;
font-size: 14px;
}
.carousel-item{
display: flex;
align-items: center;
height: 100%;
line-height: 3vw;
text-align: center;
width: 100%;
}<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script>
$.fn.carousel.Constructor.prototype.cycle = function (event)
{
if (!event)
this._isPaused = false;
if (this._interval)
{
clearInterval(this._interval);
this._interval = null;
}
if (this._config.interval && !this._isPaused)
{
// This next line does the trick.
var item = $(this._element).find('.carousel-item-next');
var newInterval = item.data('interval') || this._config.interval;
this._interval = setInterval(
(document.visibilityState ? this.nextWhenVisible : this.next).bind(this),
newInterval
);
}
};
</script>
<div id="top-bootstrap-slider" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active" data-interval="1000">
Testimonial 1
</div>
<div class="carousel-item" data-interval="2000">
Testimonial 2
</div>
<div class="carousel-item" data-interval="5000">
Testimonial 3
</div>
</div>
</div>
<div id="top-bootstrap-slider2" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active" data-interval="3000">
Testimonial 4
</div>
<div class="carousel-item" data-interval="1000">
Testimonial 5
</div>
<div class="carousel-item" data-interval="1000">
Testimonial 6
</div>
</div>
</div>
https://stackoverflow.com/questions/53635583
复制相似问题