Bootstrap 4 多项目轮播(单击循环所有新项目)[重复]
Posted
技术标签:
【中文标题】Bootstrap 4 多项目轮播(单击循环所有新项目)[重复]【英文标题】:Bootstrap 4 Multiple Item Carousel (Click cycles all new items) [duplicate] 【发布时间】:2018-09-19 05:27:13 【问题描述】:尝试使用引导 4 轮播功能来执行此操作(下图是 bootstrap 3 variant)
我尝试将其转换为引导程序 4。我对它所做的是通过 html 上的 bootstrap 3 → to bootstrap 4 html conversion tool 运行它,并调整对 CDN 中引导程序 JS 和 CSS 文件的引用。我还用颜色更改了图像引用,以便更容易区分我想要显示的图像。
下面是代码,它没有在一张幻灯片上显示所有 4 张图片
$(document).ready(function()
$('#Carousel').carousel(
interval: 5000
)
);
bodypadding-top:20px;
.carousel
margin-bottom: 0;
padding: 0 40px 30px 40px;
/* The controlsy */
.carousel-control
left: -12px;
height: 40px;
width: 40px;
background: none repeat scroll 0 0 #222222;
border: 4px solid #FFFFFF;
border-radius: 23px 23px 23px 23px;
margin-top: 90px;
.carousel-control.right
right: -12px;
/* The indicators */
.carousel-indicators
right: 50%;
top: auto;
bottom: -10px;
margin-right: -19px;
/* The colour of the indicators */
.carousel-indicators li
background: #cecece;
.carousel-indicators .active
background: #428bca;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<!------ Include the above in your HEAD tag ---------->
<div class="container">
<div class="row">
<div class="col-lg-12">
<div id="Carousel" class="carousel slide">
<ol class="carousel-indicators">
<li data-target="#Carousel" data-slide-to="0" class="active"></li>
<li data-target="#Carousel" data-slide-to="1"></li>
<li data-target="#Carousel" data-slide-to="2"></li>
</ol>
<!-- Carousel items -->
<div class="carousel-inner">
<div class="item active carousel-item">
<div class="row">
<div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250/0ff" style="max-width:100%;"></a>
</div>
<div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250/0ff" style="max-width:100%;"></a>
</div>
<div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250/0ff" style="max-width:100%;"></a>
</div>
<div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250/0ff" style="max-width:100%;"></a>
</div>
</div>
<!--.row-->
</div>
<!--.item-->
<div class="item carousel-item">
<div class="row">
<div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250/00f" style="max-width:100%;"></a>
</div>
<div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250/00f" style="max-width:100%;"></a>
</div>
<div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250/00f" style="max-width:100%;"></a>
</div>
<div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250/00f" style="max-width:100%;"></a>
</div>
</div>
<!--.row-->
</div>
<!--.item-->
<div class="item carousel-item">
<div class="row">
<div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250" style="max-width:100%;"></a>
</div>
<div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250" style="max-width:100%;"></a>
</div>
<div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250" style="max-width:100%;"></a>
</div>
<div class="col-lg-3"><a href="#" class="thumbnail"><img src="http://placehold.it/250x250" style="max-width:100%;"></a>
</div>
</div>
<!--.row-->
</div>
<!--.item-->
</div>
<!--.carousel-inner--><a data-slide="prev" href="#Carousel" class="left carousel-control">‹</a>
<a data-slide="next" href="#Carousel" class="right carousel-control">›</a>
</div>
<!--.Carousel-->
</div>
</div>
</div>
<!--.container-->
【问题讨论】:
"寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误以及在问题本身中重现它所需的最短代码。没有明确的问题陈述对其他读者没有用处。” @Zimsystem 你真的看过类似的***帖子吗?它们在功能上不一样。该问题对源代码有参考。我已经在互联网上搜索了几个小时的解决方案,这是我唯一知道的。如果您不将其保留为副本,我将不胜感激,因为这样做会损害 ***。 编辑问题以显示您到目前为止所尝试的内容。代码应该在问题中。然后解释它为什么不与同一主题的许多其他问题重复。 【参考方案1】:使用 4 项检查此引导 v4 轮播:
.col-md-3
display: inline-block;
margin-left:-4px;
.col-md-3 img
width:100%;
height:auto;
body .carousel-indicators li
background-color:red;
body .carousel-indicators
bottom: 0;
body .carousel-control-prev-icon,
body .carousel-control-next-icon
background-color:red;
body .no-padding
padding-left: 0;
padding-right: 0;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<div id="demo" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ul class="carousel-indicators">
<li data-target="#demo" data-slide-to="0" class="active"></li>
<li data-target="#demo" data-slide-to="1"></li>
<li data-target="#demo" data-slide-to="2"></li>
</ul>
<!-- The slideshow -->
<div class="container carousel-inner no-padding">
<div class="carousel-item active">
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
</div>
</div>
<div class="carousel-item">
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
</div>
</div>
<div class="carousel-item">
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<img src="https://image.shutterstock.com/z/stock-photo-sleeping-disorders-as-a-reason-for-insomnia-293777093.jpg">
</div>
</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>
您也可以查看此笔https://codepen.io/kravisingh/pen/pLGzgo
【讨论】:
你能把按钮也放在左边/右边吗:)。 箭头也添加到左侧/右侧。 滑动时图片也会弹出一点,有没有办法不让? 只需通过添加.class
删除.container
的左右填充或查看更新的答案。
这可能“有效”达到预期目的,但它没有正确使用 Bootstrap 4。【参考方案2】:
这是你想要的吗?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
.carousel-inner .active.left
left: -33%;
.carousel-inner .next
left: 33%;
.carousel-inner .prev
left: -33%;
.carousel-control.left,
.carousel-control.right
background-image: none;
.carousel-multi .carousel-inner>.item
transition: 500ms ease-in-out left;
.carousel-multi .carousel-inner>.item>.media-card
background: #333;
border-right: 10px solid #fff;
display: table-cell;
width: 1%;
.carousel-multi .carousel-inner>.item>.media-card:last-of-type
border-right: none;
.carousel-multi .carousel-inner .active
display: table;
.carousel-multi .carousel-inner .active.left
left: -33%;
.carousel-multi .carousel-inner .active.right
left: 33%;
.carousel-multi .carousel-inner .next
left: 33%;
.carousel-multi .carousel-inner .prev
left: -33%;
@media all and (transform-3d),
(-webkit-transform-3d)
.carousel-multi .carousel-inner>.item
transition: 500ms ease-in-out all;
backface-visibility: visible;
transform: none!important;
</style>
</head>
<body>
<div id="carousel-example-multi" class="carousel carousel-multi slide">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-multi" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-multi" data-slide-to="1"></li>
<li data-target="#carousel-example-multi" data-slide-to="2"></li>
<li data-target="#carousel-example-multi" data-slide-to="3"></li>
<li data-target="#carousel-example-multi" data-slide-to="4"></li>
<li data-target="#carousel-example-multi" data-slide-to="5"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<div class="media media-card">
<time class="small">10 Aug 2016</time>
<div class="media-category">Commodities Quarterly</div>
<h4 class="media-heading">
<a href="#">Research article 1</a>
</h4>
<a class="aside" href="#">Read more</a>
</div>
</div>
<div class="item ">
<div class="media media-card">
<time class="small">10 Aug 2016</time>
<div class="media-category">Commodities Quarterly</div>
<h4 class="media-heading">
<a href="#">Research article number two</a>
</h4>
<a class="aside" href="#">Read more</a>
</div>
</div>
<div class="item ">
<div class="media media-card">
<time class="small">10 Aug 2016</time>
<div class="media-category">Commodities Quarterly</div>
<h4 class="media-heading">
<a href="#">Number 3</a>
</h4>
<a class="aside" href="#">Read more</a>
</div>
</div>
<div class="item ">
<div class="media media-card">
<time class="small">10 Aug 2016</time>
<div class="media-category">Commodities Quarterly</div>
<h4 class="media-heading">
<a href="#">This is the fourth article</a>
</h4>
<a class="aside" href="#">Read more</a>
</div>
</div>
<div class="item ">
<div class="media media-card">
<time class="small">10 Aug 2016</time>
<div class="media-category">Commodities Quarterly</div>
<h4 class="media-heading">
<a href="#">...and this is the fifth</a>
</h4>
<a class="aside" href="#">Read more</a>
</div>
</div>
<div class="item ">
<div class="media media-card">
<time class="small">10 Aug 2016</time>
<div class="media-category">Commodities Quarterly</div>
<h4 class="media-heading">
<a href="#">Sixth</a>
</h4>
<a class="aside" href="#">Read more</a>
</div>
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-multi" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-multi" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<script>
//Allows bootstrap carousels to display 3 items per page rather than just one
$('.carousel.carousel-multi .item').each(function ()
var next = $(this).next();
if (!next.length)
next = $(this).siblings(':first');
next.children(':first-child').clone().attr("aria-hidden", "true").appendTo($(this));
if (next.next().length > 0)
next.next().children(':first-child').clone().attr("aria-hidden", "true").appendTo($(this));
else
$(this).siblings(':first').children(':first-child').clone().appendTo($(this));
);
</script>
</body>
</html>
https://codepen.io/MarkitDigital/pen/ZpEByz
【讨论】:
我在我的帖子上添加了一个编辑以进行澄清。它应该在每次点击时循环一组新图像,而不是一次循环希望找到一个不使用大量 javascript 的变体 片段无法正常工作 使用mdbootstrap.com/snippets/jquery/ascensus/135508以上是关于Bootstrap 4 多项目轮播(单击循环所有新项目)[重复]的主要内容,如果未能解决你的问题,请参考以下文章
页面加载后,如何使 Bootstrap js 轮播自动循环?
Bootstrap 多个项目轮播(一次显示几个轮播项目)[重复]