首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当用户在div背景图像上滚动时移除重叠div

当用户在div背景图像上滚动时移除重叠div
EN

Stack Overflow用户
提问于 2017-11-12 10:50:03
回答 1查看 460关注 0票数 0

Javascript在此表示非常感谢您的任何帮助!

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

只有当用户在body元素上滚动时,我才设法使.scroll-overlay容器的不透明度下降到0。

如何专门针对#laptop-content容器?

代码语言:javascript
复制
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);
});
代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<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>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-12 10:58:21

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

这是您的代码,并进行了必要的修改:

代码语言:javascript
复制
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);
});
代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47247903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档