简体   繁体   English

子元素更改父图像的不透明度

[英]Child elements changing opacity with parent Image

I have a <div> element which has a background image. 我有一个具有背景图像的<div>元素。 On top of that I have some text that is hidden but when the user hovers over the <div> element that text will show and the <div> opacity will lower. 最重要的是,我隐藏了一些文本,但是当用户将鼠标悬停在<div>元素上时,该文本将显示,并且<div>不透明度会降低。 My problem is when you hover over the div all elements inside that change opacity as well. 我的问题是,当您将鼠标悬停在div上时,里面的所有元素也会改变不透明度。 I have looked through stackoverflow to see if anyone has the same problem but all i found were answers that had RGBA using background colors (not images). 我已经检查了stackoverflow以查看是否有人遇到相同的问题,但是我发现的所有答案都是使用背景颜色(而非图像)的RGBA的答案。

Here is my css: 这是我的CSS:

    .pic{
      background-image:url(http://www.granitesportsinstitute.com/wp-content/uploads/2014/06/Green-Sea-Turtle-150x150.jpg);
     -webkit-transition: all .3s ease-in-out;
     -moz-transition: all .3s ease-in-out;
     -o-transition: all .3s ease-in-out;
     transition: all .3s ease-in-out;
} 
.textstuff{
    visibility:hidden;
}
.pic:hover .textstuff{
    visibility:visible;
    color:black;
}
.pic:hover{
    filter: alpha(opacity=30);  
    -moz-opacity: 0.3;  
    -khtml-opacity: 0.3;  
    opacity: 0.3;
}

HTML HERE: HTML此处:

    <div class="pic" style="height:150px;width:150px;">
       <div class="textstuff">this is text</div>
    </div>

Anytime you change the opacity of a parent element, it automatically applies to all children. 每当您更改父元素的不透明度时,它都会自动应用于所有子元素。 The only way to get around this is to not have your opacified element be the parent of the text. 解决此问题的唯一方法是不要让不透明的元素成为文本的父级。 It'd probably be best to refactor the background element as a sibling of any other elements in that container and just give it some absolute positioning. 最好将背景元素重构为该容器中任何其他元素的同级元素,并对其进行absolute定位。

Try with :before pseudo-element: :before伪元素:before尝试::

.pic {
    position: relative;
}

.pic:before {
    background-image:url(http://www.granitesportsinstitute.com/wp-content/uploads/2014/06/Green-Sea-Turtle-150x150.jpg);
    -webkit-transition: all .3s ease-in-out;
    -moz-transition: all .3s ease-in-out;
    -o-transition: all .3s ease-in-out;
    transition: all .3s ease-in-out;
    position: absolute;
    width: 100%;
    height: 100%;
    content: "";
    z-index: -1;
} 

.textstuff{
    visibility:hidden;
}

.pic:hover .textstuff{
    visibility:visible;
    color:black;
}

.pic:hover:before{
    filter: alpha(opacity=30);  
    -moz-opacity: 0.3;  
    -khtml-opacity: 0.3;  
    opacity: 0.3;
}

Any element within the .pic class would receive the opacity, in order for it not to receive it you have to create a wrapper for the pic and put the .textstuff in that wrapper sibling to the .pic .pic类中的任何元素都将接收不透明度,为了使其不接收不透明度,您必须为该图片创建一个包装器,然后将.textstuff放入该包装器中以该.pic兄弟中。

<div class="pic_wrapper">
    <div class="pic"></div>
    <div class="textstuff"></div>
</div>

I was able to get this working by wrapping it and setting the .pic to position absolute. 我可以通过将其包装并将.pic设置为绝对位置来使其工作。 This way it fills up the background but doesn't affect the text: 这样,它可以填充背景,但不会影响文本:

<div class="wrapper">
    <div class="pic"></div>
    <div class="textstuff">
        <p>This is the textstuff</p>
    </div>
</div>

CSS code: CSS代码:

.wrapper {
    position: relative;
}

.pic {
    background-image:url(http://www.granitesportsinstitute.com/wp-content/uploads/2014/06/Green-Sea-Turtle-150x150.jpg);
    width: 100%;
    height: 100%;
    position: absolute;
    -webkit-transition: all .3s ease-in-out;
    -moz-transition: all .3s ease-in-out;
    -o-transition: all .3s ease-in-out;
    transition: all .3s ease-in-out;
}
.textstuff {
    visibility:hidden;
}
.pic:hover ~ .textstuff {
    visibility:visible;
    color:black;
}
.pic:hover {
    opacity: 0.3;
    -moz-opacity: 0.3;
    -khtml-opacity: 0.3;
    opacity: 0.3;
}

Here is a fiddle showing an example: 这是一个显示示例的小提琴:

http://jsfiddle.net/jqxw5ajz/ http://jsfiddle.net/jqxw5ajz/

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

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