【问题标题】:How can I recreate this fade-opacity-on-scroll, fixed background design?如何重新创建这种滚动时不透明的固定背景设计?
【发布时间】:2016-03-02 20:25:37
【问题描述】:

有问题的设计是:http://hthworldwide.net/

我专门尝试重新创建在滚动时淡化不透明度以在背景中显示固定图像的着陆/英雄元素。

我已经接近了,但我似乎无法确定这件事。问题是如果不给透明字母提供白色背景,我就无法让白色滤镜在整个屏幕上伸展。理想情况下,您应该能够通过文本看到背景图像,如示例所示。

这是我目前所拥有的:http://codepen.io/rsprice/pen/reVazo

html:

<div class="background"></div>
<div class="opacity-layer"></div>
<div class="text"></div>
<div class="other"></div>

css:

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

.background {
  width: 3000px;
  height: 1000px;
  background-position: 0px -300px;
  background-size: 130%;
  box-sizing: border-box;
  z-index: -1;
  position: fixed;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
  margin: 0;
  padding: 0;
  color: white;
  background: url('https://images.unsplash.com/photo-1454496522488-7a8e488e8606?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&s=a9504411622da8e65df977606f82479c');
}

.opacity-layer {
  transform: rotateZ(360deg);
  -webkit-perspective: 1000;
  box-sizing: border-box;
  position: relative;
  width: 2000px;
  min-height: 100%;
  background-size: cover;
  background-position: center top;
  overflow: hidden;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
  margin: 0;
  padding: 0;
}

.text {
  height: 300px;
  width: 100%;
  background-position: center;
  background: url('http://hthworldwide.net/sites/all/themes/hthworldwide/style/images/home-intro.png') no-repeat;
  background-color: transparent;
  padding: 0;
  margin: 0;
}

.text img {
  margin: 0 auto;
}

.other {
  height: 2000px;
}

jQuery:

$(window).scroll(function(){
  $(".opacity-layer").css("opacity", 1 - $(window).scrollTop() / 250);
});

【问题讨论】:

    标签: jquery html css responsive-design


    【解决方案1】:

    如果您查看源代码,不仅可以使用单个div,还可以使用表格。

    看起来像这样:

    +--+--------------------+--+
    |  |                    |  |
    +--+--------------------+--+
    |  |  TRANSPARENT TEXT  |  |
    +--+--------------------+--+
    |  |                    |  |
    +--+--------------------+--+
    

    而代码是这样的(简化):

    <div>
       <table>
          <tr>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td id="centerCell"></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
          </tr>
       <table>
    </div>
    

    该表格中的所有单元格都有白色背景,但中间是一个(其中包含带有透明文本的白色图像)。此外,中心单元格是唯一设置宽度的单元格,因此其他单元格将占据表格/窗口的其余部分。然后当页面滚动时,不是改变图像层的不透明度,而是改变包含整个表格的div的不透明度。

    这几乎就是你在codepen 中的样子(不是问题中的代码),你只需要稍微更改一下代码以适应它:

    $(window).scroll(function(){
      $(".container table").css("opacity", .95 - $(window).scrollTop() / 250);
    });
    html,
    body {
      height: 100%;
      margin: 0;
      padding: 0;
      background: url('https://images.unsplash.com/photo-1454496522488-7a8e488e8606?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&s=a9504411622da8e65df977606f82479c');
    }
    
    .container {
      width: 100%;
      background-position: 0px -300px;
      background-size: 130%;
      box-sizing: border-box;
      border: 0;
      font-size: 100%;
      font: inherit;
      vertical-align: baseline;
      margin: 0;
      padding: 0;
      color: white;
      background:url(http://hthworldwide.net/sites/all/themes/hthworldwide/style/images/background-home-hk2.jpg);
    }
    
    #text {
      height: 300px;
      width: 980px;
      background-position: center;
      background: url('http://hthworldwide.net/sites/all/themes/hthworldwide/style/images/home-intro.png') no-repeat;
      background-color: transparent;
      padding: 0;
      margin: 0;
    }
    
    table {
      width: 100%;
      height: 100%;
      border:0;
      border-collapse:collapse;
      
    }
    
    td {
      opacity: .95;
      height:100px;
      background-color:white;
    }
    
    #footer { height:1000px; }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <div class="container">
      <table border="0">
        <tbody>
          <tr>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td id="text"></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
          </tr>
        </tbody>
      </table>
    </div>
    
    <div id="footer"></div>

    【讨论】:

    • 感谢阿尔瓦罗的详细解释!我真的很感激。
    猜你喜欢
    • 2012-07-02
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    相关资源
    最近更新 更多