webfansplz / vuejs-challenges

Collection of Vue.js challenges
https://vuejs-challenges.netlify.app/
MIT License
2.72k stars 188 forks source link

14 - 动态CSS #2790

Open Sakura-Love-Arcaea opened 2 months ago

Sakura-Love-Arcaea commented 2 months ago
<script setup>
import { ref } from 'vue';
const theme = ref('red');

const colors = ['blue', 'yellow', 'red', 'green'];

setInterval(() => {
  theme.value = colors[Math.floor(Math.random() * 4)];
}, 1000);
</script>

<template>
  <p :style="{ color: theme }">hello</p>
  <p class="p" :style="{ color: theme }">hello</p>
</template>

<style scoped>
.p {
  color: v-bind(theme);
}
</style>
Sakura-Love-Arcaea commented 2 months ago

我用了兩種方法