【发布时间】:2016-04-05 11:01:12
【问题描述】:
在我的项目中,我想淡入 html 中的 div,我正在使用以下代码
$(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).offset().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);
}
});
});
});
#container {
height:2000px;
}
#container div {
margin:50px;
padding:50px;
background-color:lightgreen;
}
.hideme {
opacity:0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdn.jsdelivr.net/g/jquery.fullpage@2.5.9(jquery.fullPage.min.js+vendors/jquery.easings.min.js+vendors/jquery.slimscroll.min.js)"></script>
<link href="https://cdn.jsdelivr.net/jquery.fullpage/2.5.9/jquery.fullPage.min.css" rel="stylesheet" />
<div id="container">
<div>Hello</div>
<div>Hello</div>
<div>Hello</div>
<div>Hello</div>
<div>Hello</div>
<div>Hello</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
<div class="hideme">Fade In</div>
</div>
可以在JS Fiddle找到 在项目中,我还使用了 javascript 代码
$(document).ready(function() {
$('#fullpage').fullpage();
});
这基本上使滚动更好,详情https://github.com/alvarotrigo/fullPage.js/
问题:由于全页代码淡入功能没有进入scroll if条件。
【问题讨论】:
-
你的 javascript 中有两个 $(document).ready() 用于同一页面吗?
-
@Glubus 没关系。
-
也许你应该使用
$(this).fadeIn(); -
@PraveenKumar 你说得对,虽然我不确定,但事实并非如此。
-
@AnasSaeed 类似stackoverflow.com/questions/33327255/…
标签: javascript jquery html fullpage.js