简体   繁体   English

用jQuery悬停效果来影响容器div

[英]Hover effect with jQuery to affect container div

I have the following jQuery Demo : http://jsfiddle.net/FTERP/ 我有以下jQuery 演示http//jsfiddle.net/FTERP/

Currently when I hover over the blue box, the img inside steve div fades out. 目前,当我将鼠标悬停在蓝色框上时, steve div中的img淡出。

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? 是否有可能当我将鼠标悬停在蓝色框(' john ')上时,整个红色区域(' 容器 ')的不透明度下降到0.4 ,但蓝色框仍然是100%蓝色?

Here is my HTML: 这是我的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: 使用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: 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: 如果您只是尝试淡出纯色背景颜色,则可以使用带有alpha通道的颜色,例如rgba()hsla() ,但是:

CSS CSS

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

JavaScript JavaScript的

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

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

in your mouseenter call put 在你的mouseenter调用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. 您将需要将其他2个div移出容器div来执行此操作,因为它会影响所有子项,您可以将其设置为绝对值,然后将其他2个div移到其上面。 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; 像其他答案一样使用rgba() ,但它并不能使它完全透明;

  2. Use .png img, and replace it with transparent one while hover. 使用.png img,并在悬停时将其替换为透明的。 like: 喜欢:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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