美文网首页
在vue 中 css 使用 js 变量

在vue 中 css 使用 js 变量

作者: daozun | 来源:发表于2022-04-18 19:18 被阅读0次
  1. vue2:
<template>
  <div
    :style="userStyle"
    class="word"
  >
    哈哈哈哈哈
  </div>
</template>

<script>
export default {
  data () {
    return {
      speed: 20
    }
  },
  computed: {
    userStyle () {
      return {
       "--speed": this.speed + 240 + "deg",
      }
    }
  }
</script>

<style scoped>
.word {
    animation: lunpan 1s steps(1, start);
    animation-fill-mode: forwards;
}

@keyframes lunpan {
  from {
    transform: rotate(0deg);
  }

  to {
    transform: rotate(var(--speed));
  }
}
</style>
  1. vue3(optional api)
<template>
  <div class="text">hello</div>
</template>

<script>
export default {
  data() {
    return {
      color: 'red'
    }
  }
}
</script>

<style>
.text {
  color: v-bind(color);
}
</style>
  1. vue3(Composition API)
<template>
  <div class="text">hello</div>
</template>

<script setup>
import { ref } from "vue";
const customTheme = ref({
    color: 'red'
});
</script>

<style>
.text {
  color: v-bind("customTheme.color");
}
</style>

相关文章

网友评论

      本文标题:在vue 中 css 使用 js 变量

      本文链接:https://www.haomeiwen.com/subject/jfadertx.html