Vue 动态赋值 class

Vue 动态赋值 class

Vue 在操作 DOM 元素的 class 属性时,有以下多种方法

更多精彩

比较通用的是否赋值方式

  1. 通过以下 :class="{show: show}" 的方式可以决定该元素是否拥有名称为 show 的 class
    • data() 中的 show 属性为 true ,则赋予 show class ,否则不赋予
<template>
    <div class="watch-mooc-panel" :class="{show: show}"></div>
</template>

<script type="text/ecmascript-6">
  export default {
    name: 'mooc',
    data() {
      return {
        show: false
      }
    }
  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
    .watch-mooc-panel
      width 100%
      height 100%
      position absolute
      transition background-color .3s
      &.show
        background-color #000000
</style>

使用三目运算符的方式

  1. 注意使用三木运算符添加的属性语法和上述方式存在明显区别
<template>
    <div class="watch-mooc-panel" :class="show ? 'show' : 'hide'"></div>
</template>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值