• 对象语法
  • 数组语法

一、对象语法

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. 使用git send-email发送邮件时报错: Unable to initialize SMTP properly怎么处理?

    答: 配置~/.gitconfig中的smtpserver   需往~/.gitconfig中添加如下内容: [sendemail] smtpserver = <stmp_server_name ...

  2. C++ STL——C++容器的共性和相关概念

    目录 一 STL容器共性机制 二 STL容器的使用场合 三 函数对象 四 谓词 五 内建函数对象 六 函数对象适配器 注:原创不易,转载请务必注明原作者和出处,感谢支持! 注:内容来自某培训课程,不一 ...

  3. LC 926. Flip String to Monotone Increasing

    A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), ...

  4. 网站title,meta,description如何设置,长度大小多少合适!

    转自:http://www.os1010.com/archives/1682 如 何 把 握 html 网 页 中 的 meta 标 签 对于高级的搜索引擎来说,html 的meta 标签并不是什么新 ...

  5. div固定在浏览器的最上方,不随滚动条滚动

    #topDIV { position: fixed; ; ; width: 100%; height: 35px; border-bottom: 1px solid #eee; background- ...

  6. unity2d教程

    https://segmentfault.com/a/1190000003965359

  7. Java之Apache Tomcat教程[归档]

    前言 笔记归档类博文. 本博文地址:Java之Apache Tomcat教程[归档] 未经同意或授权便复制粘贴全文原文!!!!盗文实在可耻!!!贴一个臭不要脸的:易学教程↓↓↓ Step1:安装JDK ...

  8. c++ 调试信息输出

    1. 把打印信息输出到指定的文件里. #include <stdio.h> #include <stdlib.h> freopen("log.txt", & ...

  9. 20190925 - macOS 的包管理工具

    众所周知,macOS 的包管理工具有 MacPorts 和 Homebrew,后者似乎更受欢迎,但前者但包数量更多. 喜欢手冲咖啡,看到 brew 这个词有好感,但可能部分由于网络的问题,部分因为 b ...

  10. 【CSS】如何在一个页面中引入样式css

    CSS(Cascading Style Sheet)又叫层叠样式表.是我们学习前端必不可少的一门语言,学习它其实就是为了学会如何去更改页面标签的样式.目前使用最广的是css3,但同样的,他是从css2 ...