简体   繁体   English

滚动页面时淡入和淡出div

[英]Fade-in and Fade-out divs while scrolling page

I've been playing with this all afternoon, both using the waypoints plugin and not. 我整个下午都在玩,无论是使用Waypoint插件还是不使用。 I'm trying to fade in elements as they come into view and fade them out when they come out of view (ideally the element would have an opacity of 1 in the middle of the viewport, an opacity of 0 at the edges, and fade in both directions) 我试图在元素出现时淡入并在它们消失时淡出(理想情况下,元素在视口中间的不透明度为1,边缘的不透明度为0,然后逐渐淡入双向)

This code works find for fading in elements as they come onto the screen, but I can't get them to fade back out again no matter what permutations I try 这段代码可以找到在屏幕上出现的淡入淡出的元素,但是无论我尝试哪种排列,我都无法让它们再次淡出

faders = $(".fades").fadeTo(0,0);

$(window).scroll(function(d,h) {
    faders.each(function(i) {
        a = $(this).offset().top + $(this).height();
        b = $(window).scrollTop() + $(window).height();
        if (a < b) $(this).fadeTo(100,2);
    });
});

try this, hope it helps 试试这个,希望对您有所帮助

            $(document).ready(function(){
                faders = $(".fades").fadeTo(0,0);
                $(window).scroll(function(){
                    faders.each(function(){
                        a = $(this).offset().top + $(this).height();
                        b = $(window).scrollTop() + ($(window).height());
                        c = $(window).scrollTop() + $(this).height();
                        if (c > $(this).offset().top){
                            $(this).fadeTo(0,0.5);
                        }
                        else if (a < b) {
                            $(this).fadeTo(0,1);
                        }
                        else { 
                            $(this).fadeTo(0,0.5);
                        }
                    });
                });
            });

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

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