div をアニメーション化する無限ループを使用して jQuery 関数を実装しようとしています。私はそれを行う方法を理解できません。これは私のコードです:
$(document).ready(function () {
$('#divers').animate({
'margin-top': '90px'
}, 6000).animate({
'margin-top': '40px'
}, 6000);
});
div をアニメーション化する無限ループを使用して jQuery 関数を実装しようとしています。私はそれを行う方法を理解できません。これは私のコードです:
$(document).ready(function () {
$('#divers').animate({
'margin-top': '90px'
}, 6000).animate({
'margin-top': '40px'
}, 6000);
});
完全なアニメーションを実行するコードを関数に入れ、その関数をコールバック パラメータとして最後のアニメーションに渡します。何かのようなもの...
$(document).ready(function() {
function animateDivers() {
$('#divers').animate(
{'margin-top':'90px'}
,6000
)
.animate(
{'margin-top':'40px'}
,6000
,animateDivers //callback the function, to restart animation cycle
);
}
animateDivers(); //call, to start the animation
});
$(document).ready(function() {
function ani() {
$('#divers').animate({
'margin-top':'90px'
},6000).animate({
'margin-top':'40px'
},6000, ani); //call the function again in the callback
});
});
ani();
});
コールバックを使用し.animate()
て関数を「リコール」します。
$(function() {
function loop(){
$('#divers')
.animate({marginTop:90},6000)
.animate({marginTop:40},6000, loop); // callback
}
loop(); // call this wherever you want
});
setInterval を使用する方法です。再帰が多すぎると、「スタック オーバーフロー」が発生します :-) 「Uncaught RangeError: 最大コール スタック サイズを超えました」
どの間隔でどのメソッドを呼び出すかを指定するsetinterval関数を設定することもできます
$(function () { setInterval(fnName, 6000); });
このanimate()
関数には、アニメーションの終了時に呼び出す関数を受け取るオプションがあります。同じ電話をかけるだけで、できあがりです。