UPDATE

上级 4f39fc67
<script setup>
import HelloWorld from './components/HelloWorld.vue'
import TheWelcome from './components/TheWelcome.vue'
</script>
<template> <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> <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> </main>
</template> </template>
<script>
<style scoped> import { reactive, toRefs, nextTick } from "vue";
header { export default {
line-height: 1.5; name: "randomColor",
} setup() {
const BasicState = reactive({
.logo { data:[
display: block; {name:'1'},
margin: 0 auto 2rem; {name:'2'},
} {name:'3'},
{name:'4'},
@media (min-width: 1024px) { {name:'5'},
header { {name:'6'}
display: flex; ],
place-items: center; exist_color:[],
padding-right: calc(var(--section-gap) / 2); randomColor(exist_color) {
} let r = Math.floor(Math.random() * 192);
let g = Math.floor(Math.random() * 192);
.logo { let b = Math.floor(Math.random() * 192);
margin: 0 2rem 0 0; 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);
header .wrapper { let color = '#' + r16 + g16 + b16;
display: flex; if (exist_color.indexOf(color) === -1) {
place-items: flex-start; return color;
flex-wrap: wrap; } 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> </style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册