用户在div背景图像上滚动时删除叠加div

时间:2017-11-12 10:50:03

标签: javascript jquery html css

Javascript noob在这里,所以任何帮助将不胜感激!

当用户向下滚动div时,我试图将.scroll-overlay容器设置为0不透明度(显示背景图像的溢出)。

只有当用户滚动主体元素时,我才设法让.scroll-overlay容器的不透明度淡化为0。

我如何专门定位#laptop-content容器?



var fadeStart = 0,
  fadeUntil = 0,
  fading = $('.scroll-overlay');

$(window).bind('scroll', function() {
  var offset = $(document).scrollTop(),
    opacity = 0;
  if (offset <= fadeStart) {
    opacity = 1;
  } else if (offset <= fadeUntil) {
    opacity = 1 - offset / fadeUntil;
  }
  fading.css('opacity', opacity).html(opacity);
});
&#13;
html {
  height: 2000px;
}

#laptop-content .img {
  -webkit-animation: scroll 0.2s ease-in-out 0s 1 alternate;
  -moz-animation: scroll 6.5s ease-in-out 0s 1 alternate;
  -o-animation: scroll 6.5s ease-in-out 0s 1 alternate;
  animation: scroll 1.2s ease-in-out 0s 1 alternate;
  background-image: url("http://via.placeholder.com/750x900");
  background-size: contain;
  background-repeat: no-repeat;
}

#laptop-content img {
  opacity: 0;
}

@-webkit-keyframes scroll {
  0% {
    background-position: 0px 40px;
  }
  50% {
    background-position: 0px -270px;
  }
  100% {
    background-position: 0px 270px;
  }
}

@-moz-keyframes scroll {
  0% {
    background-position: 0px -40px;
  }
  50% {
    background-position: 0px -270px;
  }
  100% {
    background-position: 0px -450px;
  }
}

@-o-keyframes scroll {
  0% {
    background-position: 0px -40px;
  }
  50% {
    background-position: 0px -470px;
  }
  100% {
    background-position: 0px -850px;
  }
}

@keyframes scroll {
  0% {
    background-position: 0px 300px;
  }
  80% {
    background-position: 0px -40px;
  }
  100% {
    background-position: 0px 0px;
  }
}

#laptop-container {
  padding-top: 50px;
  position: relative;
  float: left;
  z-index: 10;
  width: 750px;
  height: 500px;
}

.hide-scrollbar {
  position: absolute;
  top: 76px;
  left: 750px;
  height: 500px;
  width: 0;
  background: #000;
  opacity: 1;
  z-index: 100;
}

#laptop {
  width: auto;
  position: absolute;
  left: -189px;
}

#scroll-wrapper {
  cursor: default !important;
  top: 40px;
  width: 750px;
  height: 500px;
  margin-left: 18px;
  position: relative;
  overflow: hidden;
  -ms-touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  -webkit-text-size-adjust: none;
  -moz-text-size-adjust: none;
  -ms-text-size-adjust: none;
  -o-text-size-adjust: none;
  text-size-adjust: none;
  -webkit-box-shadow: -1px 10px 50px -6px rgba(0, 0, 0, 1);
  -moz-box-shadow: -1px 10px 50px -6px rgba(0, 0, 0, 1);
  box-shadow: -1px 10px 50px -6px rgba(0, 0, 0, 1);
}

#laptop-content {
  position: absolute;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  -webkit-transform: translateZ(0);
  -moz-transform: translateZ(0);
  -ms-transform: translateZ(0);
  -o-transform: translateZ(0);
  transform: translateZ(0);
}

#laptop-content img {
  width: 100%;
}

.scroll-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  z-index: 1;
}

@-webkit-keyframes scroll-ani {
  0% {
    opacity: 1;
    top: 29%;
  }
  15% {
    opacity: 1;
    top: 50%;
  }
  50% {
    opacity: 0;
    top: 50%;
  }
  100% {
    opacity: 0;
    top: 29%;
  }
}

@-moz-keyframes scroll-ani {
  0% {
    opacity: 1;
    top: 29%;
  }
  15% {
    opacity: 1;
    top: 50%;
  }
  50% {
    opacity: 0;
    top: 50%;
  }
  100% {
    opacity: 0;
    top: 29%;
  }
}

@keyframes scroll-ani {
  0% {
    opacity: 1;
    top: 29%;
  }
  15% {
    opacity: 1;
    top: 50%;
  }
  50% {
    opacity: 0;
    top: 50%;
  }
  100% {
    opacity: 0;
    top: 29%;
  }
}

.mouse-scroll {
  border: none;
  display: inline-block;
  margin-top: 10%;
  text-decoration: none;
  overflow: hidden;
  width: 100%;
  text-align: center;
  margin-top: 200px;
}

.mouse-scroll .mouse {
  position: relative;
  display: block;
  width: 18px;
  height: 28px;
  margin: 0 auto 20px;
  -webkit-box-sizing: border-box;
  border: 2px solid #ffffff;
  border-radius: 13px;
}

.mouse-scroll .mouse .mouse-movement {
  position: absolute;
  display: block;
  top: 29%;
  left: 50%;
  width: 4px;
  height: 4px;
  margin: -2px 0 0 -2px;
  background: #ffffff;
  border-radius: 50%;
  -webkit-animation: scroll-ani 2s linear infinite;
  animation: scroll-ani 2s linear infinite;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="laptop-container">
  <div class="hide-scrollbar"></div>

  <div id="scroll-wrapper" style="overflow-y: auto; overflow-x: hidden; cursor: move; touch-action: none; user-select: none;">
    <div>
      <div id="laptop-content">
        <div class="scroll-overlay">
          <div class="mouse-scroll">
            <span class="mouse">
							<span class="mouse-movement"></span>
            </span>
          </div>
        </div>

        <div class="img"><img src="http://via.placeholder.com/750x900"></div>

      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

如果我理解你想要什么,你已经差不多了。您只需将滚动处理程序绑定到#scroll-wrapper并计算#scroll-wrapper的滚动偏移量而不是整个文档。

在这里,您的代码需要进行必要的修改:

&#13;
&#13;
var fadeStart = 0,
  fadeUntil = 0,
  fading = $('.scroll-overlay');

$('#scroll-wrapper').bind('scroll', function() {
  var offset = $('#scroll-wrapper').scrollTop(),
    opacity = 0;
  if (offset <= fadeStart) {
    opacity = 1;
  } else if (offset <= fadeUntil) {
    opacity = 1 - offset / fadeUntil;
  }
  fading.css('opacity', opacity).html(opacity);
});
&#13;
html {
  height: 2000px;
}

#laptop-content .img {
  -webkit-animation: scroll 0.2s ease-in-out 0s 1 alternate;
  -moz-animation: scroll 6.5s ease-in-out 0s 1 alternate;
  -o-animation: scroll 6.5s ease-in-out 0s 1 alternate;
  animation: scroll 1.2s ease-in-out 0s 1 alternate;
  background-image: url("http://via.placeholder.com/750x900");
  background-size: contain;
  background-repeat: no-repeat;
}

#laptop-content img {
  opacity: 0;
}

@-webkit-keyframes scroll {
  0% {
    background-position: 0px 40px;
  }
  50% {
    background-position: 0px -270px;
  }
  100% {
    background-position: 0px 270px;
  }
}

@-moz-keyframes scroll {
  0% {
    background-position: 0px -40px;
  }
  50% {
    background-position: 0px -270px;
  }
  100% {
    background-position: 0px -450px;
  }
}

@-o-keyframes scroll {
  0% {
    background-position: 0px -40px;
  }
  50% {
    background-position: 0px -470px;
  }
  100% {
    background-position: 0px -850px;
  }
}

@keyframes scroll {
  0% {
    background-position: 0px 300px;
  }
  80% {
    background-position: 0px -40px;
  }
  100% {
    background-position: 0px 0px;
  }
}

#laptop-container {
  padding-top: 50px;
  position: relative;
  float: left;
  z-index: 10;
  width: 750px;
  height: 500px;
}

.hide-scrollbar {
  position: absolute;
  top: 76px;
  left: 750px;
  height: 500px;
  width: 0;
  background: #000;
  opacity: 1;
  z-index: 100;
}

#laptop {
  width: auto;
  position: absolute;
  left: -189px;
}

#scroll-wrapper {
  cursor: default !important;
  top: 40px;
  width: 750px;
  height: 500px;
  margin-left: 18px;
  position: relative;
  overflow: hidden;
  -ms-touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  -webkit-text-size-adjust: none;
  -moz-text-size-adjust: none;
  -ms-text-size-adjust: none;
  -o-text-size-adjust: none;
  text-size-adjust: none;
  -webkit-box-shadow: -1px 10px 50px -6px rgba(0, 0, 0, 1);
  -moz-box-shadow: -1px 10px 50px -6px rgba(0, 0, 0, 1);
  box-shadow: -1px 10px 50px -6px rgba(0, 0, 0, 1);
}

#laptop-content {
  position: absolute;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  -webkit-transform: translateZ(0);
  -moz-transform: translateZ(0);
  -ms-transform: translateZ(0);
  -o-transform: translateZ(0);
  transform: translateZ(0);
}

#laptop-content img {
  width: 100%;
}

.scroll-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  z-index: 1;
}

@-webkit-keyframes scroll-ani {
  0% {
    opacity: 1;
    top: 29%;
  }
  15% {
    opacity: 1;
    top: 50%;
  }
  50% {
    opacity: 0;
    top: 50%;
  }
  100% {
    opacity: 0;
    top: 29%;
  }
}

@-moz-keyframes scroll-ani {
  0% {
    opacity: 1;
    top: 29%;
  }
  15% {
    opacity: 1;
    top: 50%;
  }
  50% {
    opacity: 0;
    top: 50%;
  }
  100% {
    opacity: 0;
    top: 29%;
  }
}

@keyframes scroll-ani {
  0% {
    opacity: 1;
    top: 29%;
  }
  15% {
    opacity: 1;
    top: 50%;
  }
  50% {
    opacity: 0;
    top: 50%;
  }
  100% {
    opacity: 0;
    top: 29%;
  }
}

.mouse-scroll {
  border: none;
  display: inline-block;
  margin-top: 10%;
  text-decoration: none;
  overflow: hidden;
  width: 100%;
  text-align: center;
  margin-top: 200px;
}

.mouse-scroll .mouse {
  position: relative;
  display: block;
  width: 18px;
  height: 28px;
  margin: 0 auto 20px;
  -webkit-box-sizing: border-box;
  border: 2px solid #ffffff;
  border-radius: 13px;
}

.mouse-scroll .mouse .mouse-movement {
  position: absolute;
  display: block;
  top: 29%;
  left: 50%;
  width: 4px;
  height: 4px;
  margin: -2px 0 0 -2px;
  background: #ffffff;
  border-radius: 50%;
  -webkit-animation: scroll-ani 2s linear infinite;
  animation: scroll-ani 2s linear infinite;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="laptop-container">
  <div class="hide-scrollbar"></div>

  <div id="scroll-wrapper" style="overflow-y: auto; overflow-x: hidden; cursor: move; touch-action: none; user-select: none;">
    <div>
      <div id="laptop-content">
        <div class="scroll-overlay">
          <div class="mouse-scroll">
            <span class="mouse">
							<span class="mouse-movement"></span>
            </span>
          </div>
        </div>

        <div class="img"><img src="http://via.placeholder.com/750x900"></div>

      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;