jQuery set CSS background opacity

前端 未结 4 1730
终归单人心
终归单人心 2020-12-06 06:14

I have a

whose transparency of its background-color (and not its contents) I\'d like to change. A remote API sends me this colour:

4条回答
  •  無奈伤痛
    2020-12-06 06:52

    You may try this

    function convertHex(hex,opacity){
        hex = hex.replace('#','');
        r = parseInt(hex.substring(0,2), 16);
        g = parseInt(hex.substring(2,4), 16);
        b = parseInt(hex.substring(4,6), 16);
        result = 'rgba('+r+','+g+','+b+','+opacity/100+')';
        return result;
    }
    
    $('h1').css('background', convertHex('#A7D136', 0.5));
    

    An example here.

提交回复
热议问题