• vue中class样式


    一、使用class样式


    1. 数组
    <h1 :class="['red', 'thin']">这是一个邪恶的H1</h1>
     

    2. 数组中使用三元表达式
    <h1 :class="['red', 'thin', isactive?'active':'']">这是一个邪恶的H1</h1>
     

    3. 数组中嵌套对象
    <h1 :class="['red', 'thin', {'active': isactive}]">这是一个邪恶的H1</h1>
     

    4. 直接使用对象
    <h1 :class="{red:true, italic:true, active:true, thin:true}">这是一个邪恶的H1</h1>
     
    二、实例
    <style>
        .red {
          color: red;
        }
    
        .thin {
          font-weight: 200;
        }
    
        .italic {
          font-style: italic;
        }
    
        .active {
          letter-spacing: 0.5em;
        }
      </style>
    </head>
    
    <body>
      <div id="app">
        <h1 class="red thin">这是一个很大很大的H1,大到你无法想象!!!</h1>
    
        <!-- 第一种使用方式,直接传递一个数组,注意: 这里的 class 需要使用  v-bind 做数据绑定 -->
        <h1 :class="['thin', 'italic']">这是一个很大很大的H1,大到你无法想象!!!</h1>
    
        <!-- 在数组中使用三元表达式 -->
        <h1 :class="['thin', 'italic', flag?'active':'']">这是一个很大很大的H1,大到你无法想象!!!</h1>
    
        <!-- 在数组中使用 对象来代替三元表达式,提高代码的可读性 -->
        <h1 :class="['thin', 'italic', {'active':flag} ]">这是一个很大很大的H1,大到你无法想象!!!</h1>
    
        <!-- 在为 class 使用 v-bind 绑定 对象的时候,对象的属性是类名,由于 对象的属性可带引号,也可不带引号,所以 这里我没写引号;  属性的值 是一个标识符 -->
        <h1 :class="classObj">这是一个很大很大的H1,大到你无法想象!!!</h1>
    
    
      </div>
    
      <script>
        // 创建 Vue 实例,得到 ViewModel
        var vm = new Vue({
          el: '#app',
          data: {
            flag: true,
            classObj: { red: true, thin: true, italic: false, active: false }
          },
          methods: {}
        });
      </script>
    </body>
  • 相关阅读:
    mysql查询重复
    JS全局屏蔽回车事件
    java判断某个字符串包含某个字符串的个数
    给Eclipse提速的7个技巧(转)
    Mysql中将查询出来的多列的值用逗号拼接
    模仿淘宝手机号码输入框
    浏览器的默认样式
    GUBE---一丝
    学习CSS布局
    CSS 居中大全
  • 原文地址:https://www.cnblogs.com/wangyuxue/p/11791242.html
Copyright © 2020-2023  润新知