前言

下面要介绍的很多点,在之前的课程中都多少有点接触。

兼容性问题不需要看,也就是这篇笔记最后两个点「自动添加前缀」、「多重值」。

第7节 class与style绑定 - 图1

学习目标

  • [x] 掌握 class 和 style 绑定的多种写法。

  • [x] 最佳实践:如果 class 和 style 的绑定相对比较复杂,建议使用「对象语法」+「计算属性」来绑定 class 和 style。

主要内容

第7节 class与style绑定 - 图2

参考资料

  • 「官方文档」Class 与 Style 绑定:链接
  • 「MDN」Truthy:链接
  • 「MDN」浏览器引擎前缀:链接

绑定 HTML Class

操作元素的 class 列表和内联样式是数据绑定的一个常见需求。因为它们都是 attribute,所以我们可以用 v-bind 处理它们:只需要通过表达式计算出字符串结果即可。不过,字符串拼接麻烦且易错。因此,在将 v-bind 用于 class 和 style 时,Vue.js 做了专门的增强。表达式结果的类型除了字符串之外,还可以是对象或数组。

对象语法

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

  1. <div v-bind:class="{ active: isActive }"></div>

上面的语法表示 active 这个 class 存在与否将取决于数据 property isActive 的 truthiness。

demo1

你可以在对象中传入更多字段来动态切换多个 class。此外,v-bind:class 指令也可以与普通的 class attribute 共存。当有如下模板:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>模板语法 - 绑定 class</title>
  6. </head>
  7. <body>
  8. <div id="app">
  9. <div class="static" v-bind:class="{ active: isActive, 'text-danger': hasError }">test</div>
  10. </div>
  11. <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  12. <script>
  13. const vm = new Vue({
  14. el: "#app",
  15. data: {
  16. isActive: true,
  17. hasError: false
  18. }
  19. });
  20. </script>
  21. <style>
  22. .static {
  23. font-style: italic;
  24. }
  25. .active {
  26. font-weight: 700;
  27. }
  28. .text-danger {
  29. color: red;
  30. }
  31. </style>
  32. </body>
  33. </html>

最终渲染结果:<div class="static active"></div>
image.png

isActive 或者 hasError 变化时,class 列表将相应地更新。例如,如果 hasError 的值为 true,class 列表将变为 "static active text-danger"

image.png

demo2

绑定的数据对象不必内联定义在模板里:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>模板语法 - 绑定 class</title>
  6. </head>
  7. <body>
  8. <div id="app">
  9. <div class="static" v-bind:class="classObject">test</div>
  10. </div>
  11. <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  12. <script>
  13. const vm = new Vue({
  14. el: "#app",
  15. data: {
  16. classObject: {
  17. active: true,
  18. 'text-danger': false
  19. }
  20. }
  21. });
  22. </script>
  23. <style>
  24. .static {
  25. font-style: italic;
  26. }
  27. .active {
  28. font-weight: 700;
  29. }
  30. .text-danger {
  31. color: red;
  32. }
  33. </style>
  34. </body>
  35. </html>

渲染的结果和上面一样。

demo3

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

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>模板语法 - 绑定 class</title>
  6. </head>
  7. <body>
  8. <div id="app">
  9. <div class="static" v-bind:class="classObject">test</div>
  10. </div>
  11. <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  12. <script>
  13. const vm = new Vue({
  14. el: "#app",
  15. data: {
  16. isActive: true,
  17. error: null
  18. },
  19. computed: {
  20. classObject: function () {
  21. return {
  22. active: this.isActive && !this.error,
  23. 'text-danger': this.error && this.error.type === 'fatal'
  24. }
  25. }
  26. }
  27. });
  28. </script>
  29. <style>
  30. .static {
  31. font-style: italic;
  32. }
  33. .active {
  34. font-weight: 700;
  35. }
  36. .text-danger {
  37. color: red;
  38. }
  39. </style>
  40. </body>
  41. </html>

数组语法

demo4

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

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>模板语法 - 绑定 class</title>
  6. </head>
  7. <body>
  8. <div id="app">
  9. <div v-bind:class="[activeClass, errorClass]">test</div>
  10. </div>
  11. <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  12. <script>
  13. const vm = new Vue({
  14. el: "#app",
  15. data: {
  16. activeClass: 'active',
  17. errorClass: 'text-danger'
  18. }
  19. });
  20. </script>
  21. <style>
  22. .active {
  23. font-weight: 700;
  24. }
  25. .text-danger {
  26. color: red;
  27. }
  28. </style>
  29. </body>
  30. </html>

渲染为:<div class="active text-danger"></div>
image.png
如果你也想根据条件切换列表中的 class,可以用三元表达式:

  1. <div v-bind:class="[isActive ? activeClass : '', errorClass]"></div>

这样写将始终添加 errorClass,但是只有在 isActive 是 truthy 时才添加 activeClass

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

  1. <div v-bind:class="[{ active: isActive }, errorClass]"></div>

用在组件上

当在一个自定义组件上使用 class property 时,这些 class 将被添加到该组件的根元素上面。这个元素上已经存在的 class 不会被覆盖。

例如,如果你声明了这个组件:

  1. Vue.component('my-component', {
  2. template: '<p class="foo bar">Hi</p>'
  3. })

然后在使用它的时候添加一些 class:

  1. <my-component class="baz boo"></my-component>

HTML 将被渲染为:

  1. <p class="foo bar baz boo">Hi</p>

对于带数据绑定 class 也同样适用:

  1. <my-component v-bind:class="{ active: isActive }"></my-component>

当 isActive 为 truthy 时,HTML 将被渲染成为:

  1. <p class="foo bar active">Hi</p>

绑定内联样式

对象语法

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

demo1

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>模板语法 - 绑定 style</title>
  6. </head>
  7. <body>
  8. <div id="app">
  9. <div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">test</div>
  10. </div>
  11. <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  12. <script>
  13. const vm = new Vue({
  14. el: "#app",
  15. data: {
  16. activeColor: 'red',
  17. fontSize: 30
  18. }
  19. });
  20. </script>
  21. </body>
  22. </html>

image.png
image.png

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

如果使用短横线命名的话,比如 font-size记得加上引号。最终的渲染结果是不变的。

demo2

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

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>模板语法 - 绑定 style</title>
  6. </head>
  7. <body>
  8. <div id="app">
  9. <div v-bind:style="styleObject">test</div>
  10. </div>
  11. <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  12. <script>
  13. const vm = new Vue({
  14. el: "#app",
  15. data: {
  16. styleObject: {
  17. color: 'red',
  18. fontSize: '30px'
  19. }
  20. }
  21. });
  22. </script>
  23. </body>
  24. </html>

最终结果同上。

同样的,对象语法常常结合返回对象的计算属性使用。

数组语法

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

  1. <div v-bind:style="[baseStyles, overridingStyles]"></div>

自动添加前缀

v-bind:style 使用需要添加浏览器引擎前缀的 CSS property 时,如 transform,Vue.js 会自动侦测并添加相应的前缀。

在使用 vue 框架时,样式的兼容性问题,咋们不需要关注,vue 会帮我们处理好。

多重值

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

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

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