【发布时间】:2017-06-12 23:27:18
【问题描述】:
我有一个产品图片列表,每个产品都有两张不同的图片。第一个在未悬停在图像上时显示,第二个在悬停在图像上时显示。
问题是,如果我将鼠标悬停在一个产品图像上并快速移动到另一个产品图像,那么第一个产品图像会得到第二个产品的第一个图像,所以产品图像是错误的。
代码如下所示:
HTML:
<div id="listing">
<ul>
<li data-artnr="3212" class="product">
<div class="image">
<a href="/">
<img src="/pathTo/image/3212.jpg" class="loaded">
</a>
</div>
</li>
<li data-artnr="3213" class="product">
<div class="image">
<a href="/">
<img src="/pathTo/image/3213.jpg" class="loaded">
</a>
</div>
</li>
<li data-artnr="3214" class="product">
<div class="image">
<a href="/">
<img src="/pathTo/image/3214.jpg" class="loaded">
</a>
</div>
</li>
</ul>
</div>
jQuery:
$("#listingContent").find(".product").hover(function () {
$product = $(this);
$image = $(this).find(".loaded");
var artnr = $product.data("artnr");
window.oldImage = $image.attr("src");
var newImage = "/pathTo/image/"+artnr+"_topoffer.jpg";
$product.data("old", oldImage);
$image.fadeOut(150, function () {
$(this).load(function () {
$(this).fadeIn(150);
}).attr("src", newImage);
});
},
function () {
$image.fadeOut(150, function () {
$(this).load(function () {
$(this).fadeIn(150);
}).attr("src", oldImage);
});
});
我猜这与淡入/淡出有关,因此 $(this) 在淡入开始之前就已经改变了。 有没有更好的方法来避免这个问题?当我删除淡入/淡出时问题不会发生,但这是我想要的,因为没有它传输会很困难。
【问题讨论】:
-
您好,如果您可以为问题创建小提琴,那将是一个很大的帮助,这样我们更容易解决它。谢谢:)
标签: jquery hover jquery-hover