[英]Have images center the width and height using owl carousel and Bootstrap 3
[英]Bootstrap carousel width and height
我正在嘗試為圖像(寬度:100%)和固定高度做一個全寬的引導輪播,但如果我將寬度設置為 100%,那么高度也會自動采用 100%
我不確定問題是否出在我的文件中
<div id="myCarousel" class="carousel slide">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- Carousel items -->
<div class="carousel-inner">
<div class="active item">
<%= image_tag "slider/slide-bg-1.jpg", class: "tales" %>
</div>
<div class="item">
<%= image_tag "slider/slide-bg-2.jpg", class: "tales" %>
</div>
<div class="item">
<%= image_tag "slider/slide-bg-3.jpg", class: "tales" %>
</div>
</div>
<!-- Carousel nav -->
<a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
和我的 css 文件
.tales {
width: 100%;
height: 200px;
}
你想讓它響應嗎? 如果你是,那么我只會推薦以下內容:
.tales {
width: 100%;
}
.carousel-inner{
width:100%;
max-height: 200px !important;
}
但是,響應式處理此問題的最佳方法是使用如下媒體查詢:
/* Smaller than standard 960 (devices and browsers) */
@media only screen and (max-width: 959px) {}
/* Tablet Portrait size to standard 960 (devices and browsers) */
@media only screen and (min-width: 768px) and (max-width: 959px) {}
/* All Mobile Sizes (devices and browser) */
@media only screen and (max-width: 767px) {}
/* Mobile Landscape Size to Tablet Portrait (devices and browsers) */
@media only screen and (min-width: 480px) and (max-width: 767px) {}
/* Mobile Portrait Size to Mobile Landscape Size (devices and browsers) */
@media only screen and (max-width: 479px) {}
如果您使用bootstrap 4 Alpha 並且在 chrome 中圖像的高度有錯誤,我有一個解決方案:bootstrap 4 的文檔是這樣說的:
<div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
<div class="carousel-inner" role="listbox">
<div class="carousel-item active">
<img class="d-block img-fluid" src="..." alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block img-fluid" src="..." alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block img-fluid" src="..." alt="Third slide">
</div>
</div>
</div>
解決方法:
解決方案是在圖像周圍放置“div”,類為“.container”,如下所示:
<div class="carousel-item active">
<div class="container">
<img src="images/proyecto_0.png" alt="First slide" class="d-block img-fluid">
</div>
</div>
我知道這是一個較舊的帖子,但 Bootstrap 仍然活躍!
與@Eduardo 的帖子略有不同,我不得不修改:
#myCarousel.carousel.slide {
width: 100%;
max-width: 400px; !important
}
當我只修改.carousel-inner {}
,實際圖像的大小是固定的,但左/右控件在div
的一側顯示不正確。
我為 Bootstrap 3 推薦以下內容
.carousel-inner > .item > img,
.carousel-inner > .item > a > img {
min-height: 500px; /* Set slide height here */
}
我有同樣的問題。
當我的幻燈片向左移動時,我的高度更改為其原始高度,(在響應式網站中)
所以我只用 CSS 修復了它:
.carousel .item.left img{
width: 100% !important;
}
我發現如果您只想更改圖像占用的元素的高度,這將是有用的代碼。
.carousel-item {
height: 600px !important;
}
但是,這不會使圖像的大小動態化,它只會將圖像裁剪到其大小。
我創建了一個對我來說效果很好的響應式示例,我發現它非常簡單,看看我的輪播填充:
.carousel-fill {
height: -o-calc(100vh - 165px) !important;
height: -webkit-calc(100vh - 165px) !important;
height: -moz-calc(100vh - 165px) !important;
height: calc(100vh - 165px) !important;
width: auto !important;
overflow: hidden;
display: inline-block;
text-align: center;
}
.carousel-item {
text-align: center !important;
}
我的導航高度+頁腳小於 165 像素,因此該值對我有用。 取下適合您的值,我從引導程序覆蓋了 .carousel-item,因此請確保視頻居中。
我的旋轉木馬看起來像這樣,請注意視頻標簽上的“旋轉木馬填充”。
<div>
<div id="myCarousel" class="carousel slide carousel-fade text-center" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
<li data-target="#myCarousel" data-slide-to="3"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="carousel-item active">
<video autoplay muted class="carousel-fill">
<source src="~/Video/CATSTrade.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
<div class="carousel-caption">
<h2>CATS IV Trade engine</h2>
<p>Automated trading for high ROI</p>
</div>
</div>
<div class="carousel-item">
<video muted loop class="carousel-fill">
<source src="~/Video/itrs.mp4" type="video/mp4">
</video>
<div class="carousel-caption">
<h2>Machine learning</h2>
<p>Machine learning specialist</p>
</div>
</div>
<div class="carousel-item">
<video muted loop class="carousel-fill">
<source src="~/Video/frequency.mp4" type="video/mp4">
</video>
<div class="carousel-caption">
<h3>Low latency development</h3>
<p>Create ultra fast systems with our consultants</p>
</div>
</div>
<div class="carousel-item">
<img src="~/Images/data pipeline faded.png" class="carousel-fill" />
<div class="carousel-caption">
<h3>Big Data</h3>
<p>Maintain, generate, and host big data</p>
</div>
</div>
</div>
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#myCarousel" 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="#myCarousel" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
如果有人需要像我一樣控制視頻,我會像這樣開始和停止視頻:
<script language="JavaScript" type="text/javascript">
$(document).ready(function () {
$('.carousel').carousel({ interval: 8000 })
$('#myCarousel').on('slide.bs.carousel', function (args) {
var videoList = document.getElementsByTagName("video");
switch (args.from) {
case 0:
videoList[0].pause();
break;
case 1:
videoList[1].pause();
break;
case 2:
videoList[2].pause();
break;
}
switch (args.to) {
case 0:
videoList[0].play();
break;
case 1:
videoList[1].play();
break;
case 2:
videoList[2].play();
break;
}
})
});
</script>
只需將 height="400px" 放入圖像屬性中,例如
<img class="d-block w-100" src="../assets/img/natural-backdrop.jpg" height="400px" alt="First slide">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.