到达时显示Div - Jquery

时间:2013-08-15 15:37:57

标签: jquery html scroll show fade

我在页面上有一个很大的部分。我希望div在滚动到达时出现。达到div时(通过div大约30%)有没有办法让它淡入呢?

我希望这是有道理的:s

感谢您的光临。

1 个答案:

答案 0 :(得分:0)

这是一种方法。虽然也有很多通用的scrollspy插件你可以尝试。 (但是bootstrap一个对我不起作用,所以我做了这个。)

function scrollfadein(selector, offset, speed)
{
    if (speed == undefined)
    {
        speed = 1500;
    }

    $(selector)

    /* hide elements initially */
    .css('opacity', 0)

    /* each element gets its own callback so it can be removed after it's triggered */
    .each(function(){ 
        var el = $(this);
        var fun = function()
        {
            /* calculate offset if it's a fraction multiply by element height */
            offset = (offset == undefined) ? 0 : (offset < 1 ? offset*el.height() : offset);

            /* height and offset might all change if window is resized, so 
               always use up-todate values. */
            if ( $(window).scrollTop() > el.offset().top - $(window).height() + offset )
            {
                /* fadein element */
                el.animate({'opacity':1}, speed);

                /* remove callback now that it has triggered */
                $(window).off('scroll', fun);
            }
        };
        $(window).on('scroll', fun);
    });
}

示例:http://jsfiddle.net/eg4HS/