简体   繁体   English

如何在向下滚动后淡入DIV,然后在底部淡出

[英]How to Fade in a DIV After Scrolling Down, then Fade it Out at the Bottom

I have a div that I want to fade in after the user scrolls 600px down the page. 我有一个div,我想在用户向下滚动页面600px后淡入。 I have achieved this easily enough using the code below: 我使用下面的代码很容易实现这一点:

<script>
$(window).scroll(function(){
if($(window).scrollTop()>600){
$("#fade-in-area").fadeIn();
}else{
$("#fade-in-area").fadeOut();
}
});
</script>

I want the same div to fade out about 600 pixels from the bottom of the page. 我希望相同的div可以从页面底部淡出约600个像素。 I have seen a couple other people trying to do this, but can't figure out how to have it both fade in, and fade out. 我已经看到其他几个人试图这样做,但无法弄清楚如何让它既淡入淡出又淡出。

It should be pretty easy for a regular Javascript programmer I'd think. 对于我认为常规的Javascript程序员来说应该很容易。

Can anyone help me out? 谁能帮我吗?

Try this code: 试试这段代码:

<script>
   $(window).scroll(function(){
       var leftToBottom = $(document).height() - $(window).height() - $(window).scrollTop();
       var distanceFromTop = $(window).scrollTop();
       if( distanceFromTop > 600 && !$("#fade-in-area").is(":visible") 
          && leftToBottom > 600) {
            $("#fade-in-area").fadeIn();
       }else if($("#fade-in-area").is(":visible") && (distanceFromTop < 600 || leftToBottom < 600)){
            $("#fade-in-area").fadeOut();
      }
   });
</script>

It should fadeOut a div if there is less than 600 px left to scroll bottom. 如果剩下少于600像素滚动底部,它应该淡出一个div。

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

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