簡體   English   中英

jQuery在懸停的背景圖像淡出

[英]jQuery fade in background image on hover

我有一排菜單按鈕,我需要背景圖像是透明的,然后在翻轉時淡入(懸停)。 我可以用jQuery來實現這個嗎?

http://jsfiddle.net/craigzilla/JdHWY/

由於只有在塊中的文本可見時才能淡化背景,您可以在標記中進行小的更改:

HTML:

<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();
});​

演示: http //jsfiddle.net/JdHWY/11/

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>

CSS:

.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);
});

演示

它是這樣的(我的頭腦),然后你必須添加fadin()fadeout()到混合。

http://jsfiddle.net/JdHWY/4/

演示jsFiddle

$('.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()可以清除動畫隊列,因此可以處理非常好的快速鼠標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM