マウスオーバーの2つのフレーム間で時間遅延を設定します
-
30-09-2019 - |
質問
シングル用に2つの画像を表示する必要があります mouseover
. 。だから私 mouseover
画像には、最初に、5000の時間遅延で画像が表示され、同じホバーに表示するには画像が必要です。今 mouseout
元の画像を表示します。
私はJavaScriptとJQueryにそれほど精通していません。
誰かがこれを行う方法についていくつかのアイデアを教えてください。
私がしたことは、
$('.image1').mouseover(function() {
setInterval($(this).removeClass(.image1).addClass('image-over1'),5000);
$(this).removeClass(.image1).addClass('image-over2');
});
$('.image1').mouseout(function() {
$(this).removeClass('image-over1');
$(this).removeClass('image-over2').addClass(item);
});
$('.image1').click(function(){
document.location='index.php?page='index.php';
})
解決
.hover()
関数では、両方のマウスオーバー/マウスアウトを同時に指定でき、機能を作成する必要があります。 setInterval
:
$('.image1').hover(function(evt) {
// mouse over function.
// DOM Element that got the mouseover.
var target = evt.target;
if (target.timer) {
clearTimeout(target.timer);
target.timer = null;
}
target.timer = setInterval(function() {
// $(this) will not work here, since 'this' has changed.
// depending on your css you shouldn't need to remove the '.image1'
// class, just make sure .image-over1 and .image-over2 are
// stronger selectors, or occur after .image1
$('.image1').addClass('image-over2');
// at this point your element will be (just guessing <img>, could be
// anything really:
// <img class="image1 image-over1 image-over2" .../>
// it's absolutely fine for the image to have all those classes as
// long as your css is correct.
}, 5000);
$('.image1').addClass('image-over1');
}, function(evt) {
// mouse out function.
// DOM Element that got the mouseout.
var target = evt.target;
if (target.timer) {
clearTimeout(target.timer);
target.timer = null;
}
$('.image1').removeClass('image-over1');
$('.image1').removeClass('image-over2');
});
$('.image1').click(function(){ document.location='index.php?page='index.php'; })
他のヒント
まず第一に、あなたのアプローチには問題があると思います。マウスオーバーの要素から「Image1」クラスを削除すると、その要素はマウスアウトの$( "。Yimage1")セレクターによって一致しません。削除する必要がある理由はありますか?あなたがする場合(つまり、あなたが無効にする必要があるCSSのクラスに何かが定義されている場合)、あなたが一致させることができる他のいくつかのセレクターはありますか?
時間遅延に関しては、1.4を超えるjQueryバージョンを使用している場合、.delay()関数を使用できます。
$('.image1').mouseover(function() {
$(this).addClass('image-over1').delay(5000).addClass('image-over2');
});
おそらくあなたはこれらの画像のアニメーションGIFを作成するのですか?次に、ここに似たコードを使用します。 http://www.netmechanic.com/news/vol3/design_no10.htm
画像がフライで生成されたとしても、PHPでアニメーションGIFをプログラム的に生成することが可能です - 参照 http://php.net/manual/en/function.imagegif.php