jQuery set CSS background opacity

前端 未结 4 1739
终归单人心
终归单人心 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:59

    try parseInt(hex,16) to convert hex string into decimal int

    so in this case it'll be:

    var color = '#abcdef';
    var rgbaCol = 'rgba(' + parseInt(color.slice(-6,-4),16)
        + ',' + parseInt(color.slice(-4,-2),16)
        + ',' + parseInt(color.slice(-2),16)
        +',0.5)';
    $('div').css('background-color', rgbaCol)
    

    you should create a function out of this to use in your application.

提交回复
热议问题