动态绑定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. 自己动手实现java数据结构(七) AVL树

    1.AVL树介绍 前面我们已经介绍了二叉搜索树.普通的二叉搜索树在插入.删除数据时可能使得全树的数据分布不平衡,退化,导致二叉搜索树最关键的查询效率急剧降低.这也引出了平衡二叉搜索树的概念,平衡二叉搜 ...

  2. 精读JavaScript模式(三),new一个构造函数居然发生了什么?

    一.前言 上个月底,爸爸因为事故突然离世,说心里话,现在看到'去世','爸爸'这样的字眼,眼泪都会忍不住在眼眶打转,还是需要时间治愈.最近也只是零碎的看了下东西,始终沉不下心去读书,直到今天还是决定捡 ...

  3. spring-boot-2.0.3源码篇 - 国际化

    前言 针对spring boot,网上已有很多优质的系列教程,我就不再班门弄斧了(实际上是担心没别人写的好,哈哈哈!).但是还是想蹭蹭spring boot的热度,即使不考虑微服务,spring bo ...

  4. MongoDB框架Jongo的使用介绍

    1.Jongo可以用来做什么?   Jongo框架的目的是使在MongoDB中可以直接使用的查询Shell可以直接在Java中使用.在官网首页有一个非常简洁的例子:   SHELL:这种查询方式是Mo ...

  5. 前端MVC Vue2学习总结(八)——Vue Router路由、Vuex状态管理、Element-UI

    一.Vue Router路由 二.Vuex状态管理 三.Element-UI Element-UI是饿了么前端团队推出的一款基于Vue.js 2.0 的桌面端UI框架,手机端有对应框架是 Mint U ...

  6. unsafe关键字

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.L ...

  7. Oracle入门《Oracle介绍》第一章1-1

    1.Oracle 简介 a.对象关系型的数据库管理系统 (ORDBMS) b.在管理信息系统.企业数据处理.因特网及电子商务等领域使用非常广泛 c.在数据安全性与数据完整性控制方面性能优越 d.跨操作 ...

  8. 【Core】创建简单的Core MVC项目

    创建项目: 首先:打开vs选中新建项目- >选中.NET Core - >ASP.NET Core Web应用程序: 然后:在选择web应用程序,注意上面要选中.net Core 别选错了 ...

  9. Cylinder Candy(积分)

    Cylinder Candy Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Edward the confectioner is ...

  10. 【读书笔记】iOS-优化内存

    imageNamed:方法创建UIImage对象,这些对象不再使用的时候 会放到应用的默认自动回收池中,而不是当前的事件循环的自动回收池中,这样的对象占用的内存只有在应用结束的时候 才会回收.如果用这 ...