1 回答
TA贡献1752条经验 获得超4个赞
单击图像时,您可以获得包含该图像的 div 索引,即:0,1..etc然后使用此索引我们可以使carousel slide处于同一位置的活动。
演示代码:
//when img is clicked
$('.parentSlider-cell img').click(function() {
//get index for div
var idx = $(this).parents('div').index();
console.log(idx)
var id = parseInt(idx);
$('.bd-example-modal-lg').modal('show'); // show the modal
$(".carousel-inner").carousel(id); // slide carousel to selected
});
* {
box-sizing: border-box;
}
body {
font-family: sans-serif;
}
.parentSlider {
display: grid;
grid-gap: 20px;
grid-template-columns: repeat(4, 1fr);
}
.parentSlider-cell img {
width: 100%;
}
.modal-dialog {
max-width: 80%;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<div class="parentSlider">
<div class="parentSlider-cell">
<a href="javascript:void(0);"><img src="http://placehold.it/1200x600/555/000&text=One" /></a>
</div>
<div class="parentSlider-cell">
<a href="javascript:void(0);"><img src="http://placehold.it/1200x600/555/000&text=two" /></a>
</div>
<div class="parentSlider-cell">
<a href="javascript:void(0);"><img src="http://placehold.it/1200x600/fcf00c/000&text=Three" /></a>
</div>
<div class="parentSlider-cell">
<a href="javascript:void(0);"><img src="http://placehold.it/1200x600/fcf00c/000&text=Four" /></a>
</div>
</div>
<button type="button" class="btn btn-primary mt-5" data-toggle="modal" data-target=".bd-example-modal-lg">Large modal</button>
<div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div id="demo" class="carousel slide" data-ride="carousel">
<!-- The slideshow -->
<div class="carousel-inner">
<div class="carousel-item active">
<img src="http://placehold.it/1200x600/555/000&text=One" alt="Los Angeles">
</div>
<div class="carousel-item">
<img src="http://placehold.it/1200x600/fffccc/000&text=Two" alt="Chicago">
</div>
<div class="carousel-item">
<img src="http://placehold.it/1200x600/fcf00c/000&text=Three" alt="New York">
</div>
<div class="carousel-item">
<img src="http://placehold.it/1200x600/fcf00c/000&text=Four" alt="test">
</div>
</div>
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
- 1 回答
- 0 关注
- 86 浏览
添加回答
举报