美文网首页
绑定css 新操作

绑定css 新操作

作者: 0说 | 来源:发表于2020-12-31 15:28 被阅读0次
    <template>
      <div class="box" :style="styleVar">
      </div>
    </template>
    <script>
    export default {
      props: {
        height: {
          type: Number,
          default: 54,
        },
      },
      computed: {
        styleVar() {
          return {
            '--box-height': this.height + 'px'
          }
        }
      },
    }
    </script>
    <style scoped>
    .box {
      height: var(--box-height);
    }
    </style>
    

    这样我们就在vue中实现了在样式里使用js变量的方法:
    及通过css定义变量的方式,将变量在行内注入,然后在style中使用var()获取我们在行内设置的数据即可。

    相关文章

      网友评论

          本文标题:绑定css 新操作

          本文链接:https://www.haomeiwen.com/subject/hxxroktx.html