我正在使用 Bootstrap Carousel,并且根据 if .item
添加了一个 testing
类到 .link-txt
是.active
。但是,一旦从特定的 .item
中删除 .active
,我想从 .link-txt
中删除该类。我似乎无法使用 if hasClass
这是到目前为止我的 jQuery:
$('#home-carousel').on('slid.bs.carousel', function() {
if ($('.item').hasClass('active')) {
$('.active .link-txt').addClass('testing');
} else {
$('.link-txt').removeClass('testing');
}
});
HTML:
<?php
if( have_rows('carousel') ):?>
<div id="home-carousel" class="carousel bs-slider fade control-round indicators-line" data-ride="carousel" data-interval="6000">
<div class="carousel-inner" role="listbox">
<?php
$i=0;
while ( have_rows('carousel') ) : the_row();?>
<div class="item <?php if($i === 0) { ?> active <?php } ?>" style="background-image:url(<?php the_sub_field('image'); ?>);">
<div class="carousel-caption">
<div class="col-md-6 ">
<h1><a href="<?php the_sub_field('link');?>"><?php the_sub_field('heading');?></a></h1>
<a href="<?php the_sub_field('link');?>" class="link-txt">View</a>
</div>
</div>
</div>
<?php
++$i;
endwhile; ?>
</div>
</div>
<?php endif;?>
除了无法删除类testing
....我还发现第一张带有.active
的幻灯片被添加到HTML中,但这并没有'似乎是用 jQuery 检测到的。这意味着直到第二张幻灯片才添加该类。有没有办法检测第一张幻灯片上的 .active
并将测试添加到 .link-txt
中?
Here is Fiddle显示问题。
最佳答案
function linkClass() {
$('.item .link-txt').removeClass('testing') //Remove every class no check necessary
$('.item.active .link-txt').addClass('testing'); //Add the class to the active one
}
//Document ready so it works also on startup
$( document ).ready(function() {
linkClass();
});
$('#home-carousel').on('slid.bs.carousel', function() {linkClass()});
它已经可以在第一张幻灯片上运行,您的问题只是它不能在创作上滑动。所以我添加了一个文档就绪功能,这样它也可以在启动时工作。
编辑:这个例子是我从 https://www.w3schools.com/bootstrap/bootstrap_ref_js_carousel.asp 复制的。 我添加了我的代码,如果您使用开发人员控制台打开它,您将看到它完全符合您的要求。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.carousel-inner > .item > img,
.carousel-inner > .item > a > img {
width: 70%;
margin: auto;
}
</style>
</head>
<body>
<div class="container">
<h2>Carousel Events - slid.bs.carousel</h2>
<p>The <strong>slid.bs.modal</strong> event occurs when the carousel has finished sliding from one item to another.</p>
<div id="myCarousel" class="carousel slide">
<!-- Indicators -->
<ol class="carousel-indicators">
<li class="item1 active"></li>
<li class="item2"></li>
<li class="item3"></li>
<li class="item4"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="http://via.placeholder.com/350x150" alt="Chania" width="460" height="345">
</div>
<div class="item">
<img src="http://via.placeholder.com/360x150" alt="Chania" width="460" height="345">
</div>
<div class="item">
<img src="http://via.placeholder.com/370x150" alt="Flower" width="460" height="345">
</div>
<div class="item">
<img src="http://via.placeholder.com/380x150" alt="Flower" width="460" height="345">
</div>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" role="button">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
<script>
$(document).ready(function(){
// Activate Carousel
$("#myCarousel").carousel();
// Enable Carousel Indicators
$(".item1").click(function(){
$("#myCarousel").carousel(0);
});
$(".item2").click(function(){
$("#myCarousel").carousel(1);
});
$(".item3").click(function(){
$("#myCarousel").carousel(2);
});
$(".item4").click(function(){
$("#myCarousel").carousel(3);
});
// Enable Carousel Controls
$(".left").click(function(){
$("#myCarousel").carousel("prev");
});
$(".right").click(function(){
$("#myCarousel").carousel("next");
});
$("#myCarousel").on('slid.bs.carousel', function () {
$('.item img').removeClass('testing') //Remove every class no check necessary
$('.item.active img').addClass('testing');
});
});
</script>
</body>
</html>
关于jquery - Bootstrap Carousel 如果项目处于事件状态,则将类添加到 div 然后删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46317217/