问题描述
希望你有美好的一天!每次运行时我都会遇到问题,它只出现第一张幻灯片几秒钟,然后它完全消失,当我在幻灯片消失之前使用侧箭头或按钮时,它可以正常工作。
HTML
<header>
<div class="row row-content">
<div class="col">
<div id="mycarousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner" role="listBox">
<div class="carousel-item active">
<img src="img/img1.png" class="d-block img-fluid">
<div class="carousel-caption d-none d-md-block" alt="img1">
<h2>text</h2>
<p class="d-none d-sm-block">text</p>
</div>
</div>
<div class="carousel-item">
<img src="img/img2.png" class="d-block img-fluid">
<div class="carousel-caption d-none d-md-block" alt="img2">
<h2>text</h2>
<p class="d-none d-sm-block">text</p>
</div>
</div>
<div class="carousel-item">
<img src="img/img3.png" class="d-block img-fluid">
<div class="carousel-caption d-none d-md-block" alt="img3">
<h2>text</h2>
<p class="d-none d-sm-block">text</p>
</div>
</div>
</div>
<ol class="carousel-indicators">
<li data-target="#mycarousel"data-slide-to="0" class="active"></li>
<li data-target="#mycarousel"data-slide-to="1" class="active"></li>
<li data-target="#mycarousel"data-slide-to="2" class="active"></li>
</ol>
<a class="carousel-control-prev" href="#mycarousel" role="button" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#mycarousel" role="button" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
</div>
</div>
</header>
CSS
.carousel-item {
height: 420px;
}
.carousel-item img {
position: absolute;
top: 0;
left: 0;
min-height: 420px;
}
解决方法
我已经制作了这个示例并且运行良好。你有没有插入js库?因为你的代码没问题。
.carousel-item {
height: 420px;
}
.carousel-item img {
position: absolute;
top: 0;
left: 0;
min-height: 420px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<header>
<div class="row row-content">
<div class="col">
<div id="mycarousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner" role="listbox">
<div class="carousel-item active">
<img src="https://www.lupadigital.info/wp-content/uploads/2018/05/imagens-gratis.jpg" class="d-block img-fluid">
<div class="carousel-caption d-none d-md-block" alt="img1">
<h2>text</h2>
<p class="d-none d-sm-block">text</p>
</div>
</div>
<div class="carousel-item">
<img src="https://www.lupadigital.info/wp-content/uploads/2018/05/imagens-gratis.jpg" class="d-block img-fluid">
<div class="carousel-caption d-none d-md-block" alt="img2">
<h2>text</h2>
<p class="d-none d-sm-block">text</p>
</div>
</div>
<div class="carousel-item">
<img src="https://www.lupadigital.info/wp-content/uploads/2018/05/imagens-gratis.jpg" class="d-block img-fluid">
<div class="carousel-caption d-none d-md-block" alt="img3">
<h2>text</h2>
<p class="d-none d-sm-block">text</p>
</div>
</div>
</div>
<ol class="carousel-indicators">
<li data-target="#mycarousel"data-slide-to="0" class="active"></li>
<li data-target="#mycarousel"data-slide-to="1" class=""></li>
<li data-target="#mycarousel"data-slide-to="2" class=""></li>
</ol>
<a class="carousel-control-prev" href="#mycarousel" role="button" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#mycarousel" role="button" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
</div>
</div>
</header>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>