如何解决Bootstrap v5.0 Carousel 选择一个轮播项目后停止自动播放点击
在单击轮播项目后,我尝试使用 jQuery 停止轮播自动播放。但我的尝试都没有奏效。有人可以帮我吗?
谢谢!
这是我的尝试 1:
$(".carousel-item .card-selection").on("click",function (){
$("#carouselExampleControls").attr("data-bs-interval","false");
})
我也尝试过(在删除 HTML 中的 data-bs-ride="carousel" 属性后):
$(".carousel-item .card-selection").on("click",function (){
$("#carouselExampleControls").carousel({
pause: true,interval: false
});
})
这是旋转木马:
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.1/dist/umd/popper.min.js"></script>
<div id="carouselExampleControls" class="carousel slide" data-bs-ride="carousel" data-bs-interval="2000">
<div class="carousel-inner">
<div class="carousel-item active">
<div class="row selection-wrapper">
<div class="mb-2 col-6">
<div class="card card-selection h-100">
<label for="selected-item-4" class="mx-2 selected-label">
<input type="radio" name="G1" id="selected-item-4">
<span class="icon"></span>
<div class="card-body">
<h5 class="card-title">1</h5>
<p class="card-text">This is a wider card with supporting text below as a natural
lead-in to additional content. This content is a little bit longer.</p>
</div>
</label>
</div>
</div>
</div>
</div>
<div class="carousel-item">
<div class="row selection-wrapper">
<div class="mb-2 col-6">
<div class="card card-selection h-100">
<label for="selected-item-5" class=" border-light mx-1 selected-label">
<input type="radio" name="G2" id="selected-item-5">
<span class="icon"></span>
<div class="card-body">
<h5 class="card-title">2</h5>
<p class="card-text">This card has supporting text below as a natural lead-in to
additional content.</p>
</div>
</label>
</div>
</div>
</div>
</div>
</div>
</div>
解决方法
我已经让它像这样工作了,可能有更好的“引导”方式来做到这一点。
var myCarousel = document.querySelector('#carouselExampleControls')
var carousel = new bootstrap.Carousel(myCarousel,{
ride: 'carousel',interval: 100,});
let paused = false;
$('.card-body').on('click',() => {
if (paused) {
carousel.cycle()
paused = false;
} else {
carousel.pause();
paused = true;
}
});
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.1/dist/umd/popper.min.js"></script>
<div id="carouselExampleControls" class="carousel slide">
<div class="carousel-inner">
<div class="carousel-item active">
<div class="row selection-wrapper">
<div class="mb-2 col-6">
<div class="card card-selection h-100">
<label for="selected-item-4" class="mx-2 selected-label">
<input type="radio" name="G1" id="selected-item-4">
<span class="icon"></span>
<div class="card-body">
<h5 class="card-title">1</h5>
<p class="card-text">This is a wider card with supporting text below as a natural
lead-in to additional content. This content is a little bit longer.</p>
</div>
</label>
</div>
</div>
</div>
</div>
<div class="carousel-item">
<div class="row selection-wrapper">
<div class="mb-2 col-6">
<div class="card card-selection h-100">
<label for="selected-item-5" class=" border-light mx-1 selected-label">
<input type="radio" name="G2" id="selected-item-5">
<span class="icon"></span>
<div class="card-body">
<h5 class="card-title">2</h5>
<p class="card-text">This card has supporting text below as a natural lead-in to
additional content.</p>
</div>
</label>
</div>
</div>
</div>
</div>
</div>
</div>
如果我理解您关于“选择卡片后”的评论——您正在寻找能够点击卡片上的单选按钮的用户,这样做会阻止轮播(至少直到用户再次单击单选按钮取消选择卡片)。
您只需使用 JavaScript 即可完成此操作。 Bootstrap 5 不需要 jQuery,因为 Bootstrap 5 只支持现代浏览器(没有 IE),所以你不需要 jQuery。
您可以使用 Bootstrap 的 carousel 实例来停止和重新启动 carousel。
var myCarousel = document.getElementById("carouselExampleControls");
var radios = document.querySelectorAll("input[type=radio]");
var cycling = true;
radios.forEach(function(radio) {
radio.checked = false;
radio.addEventListener("click",function(e) {
var carousel = bootstrap.Carousel.getInstance(myCarousel);
if (cycling === true) {
carousel.pause();
cycling = false;
} else {
carousel.cycle();
cycling = true;
e.target.checked = false;
}
});
});
.carousel-item {
height: 12.5rem;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/js/bootstrap.bundle.min.js"></script>
<div class="container">
<div class="row selection-wrapper">
<div class="col-6 mb-2 ">
<div id="carouselExampleControls" class="carousel slide" data-bs-ride="carousel" data-bs-interval="1000">
<div class="carousel-inner">
<div class="carousel-item active">
<div class="card card-selection h-100">
<label for="selected-item-4" class="mx-2 selected-label">
<input type="radio" name="G1" id="selected-item-4">
<span class="icon"></span>
</label>
<div class="card-body">
<h5 class="card-title">1</h5>
<p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
</div>
</div>
</div>
<div class="carousel-item">
<div class="card card-selection h-100">
<label for="selected-item-5" class=" border-light mx-1 selected-label">
<input type="radio" name="G2" id="selected-item-5">
<span class="icon"></span>
</label>
<div class="card-body">
<h5 class="card-title">2</h5>
<p class="card-text">This card has supporting text below as a natural lead-in to additional content.</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
我确实将您的 div 移出了标签(div 不应该是标签的子级),但我不确定为什么您在旋转木马项目中有一行和第 6 列,所以我更改了那些。我还为卡片添加了固定高度。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。