それが動作してjQuery()
への呼び出しで渡されたオブジェクト{ counter: 0 }
だろうstep
で私にいくつかのオブジェクト
this
を与えるものではありません。
selector
または$(this)
を.animate()
のセレクタとして使用できます。 now
パラメータをstep
に設定してcounter
の値を.text()
に設定する機能があります。また、fx
オブジェクト(step
)にはthis
という要素があり、elem
というプロパティがあります。 .animate()
の外で定義されたオブジェクトを使用して、最初に設定するパラメータを渡して、オブジェクトの値を使用してアニメーションプロパティをリセットすることができます。
$(this).text()
の前に+
オペレータを追加してcounter
の値をNumber
に設定します。
this
(step
)は、.each()
の繰り返しで現在の要素にする必要がありますが、期待される効果はありません。 js
に同様のパターンを使用して
var props = {from:0, to:100};
$(".animateNumber").each(function() {
$(this).animate({
counter: props.to
}, {
step: function(now, fx) {
console.log(this, fx.elem)
$(this).text(Math.ceil(now));
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<div class="animateNumber">0</div>
<div class="animateNumber">0</div>
<div class="animateNumber">0</div>
質問で、あなたはstep
機能でthis.elem
、this.counter
を参照し、.animate()
に渡されたオブジェクトのプロパティelem
としてselector
を設定することができます。
$(".animateNumber").each(function() {
var selector = $(this);
jQuery({
elem: selector,
counter: 0
}).animate({
counter: +$(this).text()
}, {
step: function() {
// problem: the $(this) not working - if change to `selector` working
this.elem.text(Math.ceil(this.counter));
console.log(this)
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<div class="animateNumber">1</div>
<div class="animateNumber">2</div>
<div class="animateNumber">3</div>
だけでありません。あなたは 'セレクタ'に必要なオブジェクトへの参照を持っています、なぜあなたは '$(domNode)'を何度も何度も呼び出すと主張しますか? **セレクタを使う!** – Thomas