gpt4 book ai didi

jquery - Bootstrap 轮播指示器在点击时不会改变

转载 作者:行者123 更新时间:2023-12-01 02:55:50 24 4
gpt4 key购买 nike

我想修改下面的代码。我已将指示器与 Bootstrap 轮播分开。幻灯片工作正常,但当我单击指示器列表时,图像会相应更改,但指示器的指示没有更改。我尝试通过将函数分配给单击事件来修改它,但徒劳。高级感谢您的帮助


CSS:

.carousel-inner > .item > img {
margin-left: auto;
margin-right: auto;
margin-top: auto;
margin-bottom: auto;
background-color:rgba(128,128,128,0.4);
}
#containerC {
display: table-cell;
vertical-align: middle ;
background-color: grey;
width: 270px;
height:270px;
padding: 10px;
}
.carousel-indicators li {
background-color: #999;
background-color: rgba(70,70,70,.25);
}
.carousel-indicators .active {
background-color: #444;
}


HTML:

<div id="containerC">
<div id="carousel-example-generic" class="carousel" data-ride="carousel">

<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item product active">
<img src="images/Test1.jpg" alt="...">
<div class="carousel-caption">
</div>
</div>
<div class="item product">
<img src="images/Test2.jpg" alt="...">
<div class="carousel-caption">
</div>
</div>
<div class="item product">
<img src="images/Test3.jpg" alt="...">
<div class="carousel-caption">
</div>
</div>
</div>
</div>

<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>


JavaScript:

$(document).ready(function(){
console.log("hello world");
$('#carousel-example-generic').on('slide.bs.carousel', function () {
$holder = $( "ol li.active" );
$holder.next( "li" ).addClass("active");
if($holder.is(':last-child'))
{
$holder.removeClass("active");
$("ol li:first").addClass("active");
}
$holder.removeClass("active");
});

$('.carousel-indicators ol li').on("click",function(){
$('.carousel-indicators ol li.active').removeClass("active");
$(this).addClass("active");
});
});

最佳答案

选择器错误,应该是

$(document).ready(function(){
console.log("hello world");
$('#carousel-example-generic').on('slid.bs.carousel', function () {
$holder = $( "ol li.active" );
$holder.removeClass('active');
var idx = $('div.active').index('div.item');
$('ol.carousel-indicators li[data-slide-to="'+ idx+'"]').addClass('active');
});

$('ol.carousel-indicators li').on("click",function(){
$('ol.carousel-indicators li.active').removeClass("active");
$(this).addClass("active");
});
});

http://fiddle.jshell.net/n26Qy/1/

关于jquery - Bootstrap 轮播指示器在点击时不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22729391/

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