首页 > 解决方案 > 滚动显示图像:保持底部图像固定,直到完成视差覆盖,然后继续页面滚动

问题描述

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

这是我希望达到的效果: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>

标签: javascriptparallax

解决方案


推荐阅读