滚动显示图像:保持底部图像固定,直到完成视差覆盖为止,然后继续滚动页面

时间:2019-06-05 23:03:55

标签: javascript parallax

英雄图像需要保持固定,直到视差完成,然后页面才能正常滚动。

这是我希望达到的效果:http://www.bbdosf.com/

如您所见,背景并非完美对齐。 https://codepen.io/danielle93/pen/arrBqd

window.addEventListener('scroll', function (e) {
  
  var scrolled = window.pageYOffset;
  const background = document.querySelector('.cover');
  background.style.top = - (scrolled * 10) + 'px';
  
});
body{
  height: 1200px;
  margin: 0;
}

.container{
  height:100vh;
  overflow: hidden;
}

div{
  position: relative;
}

.cover{
  z-index: 1;
  position: absolute;
  background: #fff;
  opacity: .75;
  height: 100%;
  width: 100%;
}

.underneath{
  z-index: 0;
  width: 100%;
}

.knockout {
  background: url(https://placekitten.com/1200);
  background-size: cover;
  background-repeat; no-repeat;
  background-position: top;
  background-attachment: fixed;
  color: red;
  -webkit-text-fill-color: transparent;
  -webkit-background-clip: text;
  font-weight: bold;
  font-size: 300px;
  font-family: arial, helvetica;
  margin: 200px auto;
  text-align: center;
}
<div class="container">
  <div class="cover">
    <div class="knockout"><a>meow.</a>     </div>
  </div>

  <img class="underneath" src="https://placekitten.com/1200" alt="">
</div>

0 个答案:

没有答案