vue之样式绑定这些你知道吗

本文通过实例演示了Vue中动态应用样式的方法,包括对象语法、数组语法及两者的结合使用,同时展示了如何使用v-bind绑定内联样式。

您可将以下代码c,v到编辑器打开

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

    <style>
        /* 对象语法  */
        
        .active {
            border: 2px solid pink;
            width: 200px;
            height: 200px;
        }
        
        .error {
            background-color: aqua;
        }
        /* 数组语法 */
        
        .active2 {
            border: 2px solid rgb(47, 27, 223);
            width: 200px;
            height: 200px;
        }
        
        .error2 {
            background-color: rgb(216, 29, 160);
        }
        /* 结合使用 */
        
        .test {
            color: #13e713;
        }
    </style>
</head>

<body>
    <div id="app">
        <!-- 对象语法active为类名,isActive控制是否显示 -->
        <div :class="{active:isActive,error:isError}">对象语法</div>
        <button @click="qiehuan">对象语法切换</button>
        <hr>
        <!-- 数组语法 -->
        <div :class="[activeClass,errorClass]">数组语法</div>
        <button @click="shuzu">数组语法切换</button>
        <hr>
        <!-- 结合使用a类名不会覆盖 -->
        <div :class="[activeClass,errorClass,{test:isTest}]" class="a">结合使用</div>
        <button @click="shuzu">结合使用</button>
        <hr>
        <!-- style内联样式 -->
        <div :style="{border:a,width:b,height:c}">style内联样式</div>
    </div>
    <script type="text/javascript">
        var vm = new Vue({
            el: '#app',
            data: {
                //对象语法可以简化为obj{  isActive: true, isError: true,}
                isActive: true,
                isError: true,
                // 数组语法active2:类名,activeClass变量名,也可以简化为activeClass:['active2','error2']
                activeClass: 'active2',
                errorClass: 'error2',
                // 结合使用
                isTest: true,
                // style内联样式
                a: '1px solid blue',
                b: '200px',
                c: '200px'
            },
            methods: {
                // 对象语法切换
                qiehuan: function() {
                    this.isActive = !this.isActive;
                },
                // 数组语法的切换
                shuzu: function() {
                    this.activeClass = '';
                }
            }
        })
    </script>
</body>

</html>

遇见即是缘分,不吝烦请赐教

这是一个“普通文本型”问题。 Vue样式绑定通过 `:class` 和 `:style` 实现动态控制元素的 CSS,支持表达式、对象和数组语法。 --- ### 1. 类名绑定(`:class`) #### 对象语法 ```html <div :class="{ active: isActive, 'text-danger': hasError }"></div> ``` - 当 `isActive` 为真时,添加类 `active`; - 当 `hasError` 为真时,添加类 `text-danger`。 #### 数组语法 ```html <div :class="[activeClass, errorClass]"></div> ``` ```js data() { return { activeClass: 'active', errorClass: 'text-danger' } } ``` #### 混合使用 ```html <div :class="[{ active: isActive }, 'static']"></div> ``` --- ### 2. 内联样式绑定(`:style`) #### 对象语法 ```html <div :style="{ color: textColor, fontSize: fontSize + 'px' }"></div> ``` #### 数组语法(多个样式对象) ```html <div :style="[baseStyles, overridingStyles]"></div> ``` #### 动态属性 ```html <div :style="[isBold ? { fontWeight: 'bold' } : {}]"></div> ``` > ✅ Vue 会自动添加浏览器前缀(如 `-webkit-`) --- ### 3. 注意事项 - 使用驼峰命名(`fontSize`)或引号包裹短横线命名(`'font-size'`)。 - 静态样式建议写在 `<style>` 中,仅动态部分用 `:style`。 - 支持 SFC(单文件组件)中的 scoped 样式: ```vue <style scoped> .example { color: red; } </style> ``` 编译后自动加属性选择器隔离作用域。 --- ### 知识点详解: - **响应式更新**:数据变化时,绑定样式自动更新。 - **对象与数组语法**:灵活组合条件类名和样式。 - **自动前缀**:Vue 在运行时为 CSS 属性自动添加厂商前缀。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值