经过一些玩耍,发现了
getComputedStyle,我已经把这个放在一起了。
#element {
background-color: rgb(10,10,10);
background-color: rgba(10,10,10,1);
}
HTMLElement.prototype.alpha = function(a) {
current_color = getComputedStyle(this).getPropertyValue("background-color");
match = /rgba?\((\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(,\s*\d+[\.\d+]*)*\)/g.exec(current_color)
a = a > 1 ? (a / 100) : a;
this.style.backgroundColor = "rgba(" + [match[1],match[2],match[3],a].join(',') +")";
}
This is some content.
e = document.getElementById('element');
e.alpha(20);
>确保你定义你的css你的值,并级联,因为RGBA是CSS3。>还可以看到,你可以传递一个> 1为alpha,它将除以100你(我讨厌使用小数时思考百分比)。>享受!