【发布时间】:2014-01-29 16:52:55
【问题描述】:
基于这里的这个线程: Fade in element on scroll down using css
我在一个简单的测试页面上使用了已接受答案中的代码(对动画的运行方式稍作更改),并且效果很好。当我将代码移动到更复杂的页面时,它无法正常工作。我在两页之间更改的只是我想要动画元素的选择器。经过一番挖掘后,它似乎将所需元素的位置弄错了。页面上的哪些内容会影响职位的数学计算?不幸的是,我无法链接到我的页面,没有一些工作,所以如果可能的话,在我这样做之前我想要一些可能的建议。
这是我正在使用的 JQ:
jQuery(document).ready(function(){
$(window).scroll(function () {
/* Check the location of each desired element */
$('.fadein').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).delay(90 * i).animate({
'opacity': '1'
}, 600);
}
});
});
});
感谢您的帮助!
【问题讨论】:
-
您是否使用了任何可能与 jQuery
$变量冲突的 JS 插件? -
我现在有样本:简单测试:tomliv.com/so-test/scrolltest.html 问题页面:tomliv.com/so-test/about
-
@tomliv Hardcode bottom_of_window = 1426。这将一直有效,直到我们改变逻辑。
-
我们也发现了这一点 - 我们使用了 2000 - 但是页面是响应式的,因此在较窄的宽度下,由于页面高度会发生变化,因此对高度进行硬编码会失败。
标签: javascript jquery jquery-animate