Bootstrap轮播宽度和高度

时间:2013-10-20 18:43:43

标签: javascript jquery html css twitter-bootstrap

我试图为图像(宽度: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">&lsaquo;</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">&rsaquo;</a>

和我的css文件

.tales {
  width: 100%;
  height: 200px;
}

8 个答案:

答案 0 :(得分:66)

您是否想让它变得敏感?如果你那么我会推荐以下内容:

.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) {}

答案 1 :(得分:9)

如果您使用 bootstrap 4 Alpha并且Chrome中的图片高度出错,我有一个解决方案: bootstrap 4的文档说明了这一点:

<?php

if(  isset($_POST['command']))
{
  $command = $_POST['command'];
  echo 'Hello ' . $command . "\n";
}

?>



<form method="post">

  Subject: <input type="text" name="command"> <br>

</form> 

<强>解决方案:

解决方案是将&#34; div&#34;围绕图像,使用类&#34; .container&#34;,像这样:

<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>

答案 2 :(得分:8)

我推荐以下Bootstrap 3

.carousel-inner > .item > img,
.carousel-inner > .item > a > img {
  min-height: 500px;    /* Set slide height here */

}

答案 3 :(得分:7)

我知道这是一个较老的帖子,但Bootstrap仍然活着并且踢!

与@ Eduardo的帖子略有不同,我不得不修改:

#myCarousel.carousel.slide {
    width: 100%; 
    max-width: 400px; !important
}

当我仅修改.carousel-inner {}时,实际图片的大小是固定的,但左/右控件显示错误地显示在div的一侧。

答案 4 :(得分:3)

嘿伙计我有同样的问题,

当我的幻灯片向左移动时,我的身高变为原来的高度,(在响应式网站中)

所以我只用CSS修复它:

.carousel .item.left img{
    width: 100% !important;
}

答案 5 :(得分:0)

我发现,如果您只是想更改图像所占元素的高度,这将是有帮助的代码

.carousel-item { height: 600px !important; }

但是,这不会使图像的大小动态变化,只会将图像裁剪到其大小。

答案 6 :(得分:0)

我创建了一个对我来说效果很好的响应式样本,并且发现旋转木马填充非常简单:

.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;
}

我的导航高度+页脚小于165px,所以该值对我有效。取一个适合您的值,我从引导程序中覆盖了.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>

答案 7 :(得分:-1)

只要把高度=&#34; 400px&#34;进入像

这样的图像属性
<img class="d-block w-100" src="../assets/img/natural-backdrop.jpg" height="400px" alt="First slide">