发表于: 2020-07-21 21:33:47

1 2109


今天完成的事情:今天写了vue的样式绑定
明天计划的事情:继续后续的任务
遇到的问题:实际操作不多需要更多的练习
收获:

Vue.js 样式绑定

Vue.js class

class 与 style 是 HTML 元素的属性,用于设置元素的样式

Vue.js v-bind 在处理 class 和 style 时, 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。

class 属性绑定

我们可以为 v-bind:class 设置一个对象,从而动态的切换 class:

<html>

<head>
  <meta charset="utf-8">
  <title>Vue 测试实例</title>
  <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>

<style>
  .active {
    width100px;
    height100px;
    backgroundgreen;
  }
</style>
</head>

<body>
  <div id="app">
    <div v-bind:class="{ 'active': isActive }"></div>
  </div>

  <script>
    new Vue({
      el: '#app',
      data: {
        isActive: true
      }
    })
  </script>
  <script src="./test.js"></script>
</body>

</html>

结果


以上实例 div class 为:

<div class="active"></div>

也可以在对象中传入更多属性用来动态切换多个 class 。

<html>

<head>
  <meta charset="utf-8">
  <title>Vue 测试实例</title>
  <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>

<style>
  .active {
    width100px;
    height100px;
    backgroundgreen;
  }

  .text-danger {
    backgroundred;
  }
</style>
</head>

<body>
  <div id="app">
    <div class="static" v-bind:class="{ 'active': isActive, 'text-danger': hasError }">
    </div>
  </div>

  <script>
    new Vue({
      el: '#app',
      data: {
        isActive: true,
        hasError: true
      }
    })
  </script>
  <script src="./test.js"></script>
</body>

</html>

结果

也可以直接绑定数据里的一个对象:

<html>

<head>
  <meta charset="utf-8">
  <title>Vue 测试实例</title>
  <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>

<style>
  .active {
    width100px;
    height100px;
    backgroundgreen;
  }

  .text-danger {
    backgroundred;
  }
</style>
</head>

<body>
  <div id="app">
    <div v-bind:class="classObject"></div>
  </div>

  <script>
    new Vue({
      el: '#app',
      data: {
        classObject: {
          active: true,
          'text-danger': true
        }
      }
    })
  </script>
  <script src="./test.js"></script>
</body>

</html>

效果是一样的

也可以在这里绑定返回对象的计算属性。这是一个常用且强大的模式:

<html>

<head>
  <meta charset="utf-8">
  <title>Vue 测试实例</title>
  <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>

<style>
  .base {
    width100px;
    height100px;
  }

  .active {
    backgroundgreen;
  }

  .text-danger {
    backgroundred;
  }
</style>
</head>

<body>
  <div id="app">
    <div v-bind:class="classObject"></div>
  </div>
  <script>

    new Vue({
      el: '#app',
      data: {
        isActive: true,
        error: {
          value: true,
          type: 'fatal'
        }
      },
      computed: {
        classObject: function () {
          return {
            base: true,
            active: this.isActive && !this.error.value,
            'text-danger': this.error.value && this.error.type === 'fatal',
          }
        }
      }
    })
  </script>
  <script src="./test.js"></script>
</body>

</html>

结果也是一样的,剩下的明天继续


返回列表 返回列表
评论

    分享到