マウスがオブジェクトの上にあるときにのみアニメーションを実行しようとしています。アニメーションを1回繰り返して、マウスアウトで通常の状態に戻すことができます。ただし、アニメーションをマウスオーバーでループさせたいのですが。setIntervalを使用して、どのように実行しますか?私は少し立ち往生しています。
17868 次
4 に答える
9
これは次のように実行できます。
$.fn.loopingAnimation = function(props, dur, eas)
{
if (this.data('loop') == true)
{
this.animate( props, dur, eas, function() {
if( $(this).data('loop') == true ) $(this).loopingAnimation(props, dur, eas);
});
}
return this; // Don't break the chain
}
今、あなたはこれを行うことができます:
$("div.animate").hover(function(){
$(this).data('loop', true).stop().loopingAnimation({ left: "+10px"}, 300);
}, function(){
$(this).data('loop', false);
// Now our animation will stop after fully completing its last cycle
});
アニメーションをすぐに停止したい場合は、次のようにhoverOut
行を変更できます。
$(this).data('loop', false).stop();
于 2010-01-11T03:37:14.610 に答える
4
setInterval
clearInterval
タイマーを無効にするために渡すことができるIDを返します。
次のように書くことができます。
var timerId;
$(something).hover(
function() {
timerId = setInterval(function() { ... }, 100);
},
function() { clearInterval(timerId); }
);
于 2010-01-11T03:37:47.640 に答える
4
ページ上の複数のオブジェクトで機能するためにこれが必要だったので、Cletusのコードを少し変更しました。
var over = false;
$(function() {
$("#hovered-item").hover(function() {
$(this).css("position", "relative");
over = true;
swinger = this;
grow_anim();
}, function() {
over = false;
});
});
function grow_anim() {
if (over) {
$(swinger).animate({left: "5px"}, 200, 'linear', shrink_anim);
}
}
function shrink_anim() {
$(swinger).animate({left: "0"}, 200, 'linear', grow_anim);
}
于 2011-05-04T11:58:48.693 に答える
1
検討:
<div id="anim">This is a test</div>
と:
#anim { padding: 15px; background: yellow; }
と:
var over = false;
$(function() {
$("#anim").hover(function() {
over = true;
grow_anim();
}, function() {
over = false;
});
});
function grow_anim() {
if (over) {
$("#anim").animate({paddingLeft: "100px"}, 1000, shrink_anim);
}
}
function shrink_anim() {
$("#anim").animate({paddingLeft: "15px"}, 1000, grow_anim);
}
これはタイマーを使用して実現することもできます。
于 2010-01-11T03:42:46.843 に答える