vue-class与style绑定

85 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第15天,点击查看活动详情

绑定HTML class

对象语法

可以给v-bind:class传递一个对象,以动态地切换class

语法使用:

<div :class="{ active: isActive }"></div>

active这个class是否存在取决于isActive的布尔值 代码演示:

<template>
  <div id="app">
    <div :class="{'active':isActive,'text-danger': hasError}"></div>
  </div>
</template>
<script>
export default {
  name: 'App',
  data() {
    return {
      isActive: true,
      hasError: false
    }
  }
}
</script>
<style>
.active {
  border: 2px solid black;
  background-color: aqua;
  width: 200px;
  height: 200px;
}
.text-danger {
  border: 2px solid black;
  background-color: red;
  width: 200px;
  height: 200px;
}
</style>

运行结果:

image.png

打开开发者调试工具可以看出没有text-danger这个类名hasError值为false所以没有渲染这个类

绑定的数据对象如果比较复杂可以在数据属性中单独定义一个对象,然后绑定他

代码如下:

<template>
  <div id="app">
    <div :class="classObject"></div>
  </div>
</template>
<script>
export default {
  name: 'App',
  data() {
    return {
      classObject: {
        active: true,
        'text-danger': false
      }
    }
  }
}
</script>

我们也可以在这里绑定一个返回对象的计算属性。这是一个常用且强大的模式:

代码如下:

<template>
  <div id="app">
    <div :class="classObject"></div>
  </div>
</template>
<script>
export default {
  name: 'App',
  data() {
    return {
      isActive: true,
      error: null
    }
  },
  computed: {
    classObject() {
      return {
        active: this.isActive && !this.error,
        'text-danger': this.error && this.error.type === 'fatal'
      }
    }
  }
}
</script>

运行的结果和上面一样。

数组语法

我们可以把一个数组传给 :class,以应用一个 class 列表:

代码演示:

<template>
  <div id="app">
    <div :class="[ activeClass,errorClass]"></div>
  </div>
</template>
<script>
export default {
  name: 'App',
  data() {
    return {
      activeClass: 'active',
      errorClass: 'text-danger'
    }
  }
}
</script>
<style>
.active {
  border: 2px solid black;
  background-color: aqua;
  width: 200px;
  height: 200px;
}
.text-danger {
  background-color: red;
}
</style>

运行结果:

image.png

打开开发者调试工具可以看出渲染了activetext-danger这两个类名

如果你想根据条件切换列表中的 class,可以使用三元表达式: 代码如下:

<template>
  <div id="app">
    <div :class="[ isActive ? activeClass :errorClass]"></div>
  </div>
</template>
<script>
export default {
  name: 'App',
  data() {
    return {
      activeClass: 'active',
      errorClass: 'text-danger',
      isActive: true
    }
  }
}
</script>
<style>
.active {
  border: 2px solid black;
  background-color: aqua;
  width: 200px;
  height: 200px;
}
.text-danger {
  border: 2px solid black;
  background-color: red;
  width: 200px;
  height: 200px;
}
</style>

当有多个条件 class 时这样写有些繁琐。所以在数组语法中也可以使用对象语法:

<div :class="[{ active: isActive }, errorClass]"></div>

绑定内联样式

对象语法

:style 的对象语法十分直观——看着非常像 CSS,但其实是一个 JavaScript 对象。CSS 属性名可以用驼峰式 (camelCase) 或短横线分隔 (kebab-case,记得用引号括起来) 来命名

代码演示:

<template>
  <div id="app">
    <div :style="{color:activeColor,'fontSize' :fontsize+'px'}">绑定内联样式</div>
  </div>
</template>
<script>
export default {
  name: 'App',
  data() {
    return {
      activeColor: 'red',
      fontsize: 30
    }
  }
}
</script>
<style>
</style>

运行结果:

image.png

直接绑定到一个样式对象通常更好,这会让模板更清晰:

代码如下:

<template>
  <div id="app">
    <div :style="
    styleobject">绑定内联样式</div>
  </div>
</template>
<script>
export default {
  name: 'App',
  data() {
    return {
      styleobject: {
        color: 'red',
        fontSize: '30px'
      }
    }
  }
}
</script>
<style>
</style>

数组语法

:style 的数组语法可以将多个样式对象应用到同一个元素上

代码演示:

<template>
  <div id="app">
    <div :style="[baseStyles,moreStyles]">绑定内联样式</div>
  </div>
</template>
<script>
export default {
  name: 'App',
  data() {
    return {
      baseStyles: {
        border: '2px black solid'
      },
      moreStyles: {
        width: '200px',
        height: '200px',
        backgroundColor: 'aqua'
      }
    }
  }
}
</script>
<style>

image.png

自动添加前缀

我们知道css3中有一些样式属性并不被所有的浏览器所支持,Vue 将自动侦测并添加相应的前缀。Vue 是通过运行时检测来确定哪些样式的属性 是被当前浏览器支持的。如果浏览器不支持某个属性,Vue 会进行多次测试以找到支持它的前缀。

多重值

可以为 style 绑定中的属性提供一个包含多个值的数组,常用于提供多个带前缀的值,例如:

<div :style="{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }"></div>

这样写只会渲染数组中最后一个被浏览器支持的值。在本例中,如果浏览器支持不带浏览器前缀的 flexbox,那么就只会渲染 display: flex