Reputation: 6509
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"> </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}
Upvotes: 1
Views: 7037
Reputation: 4430
No, it's not possible unless children positioned outside it's container. Option:
using rgba()
like other answer, but it is not make it transparent exactly;
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')");
}
);
Upvotes: 0
Reputation: 18024
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:
#container.test {
background: rgba(255, 0, 0, 0.5); /* 0.5 = 50% transparency */
}
$(function() {
$('#john').mouseenter(function() {
$('#container').addClass("test");
}).mouseleave(function () {
$('#container').removeClass("test");
});
});
Upvotes: 2
Reputation: 1902
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.
Upvotes: 1