gpt4 book ai didi

javascript - Bootstrap Carousel - 在不同分辨率下显示的元素数

转载 作者:行者123 更新时间:2023-11-29 10:38:58 26 4
gpt4 key购买 nike

我想使用 Bootstrap 轮播在不同的屏幕分辨率下显示不同数量的元素。

例如:

  1. 我想在屏幕分辨率大于 1000 像素时显示 3 个联系人的详细信息

  2. 我想在屏幕分辨率介于 600 像素和 999 像素之间时显示 2 个联系人的详细信息

  3. 最后我想在屏幕分辨率小于 600 像素时显示单个联系人的详细信息

提前感谢您的帮助。我已经坚持了几天了..

最佳答案

易于实现适用于 Bootstrap 4 的解决方案。将从“data-”获取元素计数并从 JS 获取断点。

(function ($) {
function calcStepSize(optionNode) {
var breakM = 990;
var breakS = 768;

var width = $(window).innerWidth();

if(width < breakS) {
var key = 's';
} else if(width < breakM) {
key = 'm';
} else {
key = 'l';
}

var cnt = 1*optionNode.data("itemcount-"+key);

return cnt > 0? cnt : 1;
}

function repartition(container, items, count) {
container.children().remove();

for(var i = 0; i < items.length; i++) {
var cBlock = $('<div class="carousel-item" ></div').appendTo(container);
var cInnerBlock = $('<div class="row"></div>').appendTo(cBlock);

for(var j = 0; j < count; j++) {
var cIdx = (i + j) % items.length;

cInnerBlock.append($(items.get(cIdx)).clone());
}
}

container.children().first().addClass("active");
}

$('.carousel.multi').each(function(idx, El) {
var carousel = $(El);
var container = carousel.find('.carousel-inner');

if(!container.children().first().hasClass('carousel-item')) {
var items = container.children().clone();

var lastSize = calcStepSize(carousel);
repartition(container, items, lastSize);

$(window).resize(function () {
var cSize = calcStepSize(carousel);

if(cSize != lastSize) {
repartition(container, items, cSize);
lastSize = cSize;
}
});
} else {
container.children().first().addClass("active");
}

carousel.carousel({
interval: false
});
});

}(jQuery));
<link href="https://v4-alpha.getbootstrap.com/assets/css/docs.min.css" rel="stylesheet"/>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js"></script>
<script src="https://cdn.rawgit.com/imsky/holder/master/holder.js"></script>

<div class="container">

<div class="bd-example">

<div id="carousel-example-generic" class="carousel multi slide" data-ride="carousel" data-itemcount-l="4" data-itemcount-m="3" data-itemcount-s="2" autostart="0">
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<div class="carousel-inner" role="listbox">

<div class="col-xs-6 col-sm-6 col-md-4 col-lg-3">
<img src="holder.js/250x250/auto/#ccc:#755" class="img-fluid" alt="First slide">
</div>
<div class="col-xs-6 col-sm-6 col-md-4 col-lg-3">
<img src="holder.js/250x250/auto/#7dd:#7dd" class="img-fluid" alt="Second slide">
</div>
<div class="col-xs-6 col-sm-6 col-md-4 col-lg-3">
<img src="holder.js/250x250/auto/#747:#444" class="img-fluid" alt="Third slide">
</div>
<div class="col-xs-6 col-sm-6 col-md-4 col-lg-3">
<img src="holder.js/250x250/auto/#ccc:#755" class="img-fluid" alt="First slide">
</div>
<div class="col-xs-6 col-sm-6 col-md-4 col-lg-3">
<img src="holder.js/250x250/auto/#7dd:#7dd" class="img-fluid" alt="Second slide">
</div>
<div class="col-xs-6 col-sm-6 col-md-4 col-lg-3">
<img src="holder.js/250x250/auto/#747:#444" class="img-fluid" alt="Third slide">
</div>

</div>
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="icon-prev" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="icon-next" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>

关于javascript - Bootstrap Carousel - 在不同分辨率下显示的元素数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32372294/

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