简体   繁体   English

淡出页面顶部部分,淡入页面底部

[英]Fade out section on page top, fade in on bottom

I got this codepen: https://codepen.io/tobiasglaus/pen/NpVXRR 我得到了这个Codepen: https ://codepen.io/tobiasglaus/pen/NpVXRR
When I scroll the first section fades out and the second one fades in from the bottom with this code: 当我滚动时,使用此代码,第一个部分淡出,第二个部分从底部淡入:

$(window).scroll(function(){
    $(".top").css("opacity", 1 - $(window).scrollTop() / 400);
  });
$(window).scroll(function(){
    $(".bottom").css("opacity", 0 + $(window).scrollTop() / 400);
  });

But that's just a scroll event and only working with 2 sections. 但这只是一个滚动事件,只能使用2个部分。 Is there a way that I can add more sections and they just fade out, when they reach the top and fade in from the bottom? 有没有一种方法可以添加更多部分,当它们到达顶部并从底部逐渐消失时,它们就会淡出?

As I understand you want something like this: when an element comes into the viewport area, it's faded in and when it comes out of the viewport area, it should be faded out. 据我了解,您需要这样的东西:当一个元素进入视口区域时,它会淡入,而当它离开视口区域时,则应淡出。

So all the thing should be done in the onscroll event of the window. 因此,所有操作都应在窗口的onscroll事件中完成。 To know if the element is in and out of the viewport area we need to know about its top and bottom (which can be calculated from its top and its height ), we also need to know about the viewport's height. 要知道该元素是否在视口区域之内和之外,我们需要了解其topbottom (可以通过其topheight来计算),我们还需要了解视口的高度。 That's all we need to find out if an element is in or out the viewport area. 这就是我们需要确定元素是否在视口区域中或视口区域之外的所有内容。 The following is the detailed code. 以下是详细的代码。 NOTE: I don't include the complexity of getting the viewport's height here for simplicity (I just use document.documentElement.clientHeight - which should work in the latest versions of all modern browsers today). 注意:为简单起见,这里不包括获取视口高度的复杂性(我只使用document.documentElement.clientHeight应该可以在当今所有现代浏览器的最新版本中使用)。

HTML : HTML

<div class="fading"></div>
<div class="fading"></div>
<div class="fading"></div>
<div class="fading"></div>

CSS : CSS

.fading {
  border:1px solid red;
  margin-bottom:10px;
  height:500px;
  background:green;
}

JS : JS

var fadings = $(".fading");
$(window).scroll(function(){
  //the viewport's height
  var vpheight = document.documentElement.clientHeight;
  //loop through all interested elements
  fadings.each(function(){
    //get the rect of the current element
    var r = this.getBoundingClientRect();
    //the current element's height  
    var thisHeight = $(this).height();
    //check if the element is completely out of the viewport area
    //to just ignore it (save some computation)
    if(thisHeight + r.top < 0 || r.top > vpheight) return true;
    //calculate the opacity for partially visible/hidden element
    var opacity = Math.max(0, Math.min(1, 
                  (r.top >= 0 ? vpheight - r.top : thisHeight - Math.abs(r.top)) / vpheight));
    //set the opacity
    $(this).css("opacity", opacity);
  });           
});

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM