zoukankan      html  css  js  c++  java
  • vue样式绑定

    可以用v-bind绑定,只需要通过表达式计算出字符串结果即可。

    1.绑定css   v-bind:class

    对象语法

    <div v-bind:class="{ active: isActive }"></div>
    <!-- 可以与普通的css共存 -->
    <div
      class="static"
      v-bind:class="{ active: isActive, 'text-danger': hasError }"
    ></div>

    可以绑定一个对象的计算属性

    <div v-bind:class="classObject"></div>
    data: {
      isActive: true,
      error: null
    },
    computed: {
      classObject: function () {
        return {
          active: this.isActive && !this.error,
          'text-danger': this.error && this.error.type === 'fatal'
        }
      }
    }

    绑定一个数组,数组语法

    <div v-bind:class="[activeClass, errorClass]"></div>
    <!-- 三元表达式 -->
    <div v-bind:class="[isActive ? activeClass : '', errorClass]"></div>
    <!-- 数组里面还可以使用对象 -->
    <div v-bind:class="[{ active: isActive }, errorClass]"></div>
    data: {
      activeClass: 'active',
      errorClass: 'text-danger'
    }

    绑定到该组件的根元素上

    <my-component class="baz boo"></my-component>
    <!-- 组件 -->
    Vue.component('my-component', {
      template: '<p class="foo bar">Hi</p>'
    })
    <!-- 渲染结果结果 -->
    <p class="foo bar baz boo">Hi</p>

    2.绑定内联样式    v-bind:style

    用法同上,css属性名采用驼峰命名或者短线分隔(要加引号)

    <div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>

    <!-- 还可以绑定 -->

  • 相关阅读:
    go包初始化顺序
    go map
    go包管理
    C++ 线程池
    RAFT共识算法笔记
    最大子序列和
    常见网络攻击及其防御
    go常用标准库功能
    using代替typedef
    typename和class的区别
  • 原文地址:https://www.cnblogs.com/zsj-02-14/p/10482843.html
Copyright © 2011-2022 走看看