Fade In div when it's scrolled into viewport

后端 未结 3 2062
梦谈多话
梦谈多话 2021-02-13 09:54

Okay, so I\'ve been searching for a simple way to fade in a div when a user scrolls it into view, but I can\'t find a straight solution.

HTML

3条回答
  •  春和景丽
    2021-02-13 10:20

    Here is solution, is set on 300px

    $(document).ready(function(){
    var view = {};
    
    var checkPosition = function(){
       var elem = $('.fadethisdiv'), top = elem.offset().top;
    
                    if (top - view.limit < 300) {
                        elem.css('display','none'); 
                    }
                };
    
           $(window).bind('scroll', function() {
    
                view.top = $(window).scrollTop();
                view.limit = view.top + $(window).height();
    
                checkPosition();
            }); 
    
    });
    

    http://jsfiddle.net/kz2z5/4/

    Enjoy! :)

提交回复
热议问题