更高效的 jQuery 悬停到淡入/淡出多个 div?
我已经掌握了将鼠标悬停在图像上并在图像下方显示文本 div 的基础知识。但如何才能提高效率呢?
就目前情况而言,我必须有四个不同的函数来淡入/淡出每个图像的相应文本。
我需要保留图像的 dl 和 dt 标记,但保存文本的 div 的标记可以更改。
jQuery
$(document).ready(function()
{
$("dt.imgone").hover(
function () {
$("div.textone").fadeIn('fast');
},
function () {
$("div.textone").fadeOut('fast');
}
);
});
html
<div id="imagegallerydiv">
<dl class="gallery"><dt class="imgone"><img alt="img" src="one.jpg"></dt>
<dt>Image Title One</dt></dl>
<dl class="gallery"><dt class="imgtwo"><img alt="img" src="two.jpg"></dt>
<dt>Image Title Two</dt></dl>
<dl class="gallery"><dt class="imgthree"><img alt="img" src="three.jpg"></dt>
<dt>Image Title Three</dt></dl>
<dl class="gallery"><dt class="imgfour"><img alt="img" src="four.jpg"></dt>
<dt>Image Title Four</dt></dl>
</div>
<div id="wide-text-div-under-all-images">
<div class="textone">Lorem Ipsum One</div>
<div class="texttwo">Lorem Ipsum Two</div>
<div class="textthree">Lorem Ipsum Three</div>
<div class="textfour">Lorem Ipsum Four</div>
</div>
CSS
#imagegallerydiv {width: 700px; height:200px; margin:5px; text-align: center;}
dl.gallery {width: 97px; text-align: center; float: left;}
.gallery dt {width: 80px; margin-top:2px; font-size: .7em; text-align:center;}
#wide-text-div-under-all-images div {display: none;}
I've got the basics of hovering over an image and showing div of text below the image. But how can this be made more efficient?
As it stands, I'd have to have four different functions to fadein/out the corresponding text for each image.
I need to keep the dl and dt markup of the images, but the markup of the divs holding the text can be changed.
jQuery
$(document).ready(function()
{
$("dt.imgone").hover(
function () {
$("div.textone").fadeIn('fast');
},
function () {
$("div.textone").fadeOut('fast');
}
);
});
html
<div id="imagegallerydiv">
<dl class="gallery"><dt class="imgone"><img alt="img" src="one.jpg"></dt>
<dt>Image Title One</dt></dl>
<dl class="gallery"><dt class="imgtwo"><img alt="img" src="two.jpg"></dt>
<dt>Image Title Two</dt></dl>
<dl class="gallery"><dt class="imgthree"><img alt="img" src="three.jpg"></dt>
<dt>Image Title Three</dt></dl>
<dl class="gallery"><dt class="imgfour"><img alt="img" src="four.jpg"></dt>
<dt>Image Title Four</dt></dl>
</div>
<div id="wide-text-div-under-all-images">
<div class="textone">Lorem Ipsum One</div>
<div class="texttwo">Lorem Ipsum Two</div>
<div class="textthree">Lorem Ipsum Three</div>
<div class="textfour">Lorem Ipsum Four</div>
</div>
CSS
#imagegallerydiv {width: 700px; height:200px; margin:5px; text-align: center;}
dl.gallery {width: 97px; text-align: center; float: left;}
.gallery dt {width: 80px; margin-top:2px; font-size: .7em; text-align:center;}
#wide-text-div-under-all-images div {display: none;}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以这样做(没有测试过,但它应该可以工作)
编辑
idx =
部分You can do it this way (haven't tested it but it should work)
EDITED the
idx =
part怎么样:
jsfiddle 上的代码示例。
另一种选择是使用 < code>delegate() 因此您不会直接将一堆事件处理程序绑定到
dt
。delegate() 示例
How about:
Code example on jsfiddle.
Another option would be to use
delegate()
so you are not binding a bunch of event handlers to thedt
directly.Example of
delegate()