如何解决如何在移动视图上更改 Bootstrap Carousel 的图像?
/*Carousel CSS for Mobile View*/
@media (max-width: 425px){
#banner-one img{
background-image: url("/images/mobile_banner1.jpg") !important;
}
#banner-two img{
background-image: url("/images/mobile_banner2.jpg") !important;
}
#banner-three img{
background-image: url("/images/mobile_banner3.jpg") !important;
}
#banner-four img{
background-image: url("/images/mobile_banner4.jpg") !important;
}
}
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div id="banner-one" class="carousel-item active">
<img class="d-block w-100" src="/images/banner-one.jpg" alt="First slide">
</div>
<div id="banner-two" class="carousel-item">
<img class="d-block w-100" src="/images/banner-two.jpg" alt="Second slide">
</div>
<div id="banner-three" class="carousel-item">
<img class="d-block w-100" src="/images/banner-three.jpg" alt="Third slide">
</div>
<div id="banner-four" class="carousel-item">
<img class="d-block w-100" src="/images/banner-four.jpg" alt="Third slide">
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">PrevIoUs</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
我的项目中有一个 Bootstrap Carousel,它在每个视图中都能完美运行。但我想更改移动视图中每张幻灯片的轮播图像。我什么都试过了,但都失败了。
解决方法
您可以设置两个图像,并通过 css 播放它们的可见性。
见下面的片段(使用全屏在视图之间切换):
.desk-img {
display: block;
}
.mobile-img {
display: none;
}
@media (max-width: 700px) {
.desk-img {
display: none !important;
}
.mobile-img {
display: block;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" />
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div id="banner-one" class="carousel-item active">
<img class=" w-100 desk-img" src="https://via.placeholder.com/800x380?text=slide%201" alt="First slide">
<img class=" w-100 mobile-img" src="https://via.placeholder.com/800x380?text=mobile slide%201" alt="First slide">
</div>
<div id="banner-two" class="carousel-item">
<img class=" w-100 desk-img" src="https://via.placeholder.com/800x380?text=slide%202" alt="Second slide">
<img class=" w-100 mobile-img" src="https://via.placeholder.com/800x380?text=mobile slide%202" alt="Second slide">
</div>
<div id="banner-three" class="carousel-item">
<img class=" w-100 desk-img" src="https://via.placeholder.com/800x380?text=slide%203" alt="Third slide">
<img class=" w-100 mobile-img" src="https://via.placeholder.com/800x380?text=mobile slide%203" alt="Third slide">
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
我认为你可以只使用 picture
元素,
<picture>
<source media="(max-width: 425px)" srcset="mobile-img.jpg" />
<source media="(max-width: 768px)" srcset="tablet-img.jpg" />
<img src="desktop-or-default-img.jpg" class="d-block w-100" alt="..." />
</picture>
这种情况下,425px以下使用mobile-img.jpg,768px-425px以下使用tablet-img.jpg,desktop-or-default-img.jpg如果屏幕在(任何)px-768px 处使用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。