Bootstrap轮播使固定背景在Chrome中跳转

时间:2018-07-10 22:02:30

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

我正在使用具有以下设置的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,仅适用于图像未加载到集成代码段查看器中的情况。

以下情况仅在Google Chrome中发生,因此我不确定100%是否与Chrome,引导程序或我自己的东西有关:

每当旋转木马图像移动时,.fixed-bg div中的背景就会向右跳一些(在旋转木马移动时,它似乎与.container边界对齐)。不幸的是,在其他一些设置下,我现在无法重现,甚至使整个背景消失了一段时间。

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

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

  • 如前所述:Chrome似乎是唯一受影响的浏览器-Firefox和Edge可以正常工作。

我做错什么了吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试一下,它似乎要好得多。让我知道它是否适用于您的特定实例

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