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>
相关推荐
kyriewen1 天前
Git Commit 前自动修复代码风格?配置 Husky + lint-staged,从此 CR 只聊逻辑
前端·git·面试
岁月宁静1 天前
RAG 文档摄入全链路,从原理到生产落地
vue.js·人工智能·python
小和尚同志1 天前
AI 自动化测试探索(一):Playwright MCP
前端·人工智能·aigc
老马识途2.01 天前
在AI的帮助下理解spring的启动过程
java·前端·spring
徐小夕1 天前
Loop Engineering 深度解析与实战指南(全网最全)
前端·算法·github
运筹vivo@1 天前
Python ContextVar 底层机制与内存模型拆解
前端·数据库·python
#麻辣小龙虾#1 天前
基于vue3.0开发一款【固废与废气运维管理系统】(支持源码)
前端·vue.js·vue3
Cosolar1 天前
Docsify零构建文档站完全指南:从快速搭建到企业级部署
前端·开源·github
weixin_471383031 天前
Taro-02-页面路由
前端·taro
星栈独行1 天前
Makepad 应用如何读文件、调接口、保存数据
前端·程序人生·ui·rust·github