uniapp 随机背景色和字体颜色
时间: 2023-11-26 13:04:47 浏览: 74
可以使用Math.random()生成随机数,然后将其转换成16进制颜色代码和RGB颜色代码。
例如,以下代码会生成一个随机的16进制颜色代码:
```
let randomColor = '#' + Math.floor(Math.random() * 16777215).toString(16);
```
以下代码会生成一个随机的RGB颜色代码:
```
let randomColor = 'rgb(' + Math.floor(Math.random() * 256) + ',' + Math.floor(Math.random() * 256) + ',' + Math.floor(Math.random() * 256) + ')';
```
然后,可以将随机颜色应用到背景色和字体颜色中:
```
<template>
<view :style="{backgroundColor: bgColor, color: textColor}">
<text>Hello World</text>
</view>
</template>
<script>
export default {
data() {
return {
bgColor: '',
textColor: ''
}
},
mounted() {
this.bgColor = '#' + Math.floor(Math.random() * 16777215).toString(16);
this.textColor = '#' + Math.floor(Math.random() * 16777215).toString(16);
}
}
</script>
```