如何使用 jQuery 获取三个图像以在悬停时为不同的图像设置动画。包含代码
您看到下面我的代码有问题吗?我正在尝试获取三个不同的图像,以便在悬停时调用不同的图像...它在一个图像上工作得很好,但添加额外的东西就会出现问题...我已经尝试了许多类和 ID 的变体,但我陷入困境这点非常感谢任何帮助。
CSS
.menu2 {
padding: 0;
list-style: none;
}
.menu2 li {
float: left;
position: relative;
}
.menu2, .1 a {
display: block;
width: 218px;
height:181px;
background: url(images/btn_capture_off.png) no-repeat center center;
}
.menu2, .1 a:hover {
display: block;
width: 218px;
height:181px;
background: url(images/btn_capture_on.png) no-repeat center center;
}
.menu2, .1 li em {
background: url(images/btn_txt_capture.png) no-repeat;
width: 218px;
height: 88px;
position: absolute;
top:200px;
left: 0;
z-index: 2;
display: none;
}
.menu2, .2 a {
display: block;
width: 218px;
height:181px;
background: url(images/btn_manage_off.png) no-repeat center center;
}
.menu2, .2 a:hover {
display: block;
width: 218px;
height:181px;
background: url(images/btn_manage_on.png) no-repeat center center;
}
.menu2, .2 a li em {
background: url(images/btn_txt_manage.png) no-repeat;
width: 218px;
height: 88px;
position: absolute;
top:200px;
left: 0;
z-index: 2;
display: none;
}
.menu2, .3 a {
display: block;
width: 218px;
height:181px;
background: url(images/btn_deliver_off.png) no-repeat center center;
}
.menu2, .3 a:hover {
display: block;
width: 218px;
height:181px;
background: url(images/btn_deliver_on.png) no-repeat center center;
}
.menu2, .3 a li em {
background: url(images/btn_txt_deliver.png) no-repeat;
width: 218px;
height: 88px;
position: absolute;
top:200px;
left: 0;
z-index: 2;
display: none;
}
HTML
<ul class="menu2">
<li class="1">
<a href="#"></a>
<em></em>
</li>
<li class="2">
<a href="#"></a>
<em></em>
</li>
<li class="3">
<a href="#"></a>
<em></em>
</li>
</ul>
jQuery
$(".menu2 a.1").hover(function() {
$(this).next("em").animate({opacity: "show", top: "180"}, "slow");
}, function() {
$(this).next("em").animate({opacity: "hide", top: "200"}, "fast");
});
$(".menu2 a.2").hover(function() {
$(this).next("em").animate({opacity: "show", top: "180"}, "slow");
}, function() {
$(this).next("em").animate({opacity: "hide", top: "200"}, "fast");
});
$(".menu2 a.3").hover(function() {
$(this).next("em").animate({opacity: "show", top: "180"}, "slow");
}, function() {
$(this).next("em").animate({opacity: "hide", top: "200"}, "fast");
});
Do you see the issue with my code below? I am trying to get three different images to invoke a different image each on hover... It works great with one but something is off with adding additional... I've tried many variations with classes and IDs but I'm stuck at this point so any help is greatly appreciated.
CSS
.menu2 {
padding: 0;
list-style: none;
}
.menu2 li {
float: left;
position: relative;
}
.menu2, .1 a {
display: block;
width: 218px;
height:181px;
background: url(images/btn_capture_off.png) no-repeat center center;
}
.menu2, .1 a:hover {
display: block;
width: 218px;
height:181px;
background: url(images/btn_capture_on.png) no-repeat center center;
}
.menu2, .1 li em {
background: url(images/btn_txt_capture.png) no-repeat;
width: 218px;
height: 88px;
position: absolute;
top:200px;
left: 0;
z-index: 2;
display: none;
}
.menu2, .2 a {
display: block;
width: 218px;
height:181px;
background: url(images/btn_manage_off.png) no-repeat center center;
}
.menu2, .2 a:hover {
display: block;
width: 218px;
height:181px;
background: url(images/btn_manage_on.png) no-repeat center center;
}
.menu2, .2 a li em {
background: url(images/btn_txt_manage.png) no-repeat;
width: 218px;
height: 88px;
position: absolute;
top:200px;
left: 0;
z-index: 2;
display: none;
}
.menu2, .3 a {
display: block;
width: 218px;
height:181px;
background: url(images/btn_deliver_off.png) no-repeat center center;
}
.menu2, .3 a:hover {
display: block;
width: 218px;
height:181px;
background: url(images/btn_deliver_on.png) no-repeat center center;
}
.menu2, .3 a li em {
background: url(images/btn_txt_deliver.png) no-repeat;
width: 218px;
height: 88px;
position: absolute;
top:200px;
left: 0;
z-index: 2;
display: none;
}
HTML
<ul class="menu2">
<li class="1">
<a href="#"></a>
<em></em>
</li>
<li class="2">
<a href="#"></a>
<em></em>
</li>
<li class="3">
<a href="#"></a>
<em></em>
</li>
</ul>
jQuery
$(".menu2 a.1").hover(function() {
$(this).next("em").animate({opacity: "show", top: "180"}, "slow");
}, function() {
$(this).next("em").animate({opacity: "hide", top: "200"}, "fast");
});
$(".menu2 a.2").hover(function() {
$(this).next("em").animate({opacity: "show", top: "180"}, "slow");
}, function() {
$(this).next("em").animate({opacity: "hide", top: "200"}, "fast");
});
$(".menu2 a.3").hover(function() {
$(this).next("em").animate({opacity: "show", top: "180"}, "slow");
}, function() {
$(this).next("em").animate({opacity: "hide", top: "200"}, "fast");
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将类名称从数字更改为以字母开头的名称。
css中的逗号表示OR。删除 .menu2 之后多余的逗号。
将
$(".menu2 a.1").hover
更改为$(".menu2 .someclass1 a").hover
,即a
选择器需要放在最后。似乎有效。
http://www.jsfiddle.net/edKys/9/
Change the class names from a number to something starting with a letter.
A comma in css means OR. removing the extra comma after .menu2.
Change the
$(".menu2 a.1").hover
to$(".menu2 .someclass1 a").hover
, thea
selector needs to come last.seems to work.
http://www.jsfiddle.net/edKys/9/
我让它工作了!
这是最终的代码
CSS
jQuery
HTML
我想感谢您的回答。之后!
I got it to work!
Here is the final code
CSS
jQuery
HTML
I wanted to thank you for your answer. Later!