博客列表 >03-Vue_样式设置

03-Vue_样式设置

 一纸荒凉* Armani
 一纸荒凉* Armani原创
2021年05月05日 15:24:211131浏览

在Vue中使用样式

使用class样式

  1. .red{
  2. color: red;
  3. }
  4. .thin{
  5. font-weight:200;
  6. }
  7. .italic{
  8. font-style: italic;
  9. }
  10. .active{
  11. letter-spacing: 0.5em;
  12. word-spacing: 0.5em;
  13. }

1. 数组数组方式

class 需要使用 v-bind 做数据绑定

  1. <h1 :class="['red', 'thin']">{{msg}}</h1>

2. 数组中使用三元表达式

  1. <h1 :class="['red', 'thin', isactive?'active':'']">{{msg}}</h1>

3. 数组中嵌套对象

  1. <h1 :class="['red', 'thin', {'active': isactive}]">{{msg}}</h1>

4. 直接使用对象

  1. <h1 :class="{red:true, italic:true, active:false, thin:true}">{{msg}}</h1>
  2. <h1 :class="classObj">{{msg}}</h1>

Vue 实例对象

  1. const vm = new Vue({
  2. el: "#app",
  3. data: {
  4. msg: '这是一个大标题!',
  5. isactive: false,
  6. classObj: {red:true, italic:true, active:false, thin:true},
  7. },
  8. });

使用内联样式style

1. 直接在元素上通过 :style 的形式,书写样式对象

  1. <h1 :style="{color: 'red', 'font-size': '40px'}">这是一个善良的H1</h1>

2. 将样式对象,定义到 data 中,并直接引用到 :style

  • 在data上定义样式:
  1. const vm = new Vue({
  2. el: "#app",
  3. data: {
  4. h1StyleObj: { color: 'red', 'font-size': '40px', 'font-weight': '200' }
  5. },
  6. });
  • 在元素中,通过属性绑定的形式,将样式对象应用到元素中:
  1. <h1 :style="h1StyleObj">这是一个善良的H1</h1>

3. 在 :style 中通过数组,引用多个 data 上的样式对象

  • 在data上定义样式:
  1. const vm = new Vue({
  2. el: "#app",
  3. data: {
  4. h1StyleObj: { color: 'red', 'font-size': '40px', 'font-weight': '200' },
  5. h1StyleObj2: { fontStyle: 'italic' }
  6. },
  7. });
  • 在元素中,通过属性绑定的形式,将样式对象应用到元素中:
  1. <h1 :style="[h1StyleObj, h1StyleObj2]">这是一个善良的H1</h1>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议