gpt4 book ai didi

javascript - 当元素少于 3 时,旋转木马 slider 停止克隆

转载 作者:行者123 更新时间:2023-11-28 02:15:35 25 4
gpt4 key购买 nike

我正在使用 Bootstrap 轮播 slider ,现在我的 slider 一直在重复。当元素计数少于 3 时,我需要停止重复元素。我尝试了以下脚本。任何人都可以帮助我实现这一目标。

    <div id="carousel" class="carousel slide" data-ride="carousel" data-interval="false">
<div class="carousel-inner">
<div class="item active">
<img src="images/homogeneous/marvel-stone/marvel-stone-detail.jpg" alt="">
</div>
</div>
</div>
<div class="clearfix">
<div id="myCarousel" class="carousel slide" data-interval="false">
<div class="carousel-inner">
<div class="item active">
<div class="thumb"><img src="images/homogeneous/marvel-stone/marvel-stone-thumb1.png" alt=""></div>
</div>
<div class="item">
<div class="thumb"><img src="images/homogeneous/marvel-stone/marvel-stone-thumb2.png" alt=""></div>
</div>
<div class="item">
<div class="thumb"><img src="images/homogeneous/marvel-stone/marvel-stone-thumb3.png" alt=""></div>
</div>
<div class="item">
<div class="thumb"><img src="images/homogeneous/marvel-stone/marvel-stone-thumb4.png" alt=""></div>
</div>
</div>
<!-- /carousel-inner -->
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
<!-- /myCarousel -->
</div>
<!-- /clearfix -->

我尝试了 3 个元素的脚本。但是我不知道如何用简化的脚本编写 2 项和 1 项。

 $('#myCarousel').carousel({
interval: false
});
var totalItems = $('.item').length;
if (totalItems > 3) {
//alert();
$('.carousel .item').each(function() {
var next = $(this).next();
if (!next.length) {
next = $(this).siblings(':first');
}
next.children(':first-child').clone().appendTo($(this));

if (next.next().length > 0) {

next.next().children(':first-child').clone().appendTo($(this)).addClass('rightest');

} else {
$(this).siblings(':first').children(':first-child').clone().appendTo($(this));

}
});
} else {

//what to be here
}

my code here

最佳答案

这个为我工作。

$('#myCarousel').carousel({ 
interval: false
});
var totalItems = $('.item').length;
if (totalItems > 3) {
//alert();
$('.carousel .item').each(function(){
var next = $(this).next();
if (!next.length) {
next = $(this).siblings(':first');
}
next.children(':first-child').clone().appendTo($(this));

if (next.next().length>0) {

next.next().children(':first-child').clone().appendTo($(this)).addClass('rightest');

}
else {
$(this).siblings(':first').children(':first-child').clone().appendTo($(this));

}
});
}
else {
//alert("hello");
(function(){
$('.carousel .item').each(function(){
var itemToClone = $(this);

for (var i=1;i<2;i++) {
itemToClone = itemToClone.next();
// wrap around if at end of item collection
if (!itemToClone.length) {
itemToClone = $(this).siblings(':first');
}
// grab item, clone, add marker class, add to collection
itemToClone.children(':first-child').clone()
.addClass("cloneditem-"+(i))
.appendTo($(this));

//listener for after slide
jQuery('.carousel').on('slid.bs.carousel', function(){

//Each slide has a .item class to it, you can get the total number of slides like this
var totalItems = jQuery('.carousel .item').length;

//find current slide number
var currentIndex = jQuery('.carousel .item div.active').index() + 1;

//if slide number is last then stop carousel
if(totalItems == currentIndex){

clearInterval(jQuery('.carousel .item').data('bs.carousel').interval);

} // end of if

});

}

});
}());
}

关于javascript - 当元素少于 3 时,旋转木马 slider 停止克隆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48493309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com