小编Jon*_*Lee的帖子

如何基于动态更改的背景颜色动态更改文本颜色

我正在建立一个新的网站,需要我的文字根据不断变化的背景颜色来更改颜色,以保持对比度。我在网上搜寻了不涉及Sass的答案,但没有一个起作用...

我尝试了一些JavaScript,但是只有当背景是您手动更改的固定颜色时,它们才起作用。

我当前的文件:https : //codepen.io/jonathanlee/pen/wZXvRY

var color = function getRandomColor() {
  var letters = '0123456789ABCDEF'.split('');
  var color = '#';
  for (var i = 0; i < 6; i++) {
    color += letters[Math.floor(Math.random() * 16)];
  }
  return color;
}
setInterval(function() {
  document.getElementById("test").style.backgroundColor = color(); //() to execute the function!
}, 3000);

var ww = function isDarkColor(rgb) {
  return Math.round((
    parseInt(rgb[0], 10) * 299 +
    parseInt(rgb[1], 10) * 587 +
    parseInt(rgb[2], 10) * 114) / 1000) <= 140
}

if (ww …
Run Code Online (Sandbox Code Playgroud)

html javascript css

6
推荐指数
1
解决办法
120
查看次数

标签 统计

css ×1

html ×1

javascript ×1