动态绑定class—概述

数据绑定(v-bind指令)一个常见需求是操作元素的 class 列表。因为class是元素的一个属性,我们可以用 v-bind 处理它们

我们只需要计算出表达式最终的字符串。不过,字符串拼接麻烦又易错。

因此,在v-bind 用于 class 时,Vue.js 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。

动态绑定class—对象语法

我们可以传给 v-bind:class 一个对象,以动态地切换 class 。v-bind:class 指令可以与普通的 class 属性共存

下面的语法表示 class ,active是否添加将取决于数据属性 isActive 是否为真值 。也可以在对象中传入更多属性用来动态切换多个 class 。

下面代码中当 isActive 或者 isFontSize变化时,class 列表将相应地更新。例如,如果 isFontSize的值为 true , class列表将变为 "bg active font-size"

需要注意的是当class的名字是xx-xxx这种带横线的类名时,需要加上单引号,不然会报错

<template>
<!--template模板只能有一个根元素-->
<div id="app">
<p class="bg" v-bind:class="{active:isActive,'font-size':isFontSize}">
例如,如果 isFontSize的值为 true , class列表将变为 "bg active font-size"
</p>
</div>
</template> <script>
export default {
name: 'app',
data () {
return {
isActive: true,
isFontSize: true
}
}
}
</script>
<style>
.active{
color: red;
}
.bg{
background-color: blue;
}
.font-size{
font-size: 18px;
}
</style>

也可以直接绑定数据里的一个对象,这种写法是将样式的class名字放在这个对象中,然后是用v-bind绑定的时候绑定这个对象名字

<template>
<!--template模板只能有一个根元素-->
<div id="app">
<p class="bg" v-bind:class="classObject">
例如,如果 isFontSize的值为 true , class列表将变为 "bg active font-size"
</p>
</div>
</template> <script>
export default {
name: 'app',
data () {
return {
classObject: {
active: true,
'font-size': true
}
}
}
}
</script>
<style>
.active{
color: red;
}
.bg{
background-color: blue;
}
.font-size{
font-size: 18px;
}
</style>

也可以在这里绑定返回对象的计算属性。这是一个常用且强大的模式

<template>
<!--template模板只能有一个根元素-->
<div id="app">
<p class="bg" v-bind:class="classObject">
例如,如果 isFontSize的值为 true , class列表将变为 "bg active font-size"
</p>
</div>
</template> <script>
export default {
name: 'app',
data () {
return {
active: true,
isActive: false
}
},
computed: {
classObject: function () {
return {
active: this.active && !this.isActive,
'font-size': this.active || this.isActive
}
}
}
}
</script>
<style>
.active{
color: red;
}
.bg{
background-color: blue;
}
.font-size{
font-size: 18px;
}
</style>

动态绑定class—数组语法

我们可以把一个数组传给 v-bind:class ,给应用一个 class 列表

<template>
<!--template模板只能有一个根元素-->
<div id="app">
<p class="bg" v-bind:class="[isActive,isFontSize]">
例如,如果 isFontSize的值为 true , class列表将变为 "bg active font-size"
</p>
</div>
</template> <script>
export default {
name: 'app',
data () {
return {
isActive: 'active',
isFontSize: 'font-size'
}
}
}
</script>
<style>
.active{
color: red;
}
.bg{
background-color: blue;
}
.font-size{
font-size: 18px;
}
</style>

根据条件切换列表中的 class ,可以用三元表达式。下面此例始终添加 font-size这个class ,但是只有在 isActive 是 true 时才添加 active这个class

<template>
<!--template模板只能有一个根元素-->
<div id="app">
<p class="bg" v-bind:class="[isActive ? activeClass : '',isFontSize]">
例如,如果 isFontSize的值为 true , class列表将变为 "bg active font-size"
</p>
</div>
</template> <script>
export default {
name: 'app',
data () {
return {
isActive: true,
activeClass: 'active',
isFontSize: 'font-size'
}
}
}
</script>
<style>
.active{
color: red;
}
.bg{
background-color: blue;
}
.font-size{
font-size: 18px;
}
</style>

当有多个条件 class 时这样写有些繁琐。可以在数组语法中使用对象语法

<template>
<!--template模板只能有一个根元素-->
<div id="app">
<p class="bg" v-bind:class="[{active:isActive},isFontSize]">
例如,如果 isFontSize的值为 true , class列表将变为 "bg active font-size"
</p>
</div>
</template> <script>
export default {
name: 'app',
data () {
return {
isActive: true,
isFontSize: 'font-size'
}
}
}
</script>
<style>
.active{
color: red;
}
.bg{
background-color: blue;
}
.font-size{
font-size: 18px;
}
</style>

动态绑定内联样式style—概述

数据绑定(v-bind指令)另一个常见需求是操作元素的内联样式。因为它是属性,我们可以用 v-bind 处理它们
我们只需要计算出表达式最终的字符串。不过,字符串拼接麻烦又易错。因此,在v-bind 用于 style 时,Vue.js 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。

动态绑定内联样式style—对象语法

v-bind:style 的对象语法十分直观——看着非常像 CSS ,其实它是一个 JavaScript 对象。 CSS 属性名可以用驼峰式(camelCase)或短横分隔命名(kebab-case)

<template>
<div id="app">
<p v-bind:style="{color:colorStyle,fontSize:fontSizeStyle+'px'}">
例如,如果isFontSize的值为true,class列表将变为 "bg active font-size"
</p>
</div>
</template> <script>
export default {
name: 'app',
data () {
return {
colorStyle: 'red',
fontSizeStyle: 20
}
}
}
</script>
<style></style>

直接绑定到一个样式对象通常更好,让模板更清晰

同样的,对象语法常常结合返回对象的计算属性使用

动态绑定内联样式style—数组语法

v-bind:style 的数组语法可以将多个样式对象应用到一个元素上


v-bind指令动态绑定class和内联样式style的更多相关文章

  1. vue 内联样式style中的background

    在我们使用vue开发的时候   有很多时候我们需要用到背景图 这个时候会直接使用 内联样式 直接把你拿到的数据拼接上去 注意  在vue中直接使用style时 花括号一定别忘记 还有就是你的url一定 ...

  2. vue 内联样式style三元表达式(动态绑定样式)

    <span v-bind:style="{'display':config.isHaveSearch ? 'block':'none'}" >动态绑定样式</sp ...

  3. vue.js中内联样式style三元表达式

    <span v-bind:style="{'display':config.isHaveSearch ? 'block':'none'}" >搜索</span&g ...

  4. Vue 内联样式

    前置说明 Vue 绑定HTML 全局属性style,可以动态地改变属性值.这里就不讲内联样式的基础了,具体轻查看官网文档 Class 与 Style 绑定. 主要分为以下两个步骤进行: v-bind ...

  5. DOM与元素节点内联样式

    获取.设置及移除单个内联 CSS 属性 每个 HTML 元素都有个 style 属性,可以用来插入针对该元素的内联 CSS 属性. <div style='background-color:bl ...

  6. 如何获取内联样式的width值

    如图,如何获取内联样式的width值 不用attr 用css这样写

  7. 内联样式自动出现,一般是js控制写入的

    内联样式自动出现,一般是js控制写入的

  8. 你真的知道css三种存在样式(外联样式、内部样式、内联样式)的区别吗?

    css样式在html中有三种存在形态: 内联样式:<div style="display: none"></div> 内部样式: <style> ...

  9. 如何修改element.style内联样式;

    如何修改element.style内联样式: 我们在写前面 web页面样式的时候,会发现有些时候,我们怎么修改 style里面的值,页面上的样式都不会修改,当你用工具查看时,会发现里面会有 eleme ...

随机推荐

  1. mybatis教程之原理剖析

    MyBatis是目前非常流行的ORM框架,功能很强大,然而其实现却比较简单.优雅.本文通过代理的方式来看下其实现 方式一:传统API方式 @Test public void add() throws ...

  2. npm包

    https://www.cnblogs.com/xinxingyu/p/5736244.html     node - glob模块讲解 https://github.com/isaacs/node- ...

  3. T-SQL:Varchar和Nvarchar区别(八)

    常规数据类型:CHAR 和 VARCHAR   Unicode 数据类型 NCHAR NVARCHAR 常规数据类型 会限制除英语之外语言    Unicode 会支持多种语言 VAR 区别 : 1. ...

  4. Sublime Text3 远程 Linux

    ctrl+shift+p快捷键 ,打开命令面板 搜索命令 Install Packages,选中,Enter 稍等几秒,重新出现了面板,搜索sftp插件,选中 Enter 显示安装成功,提示你如何使用 ...

  5. elasticsearch6.7 01.入门指南(1)

    Elasticsearch 是一个高度可扩展且开源的全文检索和分析引擎.它可以让您快速.近实时地存储.检索以及分析海量数据.它通常用作那些具有复杂搜索功能和需求的应用的底层引擎或者技术. 下面是 El ...

  6. 【Java基础】1、java中的instanceof

    instanceof是Java.php的一个二元操作符(运算符),和==,>,<是同一类东西.由于它是由字母组成的,所以也是Java的保留关键字.它的作用是判断其左边对象是否为其右边类的实 ...

  7. Rarely executed and almost empty if statement drastically reduces performance in C++

    Question: Editor's clarification: When this was originally posted, there were two issues: Test perfo ...

  8. websocket学习和群聊实现

    WebSocket协议可以实现前后端全双工通信,从而取代浪费资源的长轮询.在此协议的基础上,可以实现前后端数据.多端数据,真正的实时响应.在学习WebSocket的过程中,实现了一个简化版群聊,过程和 ...

  9. WordPress 新版本中编辑器不好用, 使用原有编辑器

    将以下代码添加到当前主题的函数模板 functions.php 文件中即可. add_filter('use_block_editor_for_post', '__return_false'); re ...

  10. JqGrid: paging int asp.net

    https://www.codeproject.com/Articles/1118363/GridView-with-Server-Side-Filtering-Sorting-and-Pa http ...