【发布时间】:2020-10-04 11:17:49
【问题描述】:
我有一个多项目轮播,在较宽的屏幕上显示 6 个项目,但在较小的屏幕上继续显示 6 个项目。它只是将它们堆叠在一起并与其他内容重叠。我想在小屏幕上显示 4 个项目,然后在超小屏幕上显示 2 个项目。我已经改编了这个 codepen 中的代码:https://codepen.io/MhSami/pen/zNBMbj,我发现这个响应更快的 codepen (https://codepen.io/Qvatra/pen/yOvBoM) 有 4 个项目,但是,当我尝试将我的代码更改为它时,它根本不显示。
My current problem with the stacking of the items
When I try to change my code to the responsive carousel
这是轮播的 html
<div class="container" style="background-color:white; height:140px; padding-top:15px">
<div class="row">
<div class="col-xs-11 col-md-12 col-centered">
<div class="carousel slide" data-ride="carousel" data-type="multi" data-interval="3500" id="myCarousel">
<div class="carousel-inner">
<div class="item active">
<div class="col-md-2 col-sm-3 col-xs-6">
<div>
<a href="{% url 'category-2' category2='arsenal' %}">
<img src="{% static 'logo/arsenal_city_logo.png' %}" class="img-responsive" style="max-width:100px; max-height:110px">
</a>
</div>
</div>
</div>
<div class="item">
<div class="col-md-2 col-sm-3 col-xs-6">
<a href="{% url 'category-2' category2='man_united' %}">
<img src="{% static 'logo/manchester_united_fc_logo.png' %}" class="img-responsive" style="max-width:100px; max-height:110px">
</a>
</div>
</div>
<div class="item">
<div class="col-md-2 col-sm-3 col-xs-6">
<a href="{% url 'category-2' category2='fcb' %}">
<img src="{% static 'logo/barcelona_logo.png' %}" class="img-responsive" style="max-width:100px; max-height:110px">
</a>
</div>
</div>
<div class="item">
<div class="col-md-2 col-sm-3 col-xs-6">
<a href="{% url 'category-2' category2='chelsea' %}">
<img src="{% static 'logo/chelsea_logo.png' %}" class="img-responsive" style="max-width:100px; max-height:110px">
</a>
</div>
</div>
<div class="item">
<div class="col-md-2 col-sm-3 col-xs-6">
<a href="{% url 'category-2' category2='everton' %}">
<img src="{% static 'logo/everton_fc_logo.png' %}" class="img-responsive" style="max-width:100px; max-height:110px">
</a>
</div>
</div>
<div class="item">
<div class="col-md-2 col-sm-3 col-xs-6">
<a href="{% url 'category-2' category2='liverpool' %}">
<img src="{% static 'logo/liverpool_fc_logo.png' %}" class="img-responsive" style="max-width:100px; max-height:110px">
</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>
</div>
</div>
CSS:
.carousel-inner { margin: auto; width: 90%; }
.carousel-control { width: 4%; }
.carousel-control.left,
.carousel-control.right {
background-image:none;
}
.glyphicon-chevron-left, .carousel-control .glyphicon-chevron-right {
margin-top:-10px;
margin-left: -10px;
color: #444;
}
.carousel-inner {
a {
display:table-cell;
height: 180px;
width: 200px;
vertical-align: middle;
}
img {
max-height: 150px;
margin: auto auto;
max-width: 100%;
}
}
@media (min-width: 992px ) {
.carousel-inner > .item.next,
.carousel-inner > .item.active.right {
left: 0;
-webkit-transform: translate3d(16.7%, 0, 0);
transform: translate3d(16.7%, 0, 0);
}
.carousel-inner > .item.prev,
.carousel-inner > .item.active.left {
left: 0;
-webkit-transform: translate3d(-16.7%, 0, 0);
transform: translate3d(-16.7%, 0, 0);
}
}
@media (max-width: 992px ) {
.carousel-inner > .item.next,
.carousel-inner > .item.active.right {
left: 0;
-webkit-transform: translate3d(25%, 0, 0);
transform: translate3d(25%, 0, 0);
}
.carousel-inner > .item.prev,
.carousel-inner > .item.active.left {
left: 0;
-webkit-transform: translate3d(-25%, 0, 0);
transform: translate3d(-25%, 0, 0);
}
}
JS:
$('.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<2;i++) {
next=next.next();
if (!next.length) {
next = $(this).siblings(':first');
}
next.children(':first-child').clone().appendTo($(this));
}
});
我尝试过的:
- 将代码更改为响应式 Codepen,但没有成功
- 更改各种屏幕尺寸的变换值。您可以看到最终的媒体 css 块应该何时显示 4 个项目。
【问题讨论】:
-
您能否将其作为 sn-p 提供并简化一下?那里有很多代码,像 href 和图像 src 之类的东西在你的环境之外是行不通的。
-
我设法解决了这个问题。事实证明,我以错误的顺序为响应式 codepen 导入脚本。为了将来参考,我应该发布什么而不是整个代码块?编辑:我发布了解决方案,但我不能接受它作为答案。我做错了什么?
-
您可以发布片段。您可以在创建问题时出现的 WYSIWYG 编辑器中找到 Snippet 按钮(它是相反的 V 形按钮)。还尝试简化代码以删除不会导致问题的内容。 (例如不工作的hrefs)显然确保片段仍然复制问题!
标签: javascript html css twitter-bootstrap