gpt4 book ai didi

javascript - 在没有 for 循环的情况下调整大小时(使用 jquery)在单个元素中 Bootstrap 轮播多个图像

转载 作者:行者123 更新时间:2023-11-28 05:08:08 25 4
gpt4 key购买 nike

我有一个 Bootstrap 轮播,当我根据轮播内部宽度调整页面大小时,我需要在单个元素中制作多个图像(没有 for 循环)。旋转木马内部宽度是在我调整页面大小时动态生成的。

actual

实际代码:

<div class="carousel-inner" role="listbox">


<div class="item active">
<img src="img_flower1.jpg" alt="Chania">page 1
</div>

<div class="item">
<img src="img_flower2.jpg" alt="Flower">page 2
</div>

<div class="item">
<img src="img_flower3.jpg" alt="Flower">page 3
</div>

<div class="item">
<img src="img_flower4.jpg" alt="Flower">page 4
</div>
</div>

预期代码:

enter image description here

    <div class="item active">
<img src="img_flower1.jpg" alt="Chania">page 1
<img src="img_flower2.jpg" alt="Flower">page 2
</div>

<div class="item">
<img src="img_flower3.jpg" alt="Flower">page 3
<img src="img_flower4.jpg" alt="Flower">page 4
</div>

</div>

最佳答案

首先,您需要像这样为轮播元素添加响应能力。

<div class="item">
<div class="col-md-2 col-sm-6 col-xs-12">
<a href="#">
<img src="http://placehold.it/500/fcfcfc/333&amp;text=demo" class="img-responsive">
</a>
</div>
</div>

以便根据新的布局变化、中小型屏幕进行调整。

另一种选择是将另一个库用于轮播。我建议看看 slick .它拥有您需要的所有东西。

对于您的场景设置,就这么简单。

$('.responsive').slick({
dots: true,
infinite: false,
speed: 300,
slidesToShow: 4,
slidesToScroll: 4,
responsive: [
{
breakpoint: 1024,
settings: {
slidesToShow: 3,
slidesToScroll: 3,
infinite: true,
dots: true
}
},
{
breakpoint: 600,
settings: {
slidesToShow: 2,
slidesToScroll: 2
}
},
{
breakpoint: 480,
settings: {
slidesToShow: 1,
slidesToScroll: 1
}
}
// You can unslick at a given breakpoint now by adding:
// settings: "unslick"
// instead of a settings object
]
});

如果你想使用bootstrap轮播。这是一些要测试的示例代码。尝试在全尺寸窗口中打开,然后调整屏幕大小。

$('.carousel[data-type="multi"] .item').each(function(){
var next = $(this).next();
if (!next.length) {
next = $(this).siblings(':first');
}
next.children(':first-child').clone().appendTo($(this));

for (var i=0;i<4;i++) {
next=next.next();
if (!next.length) {
next = $(this).siblings(':first');
}

next.children(':first-child').clone().appendTo($(this));
}
});
.carousel-control 			 { width:  4%; }
.carousel-control.left,.carousel-control.right {margin-left:15px;background-image:none;}
@media (max-width: 767px) {
.carousel-inner .active.left { left: -100%; }
.carousel-inner .next { left: 100%; }
.carousel-inner .prev { left: -100%; }
.active > div { display:none; }
.active > div:first-child { display:block; }

}
@media (min-width: 767px) and (max-width: 992px ) {
.carousel-inner .active.left { left: -50%; }
.carousel-inner .next { left: 50%; }
.carousel-inner .prev { left: -50%; }
.active > div { display:none; }
.active > div:first-child { display:block; }
.active > div:first-child + div { display:block; }
}
@media (min-width: 992px ) {
.carousel-inner .active.left { left: -16.7%; }
.carousel-inner .next { left: 16.7%; }
.carousel-inner .prev { left: -16.7%; }
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-12 text-center"><h3>Testing Carousel</h3></div>
<div class="col-md-10 col-md-offset-1">
<div class="carousel slide" data-ride="carousel" data-type="multi" data-interval="3000" id="myCarousel">
<div class="carousel-inner">
<div class="item active">
<div class="col-md-2 col-sm-6 col-xs-12"><a href="#"><img src="http://placehold.it/500/e499e4/fff&amp;text=1" class="img-responsive"></a></div>
</div>
<div class="item">
<div class="col-md-2 col-sm-6 col-xs-12"><a href="#"><img src="http://placehold.it/500/e477e4/fff&amp;text=2" class="img-responsive"></a></div>
</div>
<div class="item">
<div class="col-md-2 col-sm-6 col-xs-12"><a href="#"><img src="http://placehold.it/500/eeeeee&amp;text=3" class="img-responsive"></a></div>
</div>
<div class="item">
<div class="col-md-2 col-sm-6 col-xs-12"><a href="#"><img src="http://placehold.it/500/f4f4f4&amp;text=4" class="img-responsive"></a></div>
</div>
<div class="item">
<div class="col-md-2 col-sm-6 col-xs-12"><a href="#"><img src="http://placehold.it/500/f566f5/333&amp;text=5" class="img-responsive"></a></div>
</div>
<div class="item">
<div class="col-md-2 col-sm-6 col-xs-12"><a href="#"><img src="http://placehold.it/500/f477f4/fff&amp;text=6" class="img-responsive"></a></div>
</div>
<div class="item">
<div class="col-md-2 col-sm-6 col-xs-12"><a href="#"><img src="http://placehold.it/500/eeeeee&amp;text=7" class="img-responsive"></a></div>
</div>
<div class="item">
<div class="col-md-2 col-sm-6 col-xs-12"><a href="#"><img src="http://placehold.it/500/fcfcfc/333&amp;text=8" class="img-responsive"></a></div>
</div>
</div>
<a class="left carousel-control" href="#myCarousel" data-slide="prev"><i class="glyphicon glyphicon-chevron-left"></i></a>
<a class="right carousel-control" href="#myCarousel" data-slide="next"><i class="glyphicon glyphicon-chevron-right"></i></a>
</div>
</div>

关于javascript - 在没有 for 循环的情况下调整大小时(使用 jquery)在单个元素中 Bootstrap 轮播多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39726604/

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