如何使用CSS对固定背景图像使用“线性渐变”

时间:2017-01-11 00:43:59

标签: css background-image gradient fixed linear-gradients

我在linear-gradient使用background-image。在我将background-attachment设置为fixed之前,这很有效。

如何使用background-attachment: fixed;

执行相同的效果

.photo {
  background-image: 
    linear-gradient(
      to bottom, 
      rgba(255,255,255,0) 80%, 
      rgba(255,255,255,1) 100%
    ),
    url(https://s30.postimg.org/v67rh5bdd/image.jpg);
  background-attachment: unset; /* 'fixed' does not work */
  background-position: center top;
  background-repeat: no-repeat;
  width: 100%;
  height: 55vh;
  position: absolute;
}
.panel {
  margin-top:30vh;
}
.panel-body {
  height: 100vh;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div class="container">
    <div clasas="row">
      <div class="photo"></div>
      <div class="col-xs-10 col-xs-offset-1">
        <div class="panel panel-primary">
          <div class="panel-heading">
            <h3 class="panel-title">Panel title</h3>
          </div>
          <div class="panel-body">
            Panel content
          </div>
        </div>
      </div>
    </div>
  </div>
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>

或jsbin:http://jsbin.com/miqizetoqu/edit?css,output

1 个答案:

答案 0 :(得分:1)

为什么不在const jsLoaderClass = global.document.getElementsByClassName('js-loader')[0]; // hide logo on pageload global.window.onload = function jsLoader() { jsLoaderClass.style.display = 'none'; }; div上使用伪元素来保持渐变。这可以绝对定位在图像的顶部。

.photo

见这个箱子:

http://jsbin.com/tepalahipe/edit?css,output