简体   繁体   English

动态地将列表中项目的背景颜色从深色更改为浅色

[英]dynamically change the background color of items in a list from dark to light

I want to change the background of the elements in a list from dark to light.我想将列表中元素的背景从深色更改为浅色。

The number of items in the list is variable.列表中的项目数是可变的。 It could be 5, it could be 50.可能是5,也可能是50。

I can set the default color of the first item.我可以设置第一项的默认颜色。

for example;例如; for example例如

https://jsfiddle.net/tL25ngrq/1/ https://jsfiddle.net/tL25ngrq/1/

class TodoApp extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
    }
  }
  
  render() {
  const min = 1;
  const max = 255;
  const rand = min + Math.random() * (max - min);
  const minColor = 1;
  const maxColor = 255;
  const randColor = minColor + Math.random() * (maxColor - minColor);
  console.log(randColor)
  let rows = [];
  for (let i = 0; i < rand; i++) {
  rows.push(<tr style={{ backgroundColor: `rgb(10, ${randColor * i}, 100)` }} ><td>Test {i}</td></tr>)}
    return (
      <div>
       <table>
        <tr>
         <th>Title</th>
       </tr>
        {rows}
     </table>
      </div>
    )
  }
}

ReactDOM.render(<TodoApp />, document.querySelector("#app"))

Actually I'd suggest you to use hsv instead of rgb.其实我建议你使用 hsv 而不是 rgb。 In hsv, v is represent value, and the lower it is, the darker color will be.在hsv中,v是表示值,v越小,颜色越深。

So for example you can take range from 20% (0% is fully black) to 100% and make some calculation over it.因此,例如,您可以采用从 20%(0% 为全黑)到 100% 的范围,并对其进行一些计算。 You can just divide 80 on number of rows, then for each row set V to rowNumber * (80 / numberOfRows) , I'm not sure how it work with decimal numbers, so you might use Math.floor or something like that to get an integer value.您可以在行数上除以 80,然后将每行 V 设置为rowNumber * (80 / numberOfRows) ,我不确定它如何与十进制数一起使用,因此您可以使用 Math.floor 或类似的东西来获得一个 integer 值。

Hope it helps希望能帮助到你

Here is an over simplified example, it generates a random color on each run, and combine it with a generated lightness based on the index i of the list to create a color value for each item.这是一个过度简化的示例,它在每次运行时生成随机颜色,并将其与基于列表索引i生成的亮度结合起来,为每个项目创建一个颜色值。

The length of the list is randomly generated, same as the posted code, and the lightness of each item gradually increase (go from dark to light).列表的长度是随机生成的,和贴出的代码一样,每一项的亮度逐渐增加(从暗到亮)。

The color (Hue), min and max lightness of the list can be customized.可以自定义列表的颜色(色调)、最小和最大亮度。

This example runs in the snippet below for convenience, and displays randomized results by running and hiding the snippet.为方便起见,此示例在下面的代码片段中运行,并通过运行和隐藏代码片段来显示随机结果。

Hope it will help.希望它会有所帮助。

 class TodoApp extends React.Component { constructor(props) { super(props) this.state = { } } render() { // Set the random range for length of list const min = 5; const max = 30; const rand = min + Math.random() * (max - min); // Get a random color (Hue) for first item, or set a fixed one const minColor = 0; const maxColor = 360; const randColor = minColor + Math.floor(Math.random() * (maxColor - minColor)); // Set min and max lightness const minLit = 50; const maxLit = 90; // Since the list go lighter, minLit is also the first item lightness, and maxLit the last item lightness const calcLit = (i) => (Math.floor((i* (maxLit-minLit))/(rand - 1))) + minLit; let rows = []; for (let i = 0; i < rand; i++) { const itemLit = calcLit(i); const backgroundColor = `hsl(${randColor} 100% ${itemLit}%)`; rows.push(<tr style={{backgroundColor}} ><td>{backgroundColor}</td></tr>)} return ( <div> <table> <tr> <th>Title</th> </tr> {rows} </table> </div> ) } } ReactDOM.render(<TodoApp />, document.querySelector('#root'));
 <div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.1.0/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.1.0/umd/react-dom.production.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM