jQuery悬停/隐藏/显示 - 当鼠标悬停在多个图像上时,并非所有文本都会正确消失
jQuery hover/hide/show - not all the text will disappear correctly when mousing over multiple images
我处于这种情况,我想在我的网站上显示默认文本,当将鼠标悬停在三个不同的图像上时,默认文本应该更改为三个不同的文本。
正如您在小提琴中看到的那样,我做到了这一点 - 但是如果我快速将鼠标移到所有三个图像上,那么并非所有文本都会正确消失。我如何实现这一点?
我的图像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 就可以做到这一点 - 那也没关系。
我的杰斯菲德尔: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);
}
});
});
});
演示:小提琴
我已经尝试过这样了
$("img").hover(function () {
var value = $(this).attr("id").substr($(this).attr("id").length-1);
$('div[id="default"]').find('h1').text("Content" + value);
$('div[id="default"]').find('p').text("Content of Image" + value);
});
$("img").mouseout(function(){
$('div[id="default"]').find('h1').text("Default");
$('div[id="default"]').find('p').text("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
})
现场演示
相关文章:
- 绘图悬停文本未显示
- 在 D3 中使用 Tipsy 缩小悬停文本
- 使其在悬停文本时显示Div
- 鼠标悬停文本将替换整个页面
- 数组中的工具提示/悬停文本
- 将 import.io 与鼠标悬停文本一起使用
- 选中时,悬停文本框将保持固定
- 在java中获取带有硒的悬停文本
- 悬停文本并显示一个html表格
- 在完整日历中添加事件悬停文本
- 悬停文本颜色上的 jquery UI 折叠项
- 如何从点击文本显示文件浏览器,并在悬停文本时将鼠标光标更改为指针
- 如何在D3设计上添加悬停文本
- 更改按钮上的悬停文本
- 我如何使文本改变其他图像,然后用户鼠标悬停文本
- 在NVD3.js中翻译鼠标悬停文本
- 如何在鼠标悬停文本中显示短url的目标页面的页面标题
- D3鼠标悬停文本定位
- 在jQuery中悬停文本
- HTML 画布悬停文本