简体   繁体   English

jQuery“淡出”效果向上滚动

[英]JQuery “fade out” effect on scroll up

I have this code, if scroll down Fade in and show the object 我有这段代码,如果向下滚动淡入并显示对象

css: CSS:

.fadeInBlock {
    opacity:0;    
}

js: js:

$(function() {
    $(window).scroll( function(){
        $('.fadeInBlock').each( function(i){            
            var bottom_of_object = $(this).position().top + $(this).outerHeight();
            var bottom_of_window = $(window).scrollTop() + $(window).height();
            /* Adjust the "200" to either have a delay or that the content starts fading a bit before you reach it  */
            bottom_of_window = bottom_of_window + 200;
            if( bottom_of_window > bottom_of_object ){
                $(this).animate({'opacity':'1'},500);
            }
        }); 

    });
});

and need solution to fade out the object after scroll up. 需要向上滚动后淡出对象的解决方案。

Please advise, thank you 请指教,谢谢

Maybe something like this will help 也许这样的事情会有所帮助

function isVisible(elem){
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}

$(window).scroll(function() {
  console.log('scroll')
  $('.fadeInBlock').each(function(i) {
    if (isVisible(this)) {
      $(this).animate({
        'opacity': '1'
      }, 1000);
    } else {
        $(this).css({
        'opacity': '0'
      });
    }
  });
});

https://jsfiddle.net/32feyrhy/4/ https://jsfiddle.net/32feyrhy/4/


$(window).on("load",function() {
  $(window).scroll(function() {
    $(".fade").each(function() {
      /* Check the location of each desired element */
      var objectBottom = $(this).offset().top + $(this).outerHeight();
      var windowBottom = $(window).scrollTop() + $(window).innerHeight();

      /* If the element is completely within bounds of the window, fade it in */
      if (objectBottom < windowBottom) { //object comes into view (scrolling down)
        if ($(this).css("opacity")==0) {$(this).fadeTo(500,1);}
      } else { //object goes out of view (scrolling up)
        if ($(this).css("opacity")==1) {$(this).fadeTo(500,0);}
      }
    });
  }); $(window).scroll(); //invoke scroll-handler on page-load
});

.fade {
  margin: 50px;
  padding: 50px;
  background-color: lightgreen;
  opacity: 1;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<div>
  <div class="fade">Fade In 01</div>
  <div class="fade">Fade In 02</div>
  <div class="fade">Fade In 03</div>
  <div class="fade">Fade In 04</div>
  <div class="fade">Fade In 05</div>
  <div class="fade">Fade In 06</div>
  <div class="fade">Fade In 07</div>
  <div class="fade">Fade In 08</div>
  <div class="fade">Fade In 09</div>
  <div class="fade">Fade In 10</div>
</div>

Please check this link:- Fade In on Scroll Down, Fade Out on Scroll Up - based on element position in window 请检查此链接:- 向下滚动淡入,向上滚动淡出-基于窗口中元素的位置

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

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