• 对象语法
  • 数组语法

一、对象语法

1.1对象语法绑定HTML Class

语法:v-bind:class="{'className1':boolean1,'className2':boolean2}"

 <style>
div{
width: 100px;
height: 100px;
}
.class1{
background-color: #ff0;
}
.class2{
background-color:#f00;
}
</style>

示例参考样式

 <div id="example" v-bind:class="{'class1':yellow,'class2':red}" v-on:click="changeColor"></div>
<script>
var vm = new Vue({
el:"#example",
data:{
yellow:true,
red:false
},
methods:{
changeColor(){
this.yellow = !this.yellow;
this.red = !this.red;
}
}
});
</script>

当点击div时,触发changeColor方法,数据的值发生变化时,class行间属性会被切换,下面时触发效果:

当你看到v-bind:class="{'class1':yellow,'class2':red}"这句代码是不是就想到了直接使用一个对象替代这个键值对的写法,这当然是可以的:

 <div id="example" v-bind:class="colorName" v-on:click="changeColor"></div>
<script>
var vm = new Vue({
el:"#example",
data:{
colorName:{
class1:true,
class2:false
}
},
methods:{
changeColor(){
this.colorName.class1 = !this.colorName.class1;
this.colorName.class2 = !this.colorName.class2;
}
}
});
</script>

这两种写法前一种是空间复杂度大一点,后一种是时间复杂度大一点,怎么应用看具体需求吧。

1.2对象语法绑定HTML Style

语法:v-bind:style="{styleProp1:value1,styleProp2:value2}"

将样式属性名和属性值以键值对的形式写入对象,属性名采用驼峰书写模式。

 <div id="example" v-bind:style="{width:widthVal,height:heightVal,backgroundColor:backColorVal}" v-on:click="changeColor"></div>
<script>
var vm = new Vue({
el:"#example",
data:{
widthVal:'100px',
heightVal:'100px',
backColorVal:'#ff0'
},
methods:{
changeColor(){
this.backColorVal = this.backColorVal == '#ff0' ? '#f00' : '#ff0';
}
}
});
</script>

实现的效果与HTML Class对象语法实现的一样,HTML Style对象语法表达式转换成类名的方式就不展示了。

二、数组语法

2.1数组语法绑定HTML Class

语法:v-bind:class="[classNameKey1,classNameKey2]"

(样式参考示例1.1的样式)

 <div id="example" v-bind:class="[class1,class2]" v-on:click="changeColor"></div>
<script>
var vm = new Vue({
el:"#example",
data:{
class1:'class1',
class2:''
},
methods:{
changeColor(){
this.class1 = this.class1 == '' ? 'class1' : '';
this.class2 = this.class2 == '' ? 'class2' : '';
}
}
});
</script>

2.2数组语法实现HTML Style绑定

语法:v-bind:style="[styleObje1,styleObje2]"

 <div id="example" v-bind:style="[didiFamily, styleColo]" v-on:click="changeColor" ref="example"></div>
<script>
var vm = new Vue({
el:"#example",
data:{
didiFamily:{
width:'100px',
height:'100px'
},
styleColo:{
backgroundColor:'#ff0'
}
},
methods:{
changeColor(){
return this.styleColo.backgroundColor = this.styleColo.backgroundColor == '#ff0' ? '#f00' : '#ff0';
}
}
});
</script>

vue入门:(class与style绑定)的更多相关文章

  1. Vue入门---属性、style和class绑定方法

    一 .用对象的方法绑定class <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...

  2. Vue中class与style绑定

    gitHub地址:https://github.com/lily1010/vue_learn/tree/master/lesson07 一 用对象的方法绑定class 很简单,举个栗子: <!D ...

  3. vue基础——Class与Style绑定

    Class与Style绑定 操作元素的class列表和内联样式是数据绑定的一个常见的需求. 因为它们都是属性,所以我们可以用v-bind来处理它们:只需要通过表达式计算出字符串结果即可.不过,字符串拼 ...

  4. vue 的 Class 与 Style 绑定

    操作元素的 class 列表和内联样式是数据绑定的一个常见需求.因为它们都是属性,所以我们可以用 v-bind 处理它们:只需要通过表达式计算出字符串结果即可.不过,字符串拼接麻烦且易错.因此,在将 ...

  5. 前端框架之Vue(4)-Class与Style绑定

    操作元素的 class 列表和内联样式是数据绑定的一个常见需求.因为它们都是属性,所以我们可以用 v-bind 处理它们:只需要通过表达式计算出字符串结果即可.不过,字符串拼接麻烦且易错.因此,在将  ...

  6. vue基础---Class 与 Style 绑定

    [一]绑定HTML Class (1)对象语法 ①普通绑定class <div id="area" v-bind:class="className"> ...

  7. vue中,class与style绑定

    <template> <div> <p v-bind:class="{active:isActive,'demo':Demo}">嘿嘿</ ...

  8. Vue入门(二)之数据绑定

    Vue官网: https://cn.vuejs.org/v2/guide/forms.html#基础用法 [入门系列] (一)  http://www.cnblogs.com/gdsblog/p/78 ...

  9. Vue 入门之数据绑定

    什么是双向绑定? Vue 框架很核心的功能就是双向的数据绑定. 双向是指:HTML 标签数据 绑定到 Vue 对象,另外反方向数据也是绑定的.通俗点说就是,Vue 对象的改变会直接影响到 HTML 的 ...

  10. vue从入门到进阶:Class 与 Style 绑定(四)

    绑定 HTML Class 对象语法 ①.添加单个class: <div v-bind:class="{ active: isActive }"></div> ...

随机推荐

  1. python 牛顿迭代法

    使用牛顿迭代法求方程  在x附近的一个实根. 赋值X,即迭代初值:用初值x代入方程中计算此时的f(x)=(a * x * x * x + b * x * x + c * x + d)和f’(x)=(3 ...

  2. [Python]统计1个元素在列表中的出现次数

    使用列表自带的count方法: list.count(element) 示例:  列表a,有4个元素,其中值1出现3次 In []: a=[,,,] In []: a Out[]: [, , , ] ...

  3. 面向对语法读取mysql数据库数据例:$db->query($sql)、$result->fetch_array()

    前面我们介绍过如何使用面向对象语法连接mysql数据库,今天技术人员继续讲解如何读取数据.虽然与以前面向过程类似,但还是有些不同,需要大家用心了解. echo '面向对象语法连接数据库test db ...

  4. 扩展:向量空间模型算法(Vector Space Model)

  5. ssm整合用到的依赖jar包(不充足)

    <!--spring 的核心的jar包--><dependency> <groupId>org.springframework</groupId> &l ...

  6. java中使用MappedByteBuffer将 File类转ByteBuffer

    public static WavFile openWavFile(File file) throws IOException, WavFileException { FileChannel chan ...

  7. 配置lumen的log为daily模式

    1.首先添加服务提供者类LogServiceProvider <?php namespace App\Providers; use Illuminate\Support\ServiceProvi ...

  8. svn clearup svn cleanup failed–previous operation has not finished; run cleanup if it was int错误的解决办法

    今天svn提交,出现异常:svn cleanup failed–previous operation has not finished; run cleanup if it was interrupt ...

  9. Python3 Selenium自动化web测试 ==> 第七节 WebDriver高级应用 -- 浮动框中,单击选择某个关键字选项

    学习目的: 了解WebDriver的高级应用 正式步骤: 测试Python3代码 # -*- coding:utf-8 -*- from selenium import webdriver from ...

  10. Python 清华镜像设置

    大家在通过pip 或conda 下载一些很大的第三方库时是不是有一种等到坟头的草都三尺高了,还没下载完的感觉,而且大的第三方库长时间下载,可能会导致超时自动中断下载,感谢清华的大佬们 临时使用: pi ...