Bootstrap Carousel li 项目活动类不工作
Posted
技术标签:
【中文标题】Bootstrap Carousel li 项目活动类不工作【英文标题】:Bootstrap Carousel li item active class not working 【发布时间】:2019-12-02 00:30:11 【问题描述】:我获取了 bootstrap 4 carousel 的代码,然后将其拆分为 2 个 cols,我这样做是因为我需要 carousel 指示器位于其他位置而不是 carousel 内部(默认情况下)。
这里的问题是轮播运行良好(至少在更改图像的部分)它只是“活动”类,不会更改为任何其他 li 项目。
<div class="row mx-0">
<div class="col-lg-3 col-md-3 col-sm-12 px-5">
<h2 class="primary-color text-center text-uppercase" id="nombre-caso">Name Image</h2>
<h3 id="descripcion-caso" class="primary-color text-center">Description Image</h3>
</div>
<div class="col-lg-1 col-md-1 col-sm-12">
<ol class="list-unstyled d-lg-block d-md-block d-flex" id="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active" style="margin-top:0;">
<i class="fas fa-circle"></i>
</li>
<li data-target="#myCarousel" data-slide-to="1">
<i class="fas fa-circle"></i>
</li>
<li data-target="#myCarousel" data-slide-to="2">
<i class="fas fa-circle"></i>
</li>
<li data-target="#myCarousel" data-slide-to="3">
<i class="fas fa-circle"></i>
</li>
<li data-target="#myCarousel" data-slide-to="4">
<i class="fas fa-circle"></i>
</li>
<li data-target="#myCarousel" data-slide-to="5">
<i class="fas fa-circle"></i>
</li>
<li data-target="#myCarousel" data-slide-to="6">
<i class="fas fa-circle"></i>
</li>
<li data-target="#myCarousel" data-slide-to="7">
<i class="fas fa-circle"></i>
</li>
<li data-target="#myCarousel" data-slide-to="8">
<i class="fas fa-circle"></i>
</li>
<li data-target="#myCarousel" data-slide-to="9">
<i class="fas fa-circle"></i>
</li>
</ol>
</div>
<div class="col-lg-8 col-md-8 col-sm-12 px-0">
<div class="slide-wrapper">
<div class="container" id="carousel-container">
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="carousel-item active">
<div class="fill" style="background-image: url(img/casos-exito/cob_galeria_01_harley.jpg)"></div>
</div>
<div class="carousel-item">
<div class="fill" style="background-image: url(img/casos-exito/cob_galeria_02_freightliner.jpg)"></div>
</div>
<div class="carousel-item">
<div class="fill" style="background-image: url(img/casos-exito/cob_galeria_03_loarca.jpg)"></div>
</div>
<div class="carousel-item">
<div class="fill" style="background-image: url(img/casos-exito/cob_galeria_04_automotriz_bajio.jpg)"></div>
</div>
<div class="carousel-item">
<div class="fill" style="background-image: url(img/casos-exito/cob_galeria_05_almacenes.jpg)"></div>
</div>
<div class="carousel-item">
<div class="fill" style="background-image: url(img/casos-exito/cob_galeria_06_bodegas_industriales.jpg)"></div>
</div>
<div class="carousel-item">
<div class="fill" style="background-image: url(img/casos-exito/cob_galeria_07_residencial_queretaro.jpg)"></div>
</div>
<div class="carousel-item">
<div class="fill" style="background-image: url(img/casos-exito/cob_galeria_08_secundaria_ignacio_altamirano.jpg)"></div>
</div>
<div class="carousel-item">
<div class="fill" style="background-image: url(img/casos-exito/cob_galeria_09_oficinas_icsi.jpg)"></div>
</div>
<div class="carousel-item">
<div class="fill" style="background-image: url(img/casos-exito/cob_galeria_10_casa_zibata.jpg)"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
CSS
.carousel-inner,.carousel,.carousel-item,.container#carousel-container,.fill
height:100%;
min-height:700px;
width:100%;
background-position:center center;
background-size:cover;
.slide-wrapperdisplay:inline;
.slide-wrapper .containerpadding:0;
/*------------------------------ vertical bootstrap slider----------------------------*/
.carousel-inner> .carousel-item.carousel-item-next ,
.carousel-inner > .carousel-item.active.carousel-item-right
transform: translate3d(0, 100%, 0); -webkit-transform: translate3d(0, 100%, 0); -ms-transform: translate3d(0, 100%, 0); -moz-transform: translate3d(0, 100%, 0); -o-transform: translate3d(0, 100%, 0); top: 0;
.carousel-inner > .carousel-item.carousel-item-prev ,
.carousel-inner > .carousel-item.active.carousel-item-left
transform: translate3d(0,-100%, 0); -webkit-transform: translate3d(0,-100%, 0); -moz-transform: translate3d(0,-100%, 0);-ms-transform: translate3d(0,-100%, 0); -o-transform: translate3d(0,-100%, 0); top: 0;
.carousel-inner > .carousel-item.next.carousel-item-left ,
.carousel-inner > .carousel-item.carousel-item-prev.carousel-item-right ,
.carousel-inner > .carousel-item.active
transform:translate3d(0,0,0); -webkit-transform:translate3d(0,0,0);-ms-transform:translate3d(0,0,0);; -moz-transform:translate3d(0,0,0); -o-transform:translate3d(0,0,0); top:0;
/*------------------------------- vertical carousel indicators ------------------------------*/
#carousel-indicators li
margin: 35px 0;
color: gray;
#carousel-indicators li.active i
color: var(--primaryColor);
我只想更改活动点的颜色,以在我的页面上显示一些良好的用户体验。
【问题讨论】:
您可能必须使用 javascript 来执行此操作。您需要自己处理切换活动类。您可以使用Bootstrap slide event 添加代码以切换类 【参考方案1】:这可以用最少的 css 轻松实现;您的关键是在轮播 div 中包含两个部分(“指标部分”和“图像部分”)(这是具有 id='myCarousel'
的 div;这样您将获得默认功能,包括自动幻灯片,您可以在其中看到 'active'
类也被应用。
在下面工作sn-p:
/* Make the image fully responsive */
.carousel-inner img
width: 100%;
height: 100%;
.carousel-indicators
display: block !important;
height: 90% !important;
.carousel-indicators li
background-color: red !important;
width: 10px !important;
height: 10px !important;
margin: 40% !important;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<div class='container-fluid'>
<h2>Demo Carousel</h2>
<div id="demo" class="carousel slide" data-ride="carousel">
<div class='row'>
<div class='col-lg-1 col-md-1 col-sm-1 col-1'>
<!-- Indicators -->
<ul class="carousel-indicators">
<li data-target="#demo" data-slide-to="0" class="active"></li>
<li data-target="#demo" data-slide-to="1"></li>
<li data-target="#demo" data-slide-to="2"></li>
<li data-target="#demo" data-slide-to="3"></li>
</ul>
</div>
<div class='col-lg-11 col-md-11 col-sm-11 col-11'>
<!-- The slideshow -->
<div class="carousel-inner">
<div class="carousel-item active">
<img src="https://www.w3schools.com/bootstrap4/la.jpg" >
</div>
<div class="carousel-item">
<img src="https://www.w3schools.com/bootstrap4/chicago.jpg" >
</div>
<div class="carousel-item">
<img src="https://www.w3schools.com/bootstrap4/ny.jpg" >
</div>
<div class="carousel-item">
<img src="https://www.akberiqbal.com/Jumbo.jpg" >
</div>
</div>
</div>
</div>
</div>
</div>
注意:我必须将 !important 放在堆栈溢出代码 sn-p 上的每个属性上,因为它会在 bootstrap.css
之前加载自定义 CSS - 在您的实际代码中,放置 @987654326加载bootstrap.css
后@ 块,你不必写!important
;
【讨论】:
非常感谢!它工作得很好,现在我只需要更改移动设备上指示器的显示(那些具有绝对位置,所以它们被显示在图像上哈哈哈),无论如何,谢谢你的帮助:)以上是关于Bootstrap Carousel li 项目活动类不工作的主要内容,如果未能解决你的问题,请参考以下文章
Django Bootstrap Carousel 一次 4 个项目
如何跳转到 Bootstrap 的 Carousel 中的特定项目? [复制]
Bootstrap Carousel 过渡项目 + animate.css