我有以下 jQuery 演示:http://jsfiddle.net/FTERP/
目前,当我将鼠标悬停在蓝色框上时,steve
div 中的 img
会淡出。
有没有可能当我将鼠标悬停在蓝色框('john')上时,整个红色区域('container')的不透明度下降到 0.4 ,但蓝色框仍保持 100% 蓝色?
这是我的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}
最佳答案
不透明度将始终影响所有子元素。
如果您只是想淡出纯色背景,您可以使用带有 alpha channel 的颜色,例如 rgba()
或 hsla()
,但是:
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");
});
});
关于javascript - 悬停效果与 jQuery 影响容器 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16301140/