【问题标题】:Apply "background-size:cover" to Twitter Bootstrap Carousel slides将“background-size:cover”应用于 Twitter Bootstrap Carousel 幻灯片
【发布时间】:2012-11-19 01:05:34
【问题描述】:

我正在使用带有三张幻灯片的基本 Bootstrap Carousel。在我实现滑块之前,我通过其各自 div 的 css background-image 获得了一张静态图像。在我的 CSS 中,我使用了 background-size:cover,并且非常喜欢图像响应不同浏览器宽度的方式。根据图像的宽度,我可以让图像的重要部分始终保持可见和居中,而两侧的额外宽度仅在宽屏显示器上可见作为附加效果。

现在我正在使用 Bootstrap 的 Carousel,我想保留相同的图像行为,但即使我为幻灯片的视口分配了尺寸,我仍然无法显示我的背景图像。我目前已对其进行了设置,因此每张幻灯片都显示一个单独的 css 类,并且每张幻灯片都有不同的背景图像,因此它可以在三个图像之间滚动。

如果通过 background-image 无法做到这一点,是否有另一种方法来操作图像元素,使其显示行为与 background-size:cover 相同?

这是我的 HTML:

 <div id="myCarousel" class="carousel slide">
  <!-- Carousel items -->
  <div class="carousel-inner">
    <div class="active item ad1">
      <img src="{{ STATIC_URL }}img/TestBannerAd.png">
    </div>
    <div class="item ad2">
      <img src="{{ STATIC_URL }}img/TestBannerAd2.png">
    </div>
    <div class="item ad3">
      <img src="{{ STATIC_URL }}img/TestBannerAd3.png">
    </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>
</div>

还有我的 CSS:

#myCarousel {
    width: 100%;
}
.carousel-control {
    margin-top: 20px;
}
.carousel-inner {
    width: 100%;
    height: 400px;
}
.ad1 {
    background: url(../img/TestBannerAd.png) no-repeat center center fixed; 
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}
.ad2 {
    background: url(../img/TestBannerAd2.png) no-repeat center center fixed; 
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}
.ad3 {
    background: url(../img/TestBannerAd3.png) no-repeat center center fixed; 
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

希望这不是一个愚蠢的问题,我们可以解决这个问题!

【问题讨论】:

    标签: css twitter-bootstrap slider background-image


    【解决方案1】:

    我知道我的回复已经很晚了,但我刚刚通过搜索找到了您的问题,并做了类似的事情。首先,您需要从 HTML 中删除图像并稍微更改 CSS 以将高度应用于轮播中所有具有“item”类的 div。

    将我的修改应用于您的代码应该是这样的:

    <div id="myCarousel" class="carousel slide">
      <!-- Carousel items -->
      <div class="carousel-inner">
         <div class="item active ad1">
            <div class="carousel-caption">
               <h4>First Thumbnail label</h4>
               <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>
            </div>
         </div>
         <div class="item ad2">
            <div class="carousel-caption">
                <h4>Second Thumbnail label</h4>
                <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>
             </div>
         </div>
         <div class="item ad3">
            <div class="carousel-caption">
               <h4>Third Thumbnail label</h4>
               <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>
            </div>
        </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>
    </div>
    

    还有 CSS:

    #myCarousel {
        width: 100%;
    }
    .carousel-control {
        margin-top: 20px;
    }
    .carousel-inner .item {
        width: 100%;
        height: 400px;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -o-background-size: cover;
        background-size: cover;
    }
    .ad1 {
        background: url(../img/TestBannerAd.png) no-repeat center center fixed; 
    }
    .ad2 {
        background: url(../img/TestBannerAd2.png) no-repeat center center fixed; 
    }
    .ad3 {
        background: url(../img/TestBannerAd3.png) no-repeat center center fixed; 
    }
    

    【讨论】:

    • 感谢您发布此信息。我能够实现我想做的事情,但我似乎无法在这些幻灯片中添加文本。知道我将如何使用您的代码来做到这一点吗?谢谢
    • 我用幻灯片标题和文本的标记修改了代码,只是一个带有标题和段落的类“carousel-caption”的div。我还在第一个滑块中添加了“活动”类,否则它将无法正常工作。
    【解决方案2】:

    这相当于 jQuery 的封面代码:

     $.each( jQuery('.carousel .item'), function( i, val ) {
        $(this).css('background-image','url('+$(this).find('img').attr('src')+')').css('background-size','cover').find('img').css('visibility','hidden');
      });
    

    它获取图像源并传递给项目,然后隐藏实际图像。

    此外,您可能需要隐藏轮播直到 jQuery.each 完成。我建议对 CSS 级别的图像进行可见性:隐藏 hack。因此用户不会在更大的屏幕上注意到图像放大。

    【讨论】:

    • 不错的方法。但是如果禁用了 JS 就不行了。
    • 旋转木马是否可以在禁用 JS 的情况下使用?禁用 js 的人应该处理他们破坏用户体验的选择。
    • 不错 :) 必须同时设置 .carousel 和 .item 的高度,以防止大图像破坏布局。
    【解决方案3】:

    刚找到这个密码笔https://codepen.io/wisnust10/pen/mPJwWv/ 它使用另一种解决方案。 (见 CSS)

    .item:nth-child(1) {
      background: url('https://snap-photos.s3.amazonaws.com/img-thumbs/960w/HZZKGVVJ6I.jpg');
      background-size: cover;
      background-position: center center;
      background-repeat: no-repeat;
        }
    

    在 chrome 和 IE 中对此进行了测试,它成功了。

    希望它能有所帮助。

    【讨论】:

      【解决方案4】:

      在“.item”类中使用“背景属性”,不使用“img”标签

      .carousel {
          height: 300px;
      }
      
      .carousel-inner {
          height: 100%;
      }
      
      .item {
          width: 100%;
          height: 100%;
      
          -webkit-background-size: cover;
          -moz-background-size: cover;
          -o-background-size: cover;
          background-size: cover;
      
          background-position: center;
      }
      /*
      img {                
          object-fit: cover;
      }
      */
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css" />
      
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
              
      <div id="myCarousel" class="carousel slide">
      
          <!-- Carousel items -->
          <div class="carousel-inner">
          
              <div class="item active" style="background-image: url(https://dummyimage.com/300x200/000/fff)" >
      <!--            <img src="http://placehold.it/650x450/aaa&text=Item 1" style="width: 100%; height: 100%" /> -->
              </div>
              
              <div class="item" style="background-image: url(https://dummyimage.com/600x400/000/fff)" >
      <!--            <img src="http://placehold.it/350x350/aaa&text=Item 2" style="width: 100%; height: 100%" /> -->
              </div>
              
              <div class="item" style="background-image: url(https://dummyimage.com/1200x800/000/fff)" >
      <!--            <img src="http://placehold.it/350x350/aaa&text=Item 3" style="width: 100%; height: 100%" /> -->
              </div>
              
          </div>
      
          <!-- Left and right controls -->
          <a class="left carousel-control" href="#myCarousel" data-slide="prev">
              <span class="glyphicon glyphicon-chevron-left"></span>
              <span class="sr-only">Previous</span>
          </a>
          <a class="right carousel-control" href="#myCarousel" data-slide="next">
              <span class="glyphicon glyphicon-chevron-right"></span>
              <span class="sr-only">Next</span>
          </a>
      
      </div>

      代码笔

    • Editor
    • Full view
    • Version of img

      【讨论】:

        猜你喜欢
        • 2012-07-11
        • 2014-08-02
        • 2013-04-29
        • 1970-01-01
        • 2021-04-15
        • 2013-02-05
        • 2015-07-17
        • 2022-01-26
        • 2023-03-25
        相关资源
        最近更新 更多