我有一排菜单按钮,需要背景图片透明,并且在鼠标悬停时淡入。我可以使用jQuery来实现这个功能吗?
我有一排菜单按钮,需要背景图片透明,并且在鼠标悬停时淡入。我可以使用jQuery来实现这个功能吗?
<div class="menuitem"><span></span>ITEM 01</div>
CSS:
.menuitem {
position: relative;
/* ... */
}
.menuitem span {
position: absolute;
display: none;
top: 0;
left: 0;
width: inherit;
height: inherit;
background-image: url(http://thumbs.gograph.com/gg58916312.jpg);
z-index: -1;
}
JavaScript:
$(".menuitem").hover(function() {
$(this).children("span").fadeIn();
}, function() {
$(this).children("span").fadeOut();
});
HTML:
<div class="header">
<div class="menuitem">ITEM 01</div>
<div class="menuitem">ITEM 02</div>
<div class="menuitem">ITEM 03</div>
<div class="menuitem">ITEM 04</div>
</div>
.menuitem {
background-image: url(http://thumbs.gograph.com/gg58916312.jpg);
width: 180px;
margin-left: 1px;
margin-right: 1px;
margin-top: 102px;
height: 75px;
float: left;
cursor: pointer;
font-size: 36px;
text-align: center;
line-height: 85px;
opacity: 0.5
}
jQuery:
$('.menuitem').hover(function() {
$(this).stop().animate({
opacity: 1
}, 300);
}, function() {
$(this).stop().animate({
opacity: 0.5
}, 300);
});
查看演示
.stop()
呢? - Roko C. Buljan$('.menuitem').fadeTo(0,0.7).on('mouseenter mouseleave',function( ev ){
ev=ev.type==='mouseenter' ? // event is mouseenter?
$(this).stop().fadeTo(200,1) : // (if yes)
$(this).stop().fadeTo(200,0.7) ; // (if no) event is mouseleave
});
这将非常好地处理多次快速鼠标悬停,感谢.stop()
清除动画队列。
:before
或:after
代替span
并依赖于CSS3的opacity
过渡而不是jQuery来改进。 - Ennui;)
- VisioN