如何在元素可见时淡入元素



当我滚动到它时,我正在使用我发现的这段代码淡入元素(带有 class:hideme),但它只有在完全可见时才淡入,有没有办法我可以调整代码,让它在浏览器中出现后立即淡入?

我尝试将这一行中的">"更改为"=",它有点工作:

if( bottom_of_window > bottom_of_object ){

但是当我应用于第二个元素时它不起作用

谢谢!

原始链接:http://jsfiddle.net/tcloninger/e5qaD/

代码如下:

$(document).ready(function() {
    /* Every time the window is scrolled ... */
    $(window).scroll( function(){
        /* Check the location of each desired element */
        $('.hideme').each( function(i){
            var bottom_of_object = $(this).position().top + $(this).outerHeight();
            var bottom_of_window = $(window).scrollTop() + $(window).height();
            /* If the object is completely visible in the window, fade it it */
            if( bottom_of_window > bottom_of_object ){
                $(this).animate({'opacity':'1'},500);
            }
        }); 
    });
});

你只需要检查对象的顶部

        var top_of_object = $(this).position().top;
        var bottom_of_window = $(window).scrollTop() + $(window).height();
        /* If the top of the object is visible in the window, fade it in */
        if( bottom_of_window >= top_of_object ){
            $(this).animate({'opacity':'1'},500);
        }

在此处更新了 JSFiddle:http://jsfiddle.net/e5qaD/2727/

最新更新