美文网首页
第四章 v-bind以及class与style的绑定

第四章 v-bind以及class与style的绑定

作者: kzc爱吃梨 | 来源:发表于2019-10-11 01:27 被阅读0次

应用场景: DOM 元素经常会动态地绑定一些 class 类名或 style 样式

4.1 了解bind指令

—v-­bind的复习
链接的 href 属性和图片的 src 属性都被动态设置了,当数据变化时,就会重新渲染。
在数据绑定中,最常见的两个需求就是元素的样式名称 class 和内联样式 style 的动态绑定,它们也是 HTML的属性,因此可以使用v-­bind指令。我们只需要用 v­-bind计算出表达式最终的字符串就可以,不过有时候表达式的逻辑较复杂,使用字符串拼接方法较难阅读和维护,所以 Vue.js 增强了对 class 和 style 的绑定。


4.2 绑定 class 的几种方式

4.2.1 对象语法

给 v-­bind:class 设置一个对象,可以动态地切换 class,* 值对应true ,false
当 class 的表达式过长或逻辑复杂时,还可以绑定一个计算属性,这是一种很友好和常见的
用法,一般当条件多于两个时, 都可以使用 data 或 computed 效果展示

4.2.2 数组语法

当需要应用多个 class 时, 可以使用数组语法 , 给:class 绑定一个数组,应用一个 class
列表:
数组成员直接对应className--类名

  <style>
    .btnBg1 {
      background: red;
    }
    .btnBg2 {
      background: blue;
    }
    .active1 {
      background: yellow;
    }
    .active2 {
      background: green;
    }
    .bgColor {
      background: orange;
      width: 100px;
      height: 100px;
    }
    .borderColor {
      border: 10px solid grey;
    }
  </style>
  
<body>
  
  绑定class对象语法:对象的键是类名,值是布尔值 
  <div class="demo">
    <button :class={btnBg1:isRed,btnBg2:isBlue} @click='changeColor'>点击我切换颜色</button>
    
   <button :class='active' @click='activeColor'>计算属性绑定属性</button> <hr>
    绑定class数组语法:对象的键是类名,值是布尔值 <br>
    <div :class=[bgColor,borderColor]></div>
  </div>
  
  <script>
    //需求:点击一个按钮来回切换背景色
    let app = new Vue({
      el: '.demo',
      data: {
        isRed: true,
        isBlue: false,
        isActive1: true,
        isActive2: false,

        bgColor: "bgColor",
        borderColor: "borderColor"
      },
      computed: {
        active: function(){
          return {
            active1: this.isActive1,
            active2: this.isActive2
          }
        }
      },
      methods: {
        changeColor: function(){
          this.isRed = !this.isRed
          this.isBlue = !this.isBlue
        },
        activeColor: function(){
          this.isActive1 = !this.isActive1
          this.isActive2 = !this.isActive2
        }
      }
    })
  </script>

可以用三目运算实现,对象和数组混用——————看演示

  绑定class <br>
  <div id="app">
    数组和对象数的混用,第一个成员是对象,第二个成员是数组成员
    <div :class= "[{'active': isActive}, bgColor]"></div>
  </div>
  
  <script>
    let app = new Vue({
      el: '#app',
      data: {
        isActive: true,
        bgColor: 'bgClolor'
      }
    }) 
  </script>
image.png

4.3 绑定内联样式

使用 v-­bind:style (即:style ) 可以给元素绑定内联样式,方法与 :class 类似,也有对象语法和数组语法,看起来很像直接在元素上写 CSS:
注意: css 属性名称使用驼峰命名( came!Case )或短横分隔命名( kebab­case)

  • 应用多个样式对象时 , 可以使用数组语法 :在实际业务 中,style 的数组语法并不常用 , 因为往往可以写在一个对象里面 : 而较为常用 的应当是计算属性
  • 使用 :style 时, Vue .js 会自动给特殊的 css 属性名称增加前缀, 比如 transform 。
  • 无需再加前缀属性!!!
    代码展示

相关文章

  • vue重新起航(三)

    class与style绑定 用v-bind来绑定class和style 对象语法 1.class active是否...

  • Vue学习笔记[2]

    一、Class与Style绑定 1 绑定HTML class #对象语法我们可以传给 v-bind:class 一...

  • 6 动态绑定属性

    v-bind基本用法 v-bind 绑定style 动态绑定class对象语法···...

  • Vue.js教程_4

    操作元素的class列表和内联样式的数据绑定:使用v-bind和style与class结合。v-bind也可与操作...

  • Vue入门:v-bind

    本篇为Vue的基础篇,主要关于 v-bind: class与style的动态绑定。 1. 绑定 class 的几种...

  • Vue基础指令

    Vue循环指令 Vue绑定指令: v-bind:style,v-bind:class: v-model双向数据绑定...

  • Vue绑定对象和数组

    class列表和内联样式style都是属性,所以可以使用v-bind动态绑定 v-bind:class="{...

  • Vue.js从0到1:v-bind指令

    Vue.js从0到1:v-bind 指令 1. 代码演示 v-bind :绑定 : 绑定class、绑定style...

  • 绑定Class

    注意:Class 与 Style 绑定:1、使用 (v-bind:表达式结果) 进行实现2、在将 v-bind 用...

  • vue初

    style的绑定 条件渲染 列表渲染 指令总结v-bind可以绑定属性,包括class style,而已省略,使用...

网友评论

      本文标题:第四章 v-bind以及class与style的绑定

      本文链接:https://www.haomeiwen.com/subject/ssbouctx.html