2013-05-30 24 views
1

基本上,我在这里要做的是在用户滚动到页面底部时逐渐淡入3格。使用航点逐个淡入多个div(包括图像)

我在网上搜索,发现这可以通过使用一个名为waypoints的jquery插件来完成。然而,我在Jquery方面是全新的,我真的无法弄清楚如何做到这一点。

如果有人可以看看我的代码,并告诉我我做错了什么,这将是非常有益的!

下面是HTML:

<!--#Div1--> 
    <div class="span4" id="fadein1" style="display:none"> 
    <img src="images/41.jpg" alt="41%" style="margin-right: 10px;" id="41"> 
     <h2>Boba</h2> 
     <p>We strive for <b>perfection</b> when we prepare our boba 
to use. Not under, nor overcooked to mush. Just right and still chewy.</p> 

    </div><!-- /.span4 --> 
     <div class="span4" id="fadein2" style= "display:none;"> 
     <img src="images/43.jpg" alt="43%" style="" id="43"> 
      <h2>Tea Leaves</h2> 
      <p>Tea Powders? <b>NEVER!</b> Premixes? <b>Absolutely not!</b> 
We use only the best quality house blend tea leaves.</p> 

    </div><!-- /.span4 -->  

    <div class="span4" id="fadein3" style="display:none;"> 
      <img src="images/44.jpg" alt="44%" style="" id="44"> 
      <h2>Customized Blend</h2> 
      <p>It is our goal to customize your drink the way 
<b>you</b> want it to be. 75% sugar? Light ice? You name 
it, and we will make it happen!</p> 

    </div><!-- /.span4 --> 

这里是JS

$(document).ready(function() { 

    // call waypoint plugin 
    $("#fadin1").waypoint(function(bottom-in-view) { 
     $(this).fadein(1500); 
    }, { 
     offset: 'bottom-in-view' 
    } 

    $("#fadin2").waypoint(function(bottom-in-view) { 
     $(this).fadein(2000); 
    }, { 
     offset: 'bottom-in-view' 
    } 

    $("#fadin3").waypoint(function(bottom-in-view) { 
     $(this).fadein(2500); 
    }, { 
     offset: 'bottom-in-view' 
    } 
); 

回答

0

试试这个脚本。滚动到达div时你的div将会淡入淡出。

我想你应该有一些内容的顶部和底部这些隐藏的div。

$(document).ready(function() { 
$('#fadein1').waypoint(function(event, direction) { 
    $(this).fadeIn(1500); 
}, { 
    offset: function() { 
     return -$(this).height(); 
    } 
}); 

$('#fadein2').waypoint(function(event, direction) { 
    $(this).fadeIn(2000); 
}, { 
    offset: function() { 
     return -$(this).height(); 
    } 
}); 

$('#fadein3').waypoint(function(event, direction) { 
    $(this).fadeIn(2500); 
}, { 
    offset: function() { 
     return -$(this).height(); 
    } 
}); 
}); 

HTML应该是这样......

<div>Top content 1</div> 
<div>Top content 1</div> 

<div>Your hidden div 1</div> 
<div>Your hidden div 2</div> 
<div>Your hidden div 3</div> 

<div>Below content 1</div> 
+0

谢谢!它完美地工作,就像我想要的一样。一个问题是,我不得不提出一个“方向”和“事件”的论点?在这个例子中,我不应该在方向部分输入“down”吗?它是如何工作的? –

+0

很高兴它的作品。 :) 你可以使用没有这些参数,或使用它们,如果你想添加方向或处理程序事件。 了解详细信息,请点击此处... http://imakewebthings.com/jquery-waypoints/#get-started – yeyene

+0

Waypoint 2.x中不再使用'event'参数。这是一个旧的1.x的东西。航点回调现在只接收'方向'参数。我知道这个答案不使用这些参数,只是让你知道。 :) – imakewebthings