【问题标题】:Remove overlay div when user scrolls on div background image当用户在 div 背景图像上滚动时删除覆盖 div
【发布时间】:2017-11-12 10:50:03
【问题描述】:

这里是 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>

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    如果我正确理解了您想要什么,那么您已经差不多了。您只需要将滚动处理程序绑定到 #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>

    【讨论】:

      猜你喜欢
      • 2016-01-14
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 2013-03-25
      • 2012-09-28
      • 1970-01-01
      • 2019-04-17
      • 2018-03-17
      相关资源
      最近更新 更多