最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Changing background color opacity on mouseover - Stack Overflow

programmeradmin2浏览0评论

I have a DIV that I'd like to change the background color opacity on, depending on if the mouse is over it or not.

I know you can use background: rgba(54, 25, 25, .5) etc, but I want to set the colour separately. Is there any way I can JUST modify the OPACITY, and not the colour.

I could opacity: 0.3, etc, but that effects the whole DIV, and I just want to affect the background colour.

I have a DIV that I'd like to change the background color opacity on, depending on if the mouse is over it or not.

I know you can use background: rgba(54, 25, 25, .5) etc, but I want to set the colour separately. Is there any way I can JUST modify the OPACITY, and not the colour.

I could opacity: 0.3, etc, but that effects the whole DIV, and I just want to affect the background colour.

Share Improve this question edited Aug 21, 2019 at 18:55 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Apr 30, 2012 at 18:08 Chuck Le ButtChuck Le Butt 48.8k62 gold badges209 silver badges297 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 5

No html/css doesn't have that option built in, but since you're accessing/setting the colour in javascript you might as well add in your own function which can handle that for you.

Here's an example for you:

<script type="text/javascript">
function RGBA(red,green,blue,alpha) {
    this.red = red;
    this.green = green;
    this.blue = blue;
    this.alpha = alpha;
    this.getCSS = function() {
        return "rgba("+this.red+","+this.green+","+this.blue+","+this.alpha+")";
    }
}

// store a copy of the color
var bgColor = new RGBA(255,0,0,0.5);

function setBgOpacity(elem, opac) {
    bgColor.alpha = opac;
    elem.style.backgroundColor = bgColor.getCSS();
}
</script>

Then in the HTML use the onmouseover event to change the opacity of the bgColor:

<div onmouseover="setBgOpacity(this, '0.3');"
     onmousout="setBgOpacity(this, '0.5');">Put your mouse over me</div>

You can make the background part of a different div and set the opacity of THAT div, i.e.

<div id="container">
    <div id="background"></div>
    <div id="content">
        <p>Lorum ipsum...</p>
    </div>
</div>

And

#container { overflow:hidden; position:relative; }
#background { 
    position:absolute; 
    top:0; 
    left:0; 
    width:100%; 
    height:100%; 
    background:#FF0000;
}
#background:hover { opacity:0.3; }
#content { position:relative; z-index:10; }

There a difference between the Alpha value in RGBa and Opacity. Opacity affects all child elements, Alpha does not.

You'll have to read the current colour value, then restate it as RGBa with the new Alpha value. You may need to convert the current hex colour value to a decimal triplet to do this.

If you are relying on RGBA to modify the opacity of the background color, no, there is no way to set that separately from the color itself. You will have to declare explicit RGBA values for both your normal and hover states.

No, you can't edit only the alpha of rgba. So you should use the RGB part of the RGBa.

If you want a separate background colour from the container you may want to use :before or :after.

.container { 
    position: relative;

    &:before {
        content: '';
        position: absolute;
        width: 100%;
        height: 100%;
        background-color: #000;
        opacity: 1;
        z-index: -1;
    }

    &:hover {
        &:before {
            opacity: 0.5;
        }
    }

    .content {
        z-index: 1;
    }
}

When you hover on the .container, only the opacity of the :before is effected and not the contents.

发布评论

评论列表(0)

  1. 暂无评论