js取随机16进制颜色

1.代码:

export function getRandomColor() { 

function getRandomInt(max: number) {

    const randomInt = Math.floor(Math.random() * max)

    const hexString = randomInt.toString(16).padStart(2, '0')

    return hexString

  }

  const red = getRandomInt(256)

  const green = getRandomInt(256)

  const blue = getRandomInt(256)

  return `#${red}${green}${blue}`

}

2.调用 getRandomColor() 这个方法就好

猜你喜欢

转载自blog.csdn.net/m0_65292523/article/details/143360035