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

后端 未结 14 2208
离开以前
离开以前 2020-11-29 07:04
14条回答
  •  渐次进展
    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:

    Link 1


    Link 1

    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/

提交回复
热议问题