vue样式绑定

本文展示了如何在 Vue.js 中使用 class 和 style 绑定来动态应用 CSS 样式。通过字符串、数组和对象三种方式,演示了 class 和 style 绑定的灵活性,包括对颜色、背景等属性的控制。示例代码详细说明了各种绑定方法的用法,并在页面上呈现了实际效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

css样式绑定和style内联样式绑定均可以绑定字符串,数组,对象

代码:

<template>
<!-- 样式绑定 -->
    <div class="base06">
        <home-back></home-back>
        <h2 :class="classString">样式绑定 - class绑定字符串</h2>
        <h2 :class="classArr">样式绑定 - class绑定数组</h2>
        <h2 :class="classObj">样式绑定 - class绑定对象</h2>

        <!-- 行内样式 -->
        <h2 :style="styleString">样式绑定 - style绑定字符串</h2>
        <h2 :style="styleObj">样式绑定 - style绑定对象</h2>
        <h2 :style="[styleObj,styleObj2]">样式绑定 - style绑定数组</h2>
    </div>
</template>

<script>
import Back from "@/components/common/back.vue"
export default {
    data(){
        return{
            classString:'red',
            classArr:['red','background'],
            classObj:{
                'red':true,
                'background':true,
                'green':false
            },
            styleString:'color:red',
            styleObj:{
                color:'red',
                background:'#ccc'
            },
            styleObj2:{
                fontSize:'14px'
            }
        }
    },
    components:{
        'home-back':Back
    },
    methods:{
    }
}
</script>

<style scoped lang="stylus">
    .base06
        margin-top 30px
        margin-left 50px
        h2
            margin-top 20px
        .red
            color red
        .green
            color green
        .background
            background #ccc
</style>

页面展示:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值