vue3 css使用v-bind实现动态样式

vue3 css使用v-bind实现动态样式

html 复制代码
<template>
    <button @click="toggleTheme">切换主题</button>
    <div class="card"></div>
</template>

<script setup>
import { ref } from 'vue'

const primaryColor = ref('#007bff')
const isLarge = ref(false)
const height = ref(200)
const margin = ref(20)

function toggleTheme() {
  // 切换亮色/暗色主题
  if (primaryColor.value === '#007bff') {
    primaryColor.value = '#ffc107'
    isLarge.value = true
  } else {
    primaryColor.value = '#007bff'
    isLarge.value = false
  }
}
</script>

<style scoped>
.card {
  margin-top: v-bind('margin * 2 + "px"');
  width: v-bind('isLarge ? "300px" : "200px"');
  height: v-bind('height + "px"');
  background-color: v-bind(primaryColor);
}
</style>
相关推荐
humcomm1 天前
元框架的工作原理详解
前端·前端框架
canonical_entropy1 天前
Attractor Before Harness: AI 大规模开发的方法论
前端·aigc·ai编程
zhangxingchao1 天前
多 Agent 架构到底怎么选?从 Claude Agent Teams、Cognition/Devin 到工程落地原则
前端·人工智能·后端
IT_陈寒1 天前
SpringBoot那个自动配置的坑,害我排查到凌晨三点
前端·人工智能·后端
Honor丶Onlyou1 天前
VS Code 右键菜单修复记录
前端
PILIPALAPENG1 天前
Python 语法速成指南:前端开发者视角(JS 类比版)
前端·人工智能·python
JYeontu1 天前
轮播图不够惊艳?试下这个立体卡片轮播图
前端·javascript·css
张就是我1065921 天前
从前端角度理解 CVE-2026-31431
前端
AGoodrMe1 天前
swift基础之async/await
前端·ios
irving同学462381 天前
从零搭建生产级 RAG:Embedding、Chunking、Hybrid Search 与 Reranker
前端·后端