UPDATE

上级 4f39fc67
<script setup>
import HelloWorld from './components/HelloWorld.vue'
import TheWelcome from './components/TheWelcome.vue'
</script>
<template>
<header>
<img alt="Vue logo" class="logo" src="./assets/logo.svg" width="125" height="125" />
<div class="wrapper">
<HelloWorld msg="You did it!" />
</div>
</header>
<main>
<TheWelcome />
<button @click="configColor">生成唯一随机深色</button>
<div class="colorChunk" v-for="item in data" :key="item" :style="`background-color: ${item.color}`">{{ item.color }}</div>
</main>
</template>
<style scoped>
header {
line-height: 1.5;
}
.logo {
display: block;
margin: 0 auto 2rem;
}
@media (min-width: 1024px) {
header {
display: flex;
place-items: center;
padding-right: calc(var(--section-gap) / 2);
}
.logo {
margin: 0 2rem 0 0;
}
header .wrapper {
display: flex;
place-items: flex-start;
flex-wrap: wrap;
<script>
import { reactive, toRefs, nextTick } from "vue";
export default {
name: "randomColor",
setup() {
const BasicState = reactive({
data:[
{name:'1'},
{name:'2'},
{name:'3'},
{name:'4'},
{name:'5'},
{name:'6'}
],
exist_color:[],
randomColor(exist_color) {
let r = Math.floor(Math.random() * 192);
let g = Math.floor(Math.random() * 192);
let b = Math.floor(Math.random() * 192);
let r16 = r.toString(16).length === 1 && r.toString(16) <= "f" ? 0 + r.toString(16) : r.toString(16);
let g16 = g.toString(16).length === 1 && g.toString(16) <= "f" ? 0 + g.toString(16) : g.toString(16);
let b16 = b.toString(16).length === 1 && b.toString(16) <= "f" ? 0 + b.toString(16) : b.toString(16);
let color = '#' + r16 + g16 + b16;
if (exist_color.indexOf(color) === -1) {
return color;
} else {
BasicState.randomColor(exist_color);
}
},
configColor(){
BasicState.exist_color = [];
BasicState.data.forEach(ele=>{
let color = BasicState.randomColor(BasicState.exist_color);
ele = {
name:`第${ele.name}个`,
color:color
}
})
}
})
BasicState.configColor();
return {
...toRefs(BasicState)
}
}
}
</script>
<style scoped>
.colorChunk{
height: 200px;
width: 200px;
border: 1px solid red;
}
</style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册