Vue——Class和Style绑定

矫情吗;* 2021-09-14 12:28 571阅读 0赞

Class 与 Style 绑定

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

绑定HTML class

#对象语法

我们可以给 v-bind:class 一个对象,以实现动态的切换class:

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

上面的语法表示 active 这个class是否起作用决定于后面紧跟的数据属性 isActive 的真值,当值为true的时候存在,反之不存在。例子如下:

案例1

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>绑定HTML Class测试</title>
  6. <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
  7. <style>
  8. .active {
  9. background: green;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <div id="app">
  15. <div v-bind:class="{ active: isActive }">我现在有颜色了</div>
  16. </div>
  17. <script>
  18. new Vue({
  19. el: '#app',
  20. data: {
  21. isActive: true
  22. }
  23. })
  24. </script>
  25. </body>
  26. </html>

20171221100626448
当然,也可以在对象中传入更多属性来动态切换多个class。此外,v-bind:class 指令也可以和普通的class 属性共存,有以下模板:

  1. <div class="static"
  2. v-bind:class="{ active: isActive, 'text-danger': hasError }">
  3. </div>

整个案例如下:

案例2

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Vue 测试实例 - 绑定 HTML Class</title>
  6. <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
  7. <style>
  8. .active {
  9. background: green;
  10. }
  11. .text-danger {
  12. background: red;
  13. }
  14. .static{
  15. color:red;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div id="app">
  21. <div class="static"
  22. v-bind:class="{ active: isActive, 'text-danger': hasError }">
  23. you can try it
  24. </div>
  25. </div>
  26. <script>
  27. new Vue({
  28. el: '#app',
  29. data: {
  30. isActive: true,
  31. hasError: false
  32. }
  33. })
  34. </script>
  35. </body>
  36. </html>

20171221102624973
其结果渲染为:

  1. <div class="static active"></div>

如果 hasError 的值为true,class 列表将变为” static active text-danger”
我们也可以直接绑定数据里的一个对象:
案例3

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Vue 测试实例 - 绑定 HTML Class</title>
  6. <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
  7. <style>
  8. .active {
  9. background: green;
  10. }
  11. .text-danger {
  12. color: red;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="app">
  18. <div v-bind:class="classObject">you can try it</div>
  19. </div>
  20. <script>
  21. new Vue({
  22. el: '#app',
  23. data: {
  24. classObject: {
  25. active: true,
  26. 'text-danger': true
  27. }
  28. }
  29. })
  30. </script>
  31. </body>
  32. </html>

20171221103509383

渲染的结果和上面一样。我们也可以在这里绑定一个返回对象的计算属性 。这是一个常用且强大的模式:

案例4

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Vue 测试实例 - 绑定 HTML Class</title>
  6. <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
  7. <style>
  8. .active {
  9. background: green;
  10. }
  11. .text-danger {
  12. background: red;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="app">
  18. <div v-bind:class="classObject"> you can try it</div>
  19. </div>
  20. <script>
  21. new Vue({
  22. el: '#app',
  23. data: {
  24. isActive: true,
  25. error: true
  26. },
  27. computed: {
  28. classObject: function () {
  29. console.log(typeof this.error)
  30. return {
  31. 'active': this.isActive && this.error,
  32. 'text-danger': this.error && this.error.type === 'fatal',
  33. }
  34. }
  35. }
  36. })
  37. </script>
  38. </body>
  39. </html>

20171221104655765

#数组语法

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

案例5

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Vue 测试实例 - 绑定 HTML Class</title>
  6. <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
  7. <style>
  8. .active {
  9. background: green;
  10. }
  11. .text-danger {
  12. color: red;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="app">
  18. <div v-bind:class="[activeClass, textClass]">you can try it</div>
  19. </div>
  20. <script>
  21. new Vue({
  22. el: '#app',
  23. data: {
  24. activeClass: 'active',
  25. textClass: 'text-danger'
  26. }
  27. })
  28. </script>
  29. </body>
  30. </html>

20171221105740910
注意:定义几个class属性就要赋值几个,不然会报错!

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

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

当只有 isActive 为真值时候,activeClass才起作用。

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

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

绑定内联样式

#对象语法

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

案例6

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Vue 测试实例 - 绑定内联样式</title>
  6. <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <div v-bind:style="{ color: textColor, fontSize: fontSize + 'px' }">Hello world</div>
  11. </div>
  12. <script>
  13. new Vue({
  14. el: '#app',
  15. data: {
  16. textColor: 'green',
  17. fontSize: 30
  18. }
  19. })
  20. </script>
  21. </body>
  22. </html>

20171221110756789

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

案例7

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Vue 测试实例 - 绑定内联样式</title>
  6. <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <div v-bind:style="styleObject">Hello world</div>
  11. </div>
  12. <script>
  13. new Vue({
  14. el: '#app',
  15. data: {
  16. styleObject: {
  17. color: 'green',
  18. fontSize: '30px'
  19. }
  20. }
  21. })
  22. </script>
  23. </body>
  24. </html>

20171221111747358
#数组语法

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

案例8

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Vue 测试实例 - 绑定内联样式</title>
  6. <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <div v-bind:style="[baseStyles, overridingStyles]">Hello world</div>
  11. </div>
  12. <script>
  13. new Vue({
  14. el: '#app',
  15. data: {
  16. baseStyles: {
  17. color: 'green',
  18. fontSize: '30px'
  19. },
  20. overridingStyles: {
  21. 'font-weight': 'bold'
  22. }
  23. }
  24. })
  25. </script>
  26. </body>
  27. </html>

20171221112039574

发表评论

表情:
评论列表 (有 0 条评论,571人围观)

还没有评论,来说两句吧...

相关阅读

    相关 classstyle

      在数据绑定中,一个常见需求是,将数据与元素的 class 列表,以及元素的 style 内联样式的操作绑定在一起。由于它们都是属性(attribute),因此我们可以使用

    相关 Vue style

    在Vue项目中,如果懒得写class属性,想直接将属性写在style上(我知道这不是一个好习惯)要怎么操作呢? 官方实例 <!-- style 绑定 -->

    相关 Vue classstyle

    关于vue中class和style的绑定,我们在本篇主要是为大家说明一下该怎么使用v-bind来进行处理。 首先我们看下class的几种绑定方式: 1.对象语法 通过v