在vue 中 css 使用 js 变量

  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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容