在HTML中,根据覆盖的背景区域的亮度来改变文本颜色?

PHPz
PHPz 转载
2023-08-28 13:29:02 253浏览

在HTML中,根据覆盖的背景区域的亮度来改变文本颜色?

var rgb = [255, 0, 0]; setInterval(display, 1000); function display() { rgb[0] = Math.round(Math.random() * 255); rgb[1] = Math.round(Math.random() * 255); rgb[2] = Math.round(Math.random() * 255); var d = Math.round(((parseInt(rgb[0]) * 299) + (parseInt(rgb[1]) * 587) + (parseInt(rgb[2]) * 114)) / 1000); // for foregound var f = (d> 125) ? 'black' : 'white'; // for background var b = 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')'; $('#box').css('color', f); $('#box').css('background-color', b); } <scriptsrc = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <div id = "box"> Demo</div>

以下是 CSS -

#box {
   width: 300px;
  height: 300px;
}

以上就是在HTML中,根据覆盖的背景区域的亮度来改变文本颜色?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除