开发者

jQuery .hover() seems to "disappear" after a while

开发者 https://www.devze.com 2023-03-10 19:35 出处:网络
$(\'img.questSign\').hover( function() { $(\'img.questSign\').attr(\'src\', \'/media/quest-sign-small-hover.png\');
$('img.questSign').hover( function() { 
    $('img.questSign').attr('src', '/media/quest-sign-small-hover.png');
},
function(){
    $('img.questSign').attr('src', '/media/quest-sign-small.png');
});

This code works quite awesome, when the page has been loaded freshly. But after a while it seems to be stop working. The picture does not change on hover.

ADDITION:

I ran a long-term test with all major browsers and it seems that this very special problem only appears 开发者_StackOverflow中文版in the new Firefox 5 beta. Problem fixed with new Firefox Update


Try this:

$('img.questSign').hover(function() { 
    $(this).attr('src', '/media/quest-sign-small-hover.png');
}, function() {
    $(this).attr('src', '/media/quest-sign-small.png');
});

Your old code would misbehave with multiple img.questSigns on the page.


You may want to use a timeout like this:

$('img.questSign').hover(function () {
    clearTimeout($(this).data('timeout'));
    $(this).attr('src', '/media/quest-sign-small-hover.png');
}, function () {
    var e = $(this).data('timeout', setTimeout(function () {
        e.attr('src', '/media/quest-sign-small.png');
    }, 3000)); // 3 sec
});
0

精彩评论

暂无评论...
验证码 换一张
取 消