简体   繁体   中英

Hover effect with jQuery to affect container div

I have the following jQuery Demo : http://jsfiddle.net/FTERP/

Currently when I hover over the blue box, the img inside steve div fades out.

Is it possible that when I hover over the blue box(' john '), the whole red area (' container ') opacity drops to 0.4 , but the blue box remains 100% blue?

Here is my HTML:

<div id="container">

    <div id="john" class="character normalClassName1">
        <a href="#" id="link1">&nbsp;</a>
    </div>  

    <div id="steve" class="character">
        <img src="http://placehold.it/400x400" />
    </div>

</div>  

Javascript:

 $(function() {
    $('#john').mouseenter(function() {
        $(this).addClass('hoverClassName1');
        $('.character[id!=john]').css({opacity:0.5});

        var button = $(this).find('.button');

        button.html('View more');

    }).mouseleave(function () {
        $('.hoverClassName1').removeClass('hoverClassName1');
        $('.character').css({opacity:1});

        $('.button').html('View');
    });
});

CSS:

#container {width:100%;background:red;float:left;height:450px}

#john {position:relative;margin-top:-80px;margin-left:0px;background:blue;height:380px;float:left;width: 495px;}
#john div {margin-left:250px;width:180px;height:float:left;margin-top:205px}
#john div p {color:#074471;font-weight:bold;font-size:13px;margin-left:20px;}

#steve img {float:left}

#link1 {background:transparent;position:absolute;top:0px;left:0;width:100%;height:100%;z-index:1}

.normalClassName1 {width:495px!important;z-index:3;} 
.hoverClassName1 {width:495px;z-index:4!important}

Opacity will always affect all children elements.

If you're only trying to fade out a solid background color, you can use colors with an alpha channel like rgba() or hsla() , however:

CSS

#container.test {
    background: rgba(255, 0, 0, 0.5); /* 0.5 = 50% transparency */
}

JavaScript

$(function() {
    $('#john').mouseenter(function() {
        $('#container').addClass("test");
    }).mouseleave(function () {
        $('#container').removeClass("test");
    });
});

http://jsfiddle.net/FTERP/2/

in your mouseenter call put

$('#container').css({'opacity' : '.4'});

You will need to move your other 2 divs out of the container div to do this because it effects all children, you could set it to a absolute and then move your other 2 divs on top of it. Its dirty but will work.

No, it's not possible unless children positioned outside it's container. Option:

  1. using rgba() like other answer, but it is not make it transparent exactly;

  2. Use .png img, and replace it with transparent one while hover. like:

     $('selector').hover( function () { $(this).parent().css("background-image", "url('transparent.png')"); }, function () { $(this).parent().css("background-image", "url('normal.png')"); } ); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM