pim*_*vdb 13
首先,创建一个确定颜色是否暗的函数(源,修改):
function isDark( color ) {
var match = /rgb\((\d+).*?(\d+).*?(\d+)\)/.exec(color);
return parseFloat(match[1])
+ parseFloat(match[2])
+ parseFloat(match[3])
< 3 * 256 / 2; // r+g+b should be less than half of max (3 * 256)
}
Run Code Online (Sandbox Code Playgroud)
然后,使用以下内容:
$('elem').each(function() {
$(this).css("color", isDark($(this).css("background-color")) ? 'white' : 'black');
});
Run Code Online (Sandbox Code Playgroud)