【发布时间】:2016-07-12 04:40:18
【问题描述】:
我试图让这个 jQuery 动画计数器在用户向下滚动页面超过 200 像素时触发:
来自here的原始jQuery代码
$('.Count').each(function () {
var $this = $(this);
jQuery({ Counter: 0 }).animate({ Counter: $this.text() }, {
duration: 1000,
easing: 'swing',
step: function () {
$this.text(Math.ceil(this.Counter));
}
});
});
我尝试将它包装在下面的 jQuery 中,但它直到最后才触发动画:
$(window).scroll(function() {
if ($(window).scrollTop() > 200) {
$('.Count').each(function () {
var $this = $(this);
jQuery({ Counter: 0 }).animate({ Counter: $this.text() }, {
duration: 1000,
easing: 'swing',
step: function () {
$this.text(Math.ceil(this.Counter));
}
});
});
}
});
HTML:
<span class="Count">100</span>
<br/>
<span class="Count">200</span>
<br/>
<span class="Count">300</span>
另一个帖子的小提琴是here
当用户滚动到视图或页面向下 200 像素时触发 jQuery 计数器的最佳方式是什么?我也尝试过 jQuery Wayfinder,但没有成功。
【问题讨论】:
-
为我工作:jsfiddle.net/Yy6r6/420“直到最后才触发动画”是什么意思?什么结局?
-
它不起作用@gilly3 - 向下滚动并且数字只是在 1 和 0 之间滑动 - 我希望它们在用户向下滚动时从 0 开始计数(例如向下滚动 200 像素)
-
如果在动画开始后继续滚动,动画会重新开始。动画从 div 中的当前值获取结束值。由于动画目前正在进行中,因此数量会更低。这就是为什么您看到它在 1 和 0 之间闪烁的原因。您在动画开始后不久重新启动了动画,并且只达到了 1。有很多方法可以解决这个问题。您希望动画运行多少次?就一次?每次?
-
只要用户向下滚动 200 像素,就希望动画运行一次 @gilly3,以便他们看到正在动画的数字
标签: javascript jquery html animation