【问题标题】:Bootstrap carousel makes fixed background jump in ChromeBootstrap 轮播在 Chrome 中固定背景跳转
【发布时间】:2018-12-18 20:20:24
【问题描述】:

我正在使用具有以下设置的 bootstrap v 4.1.0:

.row {
  background: #f8f9fa;
  margin-top: 20px;
}

.col {
  border: solid 1px #6c757d;
  padding: 10px;
}

.carousel-item {
  max-height: calc(30vh);
}

.pos-rel {
  position: relative;
}

.container-breaker {
  position: relative;
  left: calc(-49vw + 50%);
  width: 99vw;
}

.fixed-bg {
  background: url('http://www.jesslaversdesign.com/wp-content/uploads/2012/05/test-image-750x500px.jpg') no-repeat center center fixed;
  min-height: 25rem;
  background-size: cover;
  background-attachment: fixed;
}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />

<div class="header-carousel">
  <div id="carouselLRIndicators" class="carousel slide" data-ride="carousel">
    <ol class="carousel-indicators">
      <li data-target="#carouselLRIndicators" data-slide-to="0" class="active"></li>
      <li data-target="#carouselLRIndicators" data-slide-to="1"></li>
    </ol>
    <div class="carousel-inner">
      <div class="carousel-item active">
        <img class="d-block w-100 img-fluid" src="https://upload.wikimedia.org/wikipedia/commons/f/f6/Stripes_scaling_test_image_orig.png" alt="First slide">
      </div>
      <div class="carousel-item">
        <img class="d-block w-100 img-fluid" src="https://upload.wikimedia.org/wikipedia/commons/f/f6/Stripes_scaling_test_image_orig.png" alt="Second slide">
      </div>
    </div>
  </div>
</div>

<div class="container">
  <div class="pos-rel">
    <div class="container-breaker">
      <div class="fixed-bg">
        test
      </div>
    </div>
  </div>
</div>

JSFiddle 仅适用于图像未在集成的 sn-p 查看器中加载的情况。

以下情况仅发生在 Google Chrome 中,因此我不能 100% 确定这是 Chrome、引导程序还是我自己的问题:

每当轮播图像移动时,.fixed-bg div 中的背景就会向右跳一点(在轮播移动时,它似乎与 .container 边框对齐)。在其他一些我很遗憾现在无法重现的设置中,它甚至使整个背景消失了一段时间。

到目前为止我发现了什么:

  • 当我删除background-attachment: fixed; 时,背景似乎不再跳跃,但我真的很想保留它。

  • 如前所述:Chrome 似乎是唯一受影响的浏览器 - Firefox 和 Edge 按预期工作。

我做错了吗?

【问题讨论】:

    标签: css twitter-bootstrap google-chrome bootstrap-4 carousel


    【解决方案1】:

    你可以试试这个,好像好多了。让我知道它是否适用于您的特定实例

    HTML

    <div class="header-carousel">
      <div id="carouselLRIndicators" class="carousel slide" data-ride="carousel">
        <ol class="carousel-indicators">
          <li data-target="#carouselLRIndicators" data-slide-to="0" class="active"></li>
          <li data-target="#carouselLRIndicators" data-slide-to="1"></li>
        </ol>
        <div class="carousel-inner">
          <div class="carousel-item active"> 
            <img class="d-block w-100 img-fluid" src="https://upload.wikimedia.org/wikipedia/commons/f/f6/Stripes_scaling_test_image_orig.png" alt="First slide">
          </div>
          <div class="carousel-item">
            <img class="d-block w-100 img-fluid" src="https://upload.wikimedia.org/wikipedia/commons/f/f6/Stripes_scaling_test_image_orig.png" alt="Second slide">
          </div>
        </div>
      </div>
    </div>
    
    <div class="container">
      <div class="pos-rel">
        <div class="container-breaker">
          <div class="fixed-bg">
            test
          </div>
        </div>
      </div>
    </div>
    

    CSS

    .row {
      background: #f8f9fa;
      margin-top: 20px;
    }
    
    .col {
      border: solid 1px #6c757d;
      padding: 10px;
    }
    
    .carousel-item {
      max-height: calc(30vh);
    }
    
    .pos-rel {
      position: relative;
    }
    .container-breaker {
      position: relative;
      width: 100%;
    }
    
    .fixed-bg {
      background: url('http://www.jesslaversdesign.com/wp-content/uploads/2012/05/test-image-750x500px.jpg') no-repeat center center fixed;
      min-height: 25rem;
      background-size: cover;
      background-attachment: fixed;
    }
    

    【讨论】:

    • 所以你只用width: 100%;替换了left: calc(-49vw + 50%); width: 99vw;?不幸的是,这无济于事,因为它改变了容器的整体外观。我的版本覆盖了视口的整个宽度,而你的版本只覆盖了 .container 的宽度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 2014-06-25
    • 2021-08-20
    • 1970-01-01
    • 2016-10-15
    • 2019-11-22
    相关资源
    最近更新 更多