Vue——样式绑定的几种方式

文章目录

前言

样式绑定在vue中属于一种很常见的操作。在之前博客中针对样式的绑定操作,介绍了一个指令v-bind。缩写为:xxx

vue 官网 样式绑定

往期回顾

先简单回顾下最开始绑定标签样式的操作,当时是采取指定标签的class属性作为样式的修改。

html 复制代码
<template>
  <h1>标签动态属性</h1>
  <!-- <div v-bind:class="dynamicClass"></div>
  <br/>
  <div :class="dynamicClass"></div>

  <br/>
  <div :class="!isShow?'green':'red'"></div>
  <br/> -->
  <div v-bind="dynamicMoreBind"></div>
</template>

<script >
export default{
  data(){
    return{
      dynamicClass:"appClass",
      isShow:true,
      // 同标签 多属性值绑定 可以采取封装对象的形式实现
      dynamicMoreBind:{
        id:"moreId",
        class:"appClass"
      }
    }
  }
}
</script>
<style>
.appClass{
  color:aqua;
  border: solid 1px;
  height: 10%;
  width: 20%;
}
.green{
  color: green;
  border: solid 1px;
  height: 10%;
  width: 20%;
}
.red{
  color: red;
  border: solid 1px;
  height: 10%;
  width: 20%;
}
</style>

但使用拼接字符串的方式进行复杂样式的绑定,是很容易出现问题的。因此,vue为classv-bind指令做了功能增强,除了使用字符串之外,表达式的值也可以是对象或数组。

绑定对象

v-bind指令,动态绑定元素属性的时候,可以采取如下的方式进行对象信息的绑定。

html 复制代码
<template>
    <h1>绑定样式class</h1>
    <hr>
    <h2>绑定对象</h2>
    <div :class="{active:isActive,'text-danger':hasError}">6666666</div>
</template>
<script>
export default{
    data(){
        return{
            isActive:true, // 如果为true 则使用 .active ,否则不使用
            hasError:true
        }
    }
}
</script>
<style>
    .text-danger{
        color: red;
    }
    .active{
        font-size: 50px
    }
</style>

其中,:class="{active:isActive,'text-danger':hasError}"中,对象的各个属性值的含义如下:

  • 当变量 isActive 为 true 时,则会使用 active 样式
  • 当变量 hasError 为 true 时,则会使用 text-danger 样式

效果如下所示:

若其中某个样式的值为false,显示效果如下:

绑定对象的另一种写法

上述的绑定操作中,如果出现很多的对象属性时,采取该方式依旧不能很好的阅读代码和维护。可以将对象定义在tata区中。如下所示:

html 复制代码
<template>
    <h1>绑定样式class</h1>
    <hr>
    <h2>绑定对象</h2>
    <!-- <div :class="{active:isActive,'text-danger':hasError}">6666666</div> -->

    <div :class="objClass">7777777</div>
</template>
<script>
export default{
    data(){
        return{
            isActive:false, // 如果为true 则使用 .active ,否则不使用
            hasError:true,
            objClass:{
                'active':true, // 如果为true 则使用 .active ,否则不使用
                'text-danger':true
            }
        }
    }
}
</script>
<style>
    .text-danger{
        color: red;
    }
    .active{
        font-size: 50px
    }
</style>

绑定数组

数组样式数据,绑定class。如果以其他的语言比如Java来看这个问题,其实数组也是对象的一种形式。前端代码如下所示:

html 复制代码
<template>
    <h1>绑定样式class</h1>
    <hr>
    <h2>绑定对象</h2>
    <!-- <div :class="{active:isActive,'text-danger':hasError}">6666666</div> -->

    <div :class="objClass">7777777</div>
    <h2>绑定数组</h2>
    <div :class="['active','text-danger']">数组样式数据绑定1</div>
    <div :class="arryActClass">数组样式数据绑定2</div>
</template>
<script>
export default{
    data(){
        return{
            isActive:false, // 如果为true 则使用 .active ,否则不使用
            hasError:true,
            objClass:{
                'active':true, // 如果为true 则使用 .active ,否则不使用
                'text-danger':true
            },
            arryActClass:['active','text-danger']
        }
    }
}
</script>
<style>
    .text-danger{
        color: red;
    }
    .active{
        font-size: 50px
    }
</style>

展示效果如下:

:class绑定数组类型数据的写法,这两种效果是一样的。

在数组对象的写法中,还支持三目表达式的语法,如下所示:

html 复制代码
<template>
    <h1>绑定样式class</h1>
    <hr>
    <h2>绑定对象</h2>
    <!-- <div :class="{active:isActive,'text-danger':hasError}">6666666</div> -->

    <div :class="objClass">7777777</div>
    <h2>绑定数组</h2>
    <div :class="['active','text-danger']">数组样式数据绑定1</div>
    <div :class="arryActClass">数组样式数据绑定2</div>
    <div :class="[isActive?'active text-danger':'green']">数组样式数据绑定3</div>
</template>
<script>
export default{
    data(){
        return{
            isActive:false, // 如果为true 则使用 .active ,否则不使用
            hasError:true,
            objClass:{
                'active':true, // 如果为true 则使用 .active ,否则不使用
                'text-danger':true
            },
            arryActClass:['active','text-danger']
        }
    }
}
</script>
<style>
    .text-danger{
        color: red;
    }
    .active{
        font-size: 50px
    }
    .green{
        color: green;
    }
</style>

数组与对象的嵌套

数组与对象嵌套的方式,也能进行标签样式的绑定。但这里需要注意一个细节。

只能是数组嵌套对象

主要写法如下所示:

html 复制代码
:class="['active',{green:isGreen}]"

案例如下所示:

html 复制代码
<template>
    <h1>绑定样式class</h1>
    <hr>
    <h2>绑定对象</h2>
    <!-- <div :class="{active:isActive,'text-danger':hasError}">6666666</div> -->

    <div :class="objClass">7777777</div>
    <h2>绑定数组</h2>
    <div :class="['active','text-danger']">数组样式数据绑定1</div>
    <div :class="arryActClass">数组样式数据绑定2</div>
    <div :class="[isActive?'active text-danger':'green']">数组样式数据绑定3</div>
    <h2>数组嵌套对象</h2>
    <!-- isGreen 变量值为true时,使用 green 样式 -->
    <div :class="['active',{green:isGreen}]">数组嵌套对象</div>
</template>
<script>
export default{
    data(){
        return{
            isActive:false, // 如果为true 则使用 .active ,否则不使用
            hasError:true,
            objClass:{
                'active':true, // 如果为true 则使用 .active ,否则不使用
                'text-danger':true
            },
            arryActClass:['active','text-danger'],
            isGreen:true
        }
    }
}
</script>
<style>
    .text-danger{
        color: red;
    }
    .active{
        font-size: 50px
    }
    .green{
        color: green;
    }
</style>
相关推荐
Csvn6 小时前
OpenSpec 详细使用教程
前端
之歆7 小时前
Day19_LESS 完全指南——从入门到工程实践
前端·css·less
云水一下8 小时前
HTML5 从入门到精通:实战收官——从零搭建完整静态网站,综合运用所有知识
前端·html5
不总是8 小时前
Windows 系统 Node.js 免安装版(zip)安装与配置教程(2026 最新)
前端·windows·node.js
冬奇Lab8 小时前
每日一个开源项目(第105篇):Twenty - 跳出 Salesforce 的圈套,定义现代开源 CRM
前端·后端·开源
zhangyao9403309 小时前
开发pc端时,表格的高度怎么设置才能铺满页面
前端·javascript·elementui
XinZong9 小时前
实测OpenClaw虾淘:全民工具AI时代,冷门非工具类的Skill还能出圈吗?
javascript
kjs--10 小时前
浏览器书签执行脚本
前端
烛衔溟10 小时前
TypeScript 类的类型 —— 作为类型使用
javascript·ubuntu·typescript
之歆10 小时前
Day16_JavaScript 轮播图与事件工程实战(下篇)
服务器·开发语言·前端·javascript·网络·性能优化