我处于这种情况,我希望在我的网站上显示默认文本,当将鼠标悬停在三个不同的图像上时,默认文本应更改为三个不同的文本。
我做到了这一点,正如您在我的 fiddle 中看到的那样 - 但如果我将鼠标快速移动到所有三个图像上,那么并非所有文本都会正确消失。我如何实现这一目标?
我的图像 JavaScript 代码:
$().ready(function () {
$("#image1").hover(
function () {
$("#default").hide(timer,
function () {
$("#content1").show(timer,
function () {})
});
},
function () {
$("#content1").hide(timer, function () {
$("#default").show(timer,
function () {});
});
});
});
附言。如果仅使用 HTML 和 CSS 就可以做到这一点 - 那也没关系。
我的 jsfiddle:http://jsfiddle.net/T4BCx/4/
最佳答案
这是因为动画命令的排队性质,在您的情况下,您需要强制 jQuery 在显示/隐藏元素之前完成所有先前排队的动画。您可以使用 .stop()做这个。
而且你的解决方案看起来很复杂,我也做了一些 dom 更改以使其简单
<table>
<tr>
<td>
<img src="http://placehold.it/150x150" id="image1" alt="description" width="150px" class="content-img" data-target="#content1" />
</td>
<td>
<img src="http://placehold.it/150x150" id="image2" alt="description" width="150px" class="content-img" data-target="#content2" />
</td>
<td>
<img src="http://placehold.it/150x150" id="image3" alt="description" width="150px" class="content-img" data-target="#content3" />
</td>
</tr>
<tr>
<td colspan="3" style="height:100px">
<div id="default">
<h1>default</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
<div id="content1">
<h1>content 1</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
<div id="content2">
<h1>content 2</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
<div id="content3">
<h1>content 3</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
</td>
</tr>
</table>
然后
//initial
var timer = 200;
jQuery(function ($) {
var inside = false;
$("#content1, #content2, #content3").hide();
$(".content-img").hover(function () {
inside = true;
var $target = $($(this).data('target'));
$("#default").stop(true, true).hide(timer, function () {
if (inside) {
$target.stop(true, true).show(timer);
}
});
}, function () {
inside = false;
$($(this).data('target')).stop(true, true).hide(timer, function () {
if (!inside) {
$("#default").stop(true, true).show(timer);
}
});
});
});
演示:Fiddle
关于javascript - jQuery 悬停/隐藏/显示 - 将鼠标悬停在多个图像上时,并非所有文本都会正确消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19585679/