我在页面下方有一个 div 相当远的部分。我希望通过滚动到达 div 时显示它。当达到 div 时(大约 30% 通过 div)有没有办法让它淡入淡出?
我确实希望这是有道理的:s
感谢您的关注。
这是一种方法。尽管您也可以尝试许多通用的 scrollspy 插件。(但是引导程序对我不起作用,所以我做了这个。)
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/