javascript - 当用户在 div 背景图像上滚动时删除覆盖 div

标签 javascript jquery html css

此处为 Javascript 菜鸟,如有任何帮助,我们将不胜感激!

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

我已经设法让 .scroll-overlay 容器的不透明度仅在用户滚动 body 元素时淡入 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);
});
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;
}
<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>

最佳答案

如果我正确理解了您的需求,那么您就快完成了。您只需将滚动处理程序绑定(bind)到 #scroll-wrapper 并计算 #scroll-wrapper 而不是整个文档的滚动偏移量。

这是经过必要更改的代码:

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);
});
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;
}
<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>

关于javascript - 当用户在 div 背景图像上滚动时删除覆盖 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47247903/

相关文章:

javascript - 用于验证十进制数字的正则表达式

javascript - 如何在 JavaScript 上调用特定动态按钮的 onclick 事件

javascript - Bootstrap 表动态添加行到表和用户然后突出显示并选择一行

javascript - 从具有多个参数的数组中删除重复值

javascript - JQuery/Javascript 基于单选框在表格中设置输入框

jquery - 使用 jQuery 替换 HTML5 首字母缩略词标签

html - 用 css 分隔表中的 th 和 td

javascript - html 淡入子页面并更改 URL [更新]

javascript - html/css + MVC 的命名约定?

javascript - 获取 WebBrowser 控件的正确页面高度