前端 CSS 动态设置样式::class、:style 等技巧详解

打印 上一主题 下一主题

主题 953|帖子 953|积分 2859

一、:class 动态绑定类名

v-bind:class(缩写为 :class)可以动态地绑定一个或多个 CSS 类名。
1. 对象语法

通过对象语法,可以根据条件动态切换类名。
  1. <template>
  2.   <div :class="{ greenText: isActive, 'red-text': hasError }">海绵宝宝不喜欢侬,喜欢章鱼哥。</div>
  3. </template>
  4. <script>
  5. export default {
  6.   data() {
  7.     return {
  8.       isActive: true,
  9.       hasError: false,
  10.     };
  11.   },
  12. };
  13. </script>
  14. <style>
  15. .greenText {
  16.   color: green;
  17. }
  18. .red-text {
  19.   color: red;
  20. }
  21. </style>
复制代码


  • greenText:当 isActive 为 true 时,添加 greenText 类。
  • red-text:当 hasError 为 true 时,添加 red-text 类。
效果图:

2. 数组语法

通过数组语法,可以同时绑定多个类名。
  1. <template>
  2.   <div :class="[textClass, bgcClass]">海绵宝宝不喜欢侬,喜欢章鱼哥。</div>
  3. </template>
  4. <script>
  5. export default {
  6.   data() {
  7.     return {
  8.       textClass: 'greenText',
  9.       bgcClass: 'pinkBgc',
  10.     };
  11.   },
  12. };
  13. </script>
  14. <style>
  15. .greenText {
  16.   color: green;
  17. }
  18. .pinkBgc {
  19.   width: 300px;
  20.   height: 200px;
  21.   background-color: pink;
  22.   margin: 200px auto;
  23. }
  24. </style>
复制代码


  • textClass 和 bgcClass 是数据属性,它们的值会同时作为类名绑定到元素上。
效果图:

3. 联合计算属性

当类名的逻辑较为复杂时,可以使用计算属性来动态天生类名对象。
  1. <template>
  2.   <div :class="computedClass">海绵宝宝不喜欢侬,喜欢章鱼哥。</div>
  3. </template>
  4. <script>
  5. export default {
  6.   data() {
  7.     return {
  8.       isActive: true,
  9.       hasError: true
  10.     };
  11.   },
  12.   computed: {
  13.     computedClass() {
  14.       return {
  15.         greenText: this.isActive && !this.hasError,
  16.         'text-red': this.hasError
  17.       };
  18.     }
  19.   }
  20. };
  21. </script>
  22. <style>
  23. .greenText {
  24.   color: green;
  25. }
  26. .text-red{
  27.   color: red;
  28. }
  29. </style>
复制代码


  • greenText:isActive 为true并且hasError为false的时候生效;
  • text-red:hasError 为true的时候生效;
    效果图:

二、:style 动态绑定内联样式

v-bind:style(缩写为 :style)可以动态地绑定内联样式。
1. 对象语法

通过对象语法,可以直接绑定样式对象。
  1. <template>
  2.   <div :style="{ color: activeColor, fontSize: fontSize + 'px' }">海绵宝宝不喜欢侬,喜欢章鱼哥。</div>
  3. </template>
  4. <script>
  5. export default {
  6.   data() {
  7.     return {
  8.       activeColor: 'red',
  9.       fontSize: 12
  10.     };
  11.   },
  12. };
  13. </script>
复制代码


  • activeColor 和 fontSize 是数据属性,它们的值会作为样式绑定到元素上。
    效果图:

2. 数组语法

通过数组语法,可以同时绑定多个样式对象。
  1. <template>
  2.   <div :style="[styles1, styles2]">海绵宝宝不喜欢侬,喜欢章鱼哥。</div>
  3. </template>
  4. <script>
  5. export default {
  6.   data() {
  7.     return {
  8.       styles1: {
  9.         color: 'red',
  10.         fontSize: '14px'
  11.       },
  12.       styles2: {
  13.         fontWeight: 'bold',
  14.         textDecoration: 'underline'
  15.       }
  16.     };
  17.   },
  18. };
  19. </script>
复制代码


  • styles1 和 styles2 的所有样式都会绑定到元素上。
    效果图:

3. 使用三元表达式

可以在 :style 中使用三元表达式,根据条件动态设置样式值。
  1. <template>
  2.   <div :style="{ color: isActive ? 'green' : 'red' }">海绵宝宝不喜欢侬,喜欢章鱼哥。</div>
  3. </template>
  4. <script>
  5. export default {
  6.   data() {
  7.     return {
  8.       isActive: true
  9.     };
  10.   },
  11. };
  12. </script>
复制代码
效果图:

4. 使用模板字符串

可以使用模板字符串动态拼接样式值。
  1. <template>
  2.   <div :style="`color: ${isActive ? 'green' : 'red'}; font-size: ${fontSize}px`" class="demo">海绵宝宝不喜欢侬,喜欢章鱼哥。</div>
  3. </template>
  4. <script>
  5. export default {
  6.   data() {
  7.     return {
  8.       isActive: false,
  9.       fontSize: 12
  10.     };
  11.   },
  12. };
  13. </script>
复制代码
效果图:

祝各人新年快乐!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

风雨同行

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表