JQuery disappears in background image on hover

I have a number of menu buttons, and I need the background image to be transparent and then disappear when it capsizes (freezes). can i use jQuery for this?

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

+3
source share
4 answers

Since it is impossible to fade the background only if the text in the block is visible, you can make small changes to the layout:

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

DEMO: http://jsfiddle.net/JdHWY/11/

+4
source

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

Watch the demo

+2
source

- ( ), fadin() fadeout() , .

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

0

demo 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(), .

0

All Articles