Is it possible to change only the alpha of a rgba background colour on hover?

后端 未结 14 2206
离开以前
离开以前 2020-11-29 07:04
相关标签:
14条回答
  • 2020-11-29 07:36

    This is about the simplest way; put this in your css stylesheet:

    a:hover { color : #c00; } 
    

    done!

    0 讨论(0)
  • 2020-11-29 07:37

    No, that's not possible.

    If you want to use rgba, you must set each value together. There's no way to only change the alpha.

    0 讨论(0)
  • 2020-11-29 07:37

    Why not use :hover and specify a different opacity in the hover class?

    a:hover {
         opacity:0.6
    }
    
    0 讨论(0)
  • 2020-11-29 07:39

    You could do various things to avoid having to hard code the numbers if you want to. Some of these methods only work if you use a plain white background as they're really adding white on top rather than reducing opacity. The first one should work fine for everything provided:

    • you aren't already using the psuedo-element for something; and
    • you can set position to relative or absolute on the <div> tag

    Option 1: ::before psuedo-element:

    .before_method{
      position:relative;
    }
    .before_method:before{
      display:block;
      content:" ";
      position:absolute;
      z-index:-1;
      background:rgb(18, 176, 41);
      top:0;
      left:0;
      right:0;
      bottom:0;
      opacity:0.5;
    }
    .before_method:hover:before{
      opacity:1;
    }
    

    Option 2: white gif overlay:

    .image_method{
      background-color: rgb(118, 76, 41);
      background-image: url(https://upload.wikimedia.org/wikipedia/commons/f/fc/Translucent_50_percent_white.png)
    }
    .image_method:hover{
      background-image:none;
    }
    

    Option 3: box-shadow method:

    A variation of the gif method, but may have performance issues.

    .shadow_method{
      background-color: rgb(18, 176, 41);
      box-shadow:inset 0 0 0 99999px rgba(255,255,255,0.2);
    }
    .shadow_method:hover{
      box-shadow:none;
    }
    

    CodePen examples: http://codepen.io/chrisboon27/pen/ACdka

    0 讨论(0)
  • 2020-11-29 07:43

    there is an alternative,you can add a linear-gradient background image onto the original color.

    a{
      background: green
    }
    a:hover{
      background-image:linear-gradient(hsla(0,0%,0%,.2) 100%,transparent 100%) // darker
    }
    a:hover{
      background-image:linear-gradient(hsla(255,100%,100%,.2) 100%,transparent 100%) // lighter
    }
    

    also, with css3 filter property,you can do that too,but it seems that it will change the text color

    a:hover{
       filter: brightness(80%) //darker
    }
    a:hover{
       filter: brightness(120%) //lighter
    }
    

    here is a jsfiddle:https://jsfiddle.net/zhangyu911013/epwyL296/2/

    0 讨论(0)
  • 2020-11-29 07:45

    I faced a similar problem. Here's what I did and it works fine( only alpha changes on hover and also the text is not affected) by the following steps:

    1) Apply a highlighted(or any of your choice) class to whichever element you wish to change background alpha of.

    2) Get the background color rgba

    3) Store it in a string and manipulate it(change alpha) as you want on hover(mouseenter and mouseleave)

    HTML Code:

    <div class="highlighted brown">Link 1</div><br><br>
    <div class="highlighted green">Link 1</div>
    

    CSS Code:

    .brown {background-color: rgba(118,76,41,.8);}
    .green {background-color: rgba(51,91,11,.8);}
    

    Javascript Code:

    $(document).on({
    
                mouseenter: function() {
    
                var rgba_str = $(this).css("background-color");
                var new_rgba_str ="rgba(" + rgba_str.substring(rgba_str.lastIndexOf("(")+1,rgba_str.lastIndexOf(",")) + ", 0.5)";   
    
                    $(this).css("background-color",new_rgba_str ); 
                   },
    
                 mouseleave: function(){
    
                     var rgba_str = $(this).css("background-color");
    
                var new_rgba_str ="rgba(" + rgba_str.substring(rgba_str.lastIndexOf("(")+1,rgba_str.lastIndexOf(",")) + ", 0.8)";               
                    $(this).css("background-color",new_rgba_str ); 
    
                   }
    
            },'.highlighted');
    

    Working Fiddle:http://jsfiddle.net/HGHT6/1/

    0 讨论(0)
提交回复
热议问题