动态绑定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. [深度学习]CNN--卷积神经网络中用1*1 卷积有什么作用

    1*1卷积过滤器 和正常的过滤器一样,唯一不同的是它的大小是1*1,没有考虑在前一层局部信息之间的关系.最早出现在 Network In Network的论文中 ,使用1*1卷积是想加深加宽网络结构 ...

  2. linux nohup

    nohup RAILS_ENV=production bundle exec XXXX & nohup RAILS_ENV=production bundle exec XXXX >/d ...

  3. 简要描述 JavaScript 中定义函数的几种方式

    JavaScript 中,有三种定义函数的方式: 1.函数语句:即使用 function 关键字显式定义函数.如: function f(x){ return x+1; }  2.函数定义表达式:也称 ...

  4. ExtJS中xtype一览

    基本组件: xtype Class 描述 button Ext.Button 按钮 splitbutton Ext.SplitButton 带下拉菜单的按钮 cycle Ext.CycleButton ...

  5. java中的重载(overload)和重写(override)区别

    方法重载(overload): 方法重载就是在一个类中可以创建多个方法,它们具有相同的名字,但是具有不同的参数和不同定义,调用方法时通过传递给他们的不同参数个数和参数列表决定具体使用     哪   ...

  6. csharp: Setting the value of properties reflection

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. Asp.Net初学小结

    第一章   1.搭建Asp.net开发环境   1).net FrameWork(VS) 2)IIS(xp:5.1,2003:6.0,vista:70,win7:7.5) C:\Windows\Mic ...

  8. Python入门到精通学习书籍推荐!

    1.Python基础教程(第2版 修订版)<Python基础教程(第2版修订版)>包括Python程序设计的方方面面,内容涉及的范围较广,既能为初学者夯实基础,又能帮助程序员提升技能,适合 ...

  9. [PHP] 算法-数组重复数字统计的PHP实现

    在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为7的数组{ ...

  10. JQuery实现省市区的三级联动

    JQuery实现省市区的三级联动 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "h ...