0 html javascript jquery animation
我试图让这个jQuery动画计数器在用户向下滚动超过200个像素时立即触发:
来自这里的原始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));
}
});
});
Run Code Online (Sandbox Code Playgroud)
我试图将它包装在下面的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));
}
});
});
}
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<span class="Count">100</span>
<br/>
<span class="Count">200</span>
<br/>
<span class="Count">300</span>
Run Code Online (Sandbox Code Playgroud)
来自另一篇文章的小提琴就在这里
一旦用户滚动到视图或页面下200像素,触发jQuery计数器的最佳方法是什么?我也尝试了jQuery Wayfinder,但没有任何运气让它工作.
$(window).off("scroll")在触发动画之前取消绑定滚动处理程序(with ),以防止动画在用户继续滚动时重新启动.
$(window).scroll(startCounter);
function startCounter() {
if ($(window).scrollTop() > 200) {
$(window).off("scroll", startCounter);
$('.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));
}
});
});
}
}Run Code Online (Sandbox Code Playgroud)
body {
font-family: sans-serif;
height: 300vh;
}
.Count {
position: fixed;
top: 8px;
left: 8px;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="Count">100</div>Run Code Online (Sandbox Code Playgroud)
吉利的答案很好,但它缺少让它从0开始并获得一定值的部分;
这是你可以做的:
var stop = $("#someElement").offset().top;
$(window).scroll(function() {
if ($(window).scrollTop() > stop ) {
$(window).off("scroll");
$('.Count').each(function () {
var $this = $(this);
jQuery({ Counter: 0 }).animate({ Counter: $this.attr("data") }, {
duration: 1000,
easing: 'swing',
step: function () {
$this.text(Math.ceil(this.Counter));
}
});
});
}
});Run Code Online (Sandbox Code Playgroud)
body {
font-family: sans-serif;
height: 300vh;
}
.Count {
position: fixed;
top: 8px;
left: 8px;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="Count" data=200>0</div>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24150 次 |
| 最近记录: |