【问题标题】:How to get Bootstrap Carousel to fit 100% to screen?如何让 Bootstrap Carousel 100% 适合屏幕?
【发布时间】:2015-06-19 04:48:06
【问题描述】:

我想知道如何让 Bootstrap's Carousel example 中的轮播 100% 适合屏幕,而不是现在的显示方式,它允许在加载主登录页面时包含一些空白的圆形图像。

Here's a simple illustration of what I'm trying to get working.

页面来源(相关位):

<!-- Carousel
  ================================================== -->
<div id="myCarousel" class="carousel slide" 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>
  </ol>
  <div class="carousel-inner" role="listbox">
    <div class="item active">
      <img class="first-slide" src="data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" alt="First slide">
      <div class="container">
        <div class="carousel-caption">
          <h1>Example headline.</h1>
          <p>Note: If you're viewing this page via a <code>file://</code> URL, the "next" and "previous" Glyphicon buttons on the left and right might not load/display properly due to web browser security rules.</p>
          <p><a class="btn btn-lg btn-primary" href="#" role="button">Sign up today</a></p>
        </div>
      </div>
    </div>
    <div class="item">
      <img class="second-slide" src="data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" alt="Second slide">
      <div class="container">
        <div class="carousel-caption">
          <h1>Another example headline.</h1>
          <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
          <p><a class="btn btn-lg btn-primary" href="#" role="button">Learn more</a></p>
        </div>
      </div>
    </div>
    <div class="item">
      <img class="third-slide" src="data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" alt="Third slide">
      <div class="container">
        <div class="carousel-caption">
          <h1>One more for good measure.</h1>
          <p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
          <p><a class="btn btn-lg btn-primary" href="#" role="button">Browse gallery</a></p>
        </div>
      </div>
    </div>
  </div>
  <a class="left carousel-control" href="#myCarousel" 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="#myCarousel" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div><!-- /.carousel -->

carousel.css(相关位):

/* CUSTOMIZE THE CAROUSEL
-------------------------------------------------- */

/* Carousel base class */
.carousel {
  height: 500px;
  margin-bottom: 60px;
}
/* Since positioning the image, we need to help out the caption */
.carousel-caption {
  z-index: 10;
}

/* Declare heights because of positioning of img element */
.carousel .item {
  height: 500px;
  background-color: #777;
}
.carousel-inner > .item > img {
  position: absolute;
  top: 0;
  left: 0;
  min-width: 100%;
  height: 500px;
}

我知道手头的任务非常简单,并且我承认在过去的一个小时左右我已经尝试过修改 carousel.css 文件(更改各种高度位置、z-index 等值),但是如果不把事情搞砸,我似乎无法让调整大小起作用。更具体地说,我尝试添加

【问题讨论】:

标签: html css twitter-bootstrap carousel


【解决方案1】:

您为轮播和轮播项目​​设置了固定高度。那么它显然将是那个大小。

试试这个

html,body{
   height:100%;
}
.carousel,.item,.active{
   height:100%;
 }
.carousel-inner{
    height:100%;
}

Look at this question for demo and more.

即使更改后它也不起作用,那么您可以在这里用这个替换您的旋转木马 DEMO

也可以通过 jquery 获取窗口总高度,并将其设置为轮播包装器和其他元素的高度。

$(document).ready(function(){
   var height = $(window).height();  //getting windows height
   jQuery('#myCarousel').css('height',height+'px');   //and setting height of carousel
});

【讨论】:

  • 没有骰子。我认为您链接到的示例不起作用,因为它基于 Bootstrap v2.3.1(而我使用的是 v3.3.4)。虽然我想我可以使用您链接到的 DEMO 中的代码,但我更愿意为当前版本找到解决方案。
  • 我最终还是替换了我的代码,但使用了此处找到的示例:github.com/IronSummitMedia/startbootstrap-full-slider。不过感谢您的帮助!
【解决方案2】:

在尝试使用 Bootstrap 网站上提供的当前版本的轮播后,我只是认为尝试为他们提供的代码寻找解决方法只会比它的价值更麻烦。

所以,我找到了 this example,只是将轮播 HTML 替换为 the one provided by their source - starting at the 'header',并将 carousel.css 中的轮播部分替换为 their full-slider.css part

最终代码如下所示:

HTML

<!-- Full Page Image Background Carousel Header -->
<header id="myCarousel" class="carousel slide">
    <!-- 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>
    </ol>

    <!-- Wrapper for Slides -->
    <div class="carousel-inner">
        <div class="item active">
            <!-- Set the first background image using inline CSS below. -->
            <div class="fill" style="background-image:url('http://placehold.it/1900x1080&text=Slide One');"></div>
            <div class="carousel-caption">
                <h2>Caption 1</h2>
            </div>
        </div>
        <div class="item">
            <!-- Set the second background image using inline CSS below. -->
            <div class="fill" style="background-image:url('http://placehold.it/1900x1080&text=Slide Two');"></div>
            <div class="carousel-caption">
                <h2>Caption 2</h2>
            </div>
        </div>
        <div class="item">
            <!-- Set the third background image using inline CSS below. -->
            <div class="fill" style="background-image:url('http://placehold.it/1900x1080&text=Slide Three');"></div>
            <div class="carousel-caption">
                <h2>Caption 3</h2>
            </div>
        </div>
    </div>

    <!-- Controls -->
    <a class="left carousel-control" href="#myCarousel" data-slide="prev">
        <span class="icon-prev"></span>
    </a>
    <a class="right carousel-control" href="#myCarousel" data-slide="next">
        <span class="icon-next"></span>
    </a>

</header>

CSS

/* CUSTOMIZE THE CAROUSEL
-------------------------------------------------- */
.carousel,
.item,
.active {
    height: 100%;
    margin-bottom: 60px;
}

.carousel-inner {
    height: 100%;
}

/* Background images are set within the HTML using inline CSS, not here */

.fill {
    width: 100%;
    height: 100%;
    background-position: center;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    background-size: cover;
    -o-background-size: cover;
}

IMO,无论如何,这个版本更容易阅读。感谢@K.C。寻求帮助!

【讨论】:

    【解决方案3】:

    参加聚会有点晚了……但我想永远不会迟到。

    无论如何,我对此采取了不同的方法。

    CSS:

    html, body, .carousel,  .carousel-inner, .carousel-inner .item {
       height: 100%;
    }
    
    .Outside {
        background-image: url("../Your/Image/Path/Outside.jpg");
        z-index: -2;
        display: block;
        position: fixed;
        width: 100%;
        height: 100%;
        background-repeat: no-repeat;
        background-size: cover;
        background-position: 50% 50%;
    }
    

    HTML:

    <div id="carousel1" class="carousel carousel-fade" data-ride="carousel">
    
    <!-- Indicators -->
      <ol class="carousel-indicators">
        <li data-target="#carousel1" data-slide-to="0" class="active"></li>
        <li data-target="#carousel1" data-slide-to="1"></li>
        <li data-target="#carousel1" data-slide-to="2"></li>
      </ol>
    
      <!-- Wrapper for slides -->
      <div class="carousel-inner" role="listbox">
        <div class="item active">
          <div class="Outside"></div>
          <div class="carousel-caption">
          </div>
        </div>
       </div>
    </div>
    

    希望这会有所帮助。

    【讨论】:

      【解决方案4】:

      我有类似的要求,我发现下面的引导类几乎可以完成这项工作。

      class="d-block w-100 h-100"

      &lt;img src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQMueyisr8wYKRW8wS4G19HmwvGg_T_j5U3LgBfSPmdUc8aq-AX" class="d-block w-100 h-100" &gt;

      【讨论】:

      • 在 Bootstrap 5 中对我没有影响。
      【解决方案5】:

      这是一个对我有用的简单解决方案。

      将此类添加到 carousel-inner div 内的图像标签中。

      CSS:

      .carousel-img-width {
        width: 100%;
      }
      

      HTML:

      <img src="img_url" alt="text" class="carousel-img-width">
      

      【讨论】:

        【解决方案6】:

        这可能是因为引导类的媒体查询试图将最大宽度设置为更小的尺寸。 您可以将自定义类应用于轮播并覆盖最大宽度以适应屏幕。

        .custom-carousel {
          max-width:100%;
          width:100%;
        }
        HTML:
        <div id="carousel1" class="carousel carousel-fade custom-carousel" data-ride="carousel">
        
        <!-- Indicators -->
          <ol class="carousel-indicators">
            <li data-target="#carousel1" data-slide-to="0" class="active"></li>
            <li data-target="#carousel1" data-slide-to="1"></li>
            <li data-target="#carousel1" data-slide-to="2"></li>
          </ol>
        
          <!-- Wrapper for slides -->
          <div class="carousel-inner" role="listbox">
            <div class="item active">
              <div class="Outside"></div>
              <div class="carousel-caption">
              </div>
            </div>
           </div>
        </div>

        【讨论】:

          猜你喜欢
          • 2021-12-31
          • 2019-09-21
          • 1970-01-01
          • 2013-07-27
          • 1970-01-01
          • 2020-02-03
          • 2016-08-25
          • 2022-11-30
          • 2015-03-29
          相关资源
          最近更新 更多