vue中class和style属性

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <meta charset="utf-8">
  <title>class与style 属性</title>
</head>
<style>
  .active,
  .activeClass {
    color: red;
    width: 100px;
    border: 1px solid #ccc;
    height: 100px;
  }
</style>

<body>
  <div class="app">
    <!---绑定 HTML Class----------------------------->
    <!--对象语法-->
    <div v-bind:class="{active:at}">2</div>
    <div v-bind:class="{active:at,alink:al}">3</div>
    <div v-bind:class="obj">4</div>
    <div v-bind:class="objClass">5</div>
    <!--数组语法-->
    <div v-bind:class="[activeClass, errorClass]">数组</div>
    <div v-bind:class="[isActive ? activeClass : '', errorClass]">数组</div>

    <!--用在组件上-->
    <my-component class="baz boo">组件</my-component>
    <my-component v-bind:class="{at}">组件</my-component>

    <!---绑定内联样式---------------------------->
    <div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">内联</div>
    <div v-bind:style="styleObject">直接绑定到一个样式对象</div>
    <!--数组语法-->
    <div v-bind:style="[baseStyles, overridingStyles]">内联数组语法</div>

  </div>
</body>
<script src="js/vue.js" charset="utf-8"></script>
<script type="text/javascript">

  Vue.component('my-component', {
    template: '<p class="foo bar">Hi</p>'
  })

  var vm = new Vue({
    el: '.app',
    data: {
      msg: '',
      activeClass: 'active',
      errorClass: 'text-danger',
      activeColor: 'red',
      fontSize: 50,
      at: true, //true|false
      al: false,
      err: null,
      isActive: true,

      obj: { //可做一个对象
        active: true, //true|false
        alink: false,
      },
      baseStyles: {
        color: 'red',
        'font-size': '20px'
      },
      styleObject: {
        color: 'red',
        fontSize: '13px'
      },
      overridingStyles: {
        width: '200px',
        fontSize: '63px'
      }
    },
    computed: { //计算属性
      objClass: function() { //可做一个对象方法
        return {
          active: this.at && !this.err, //为true 并且不为null
          'text-align': this.err && this.err.type === 'fatal'
        }
      }
    }

  });
</script>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑色咖啡 Ken

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值