随笔 - 156  文章 - 0  评论 - 3  阅读 - 10万

Vue — v-load封装 loading效果

复制代码
<template>
  <div class="app">
    <div class="box" v-load="isLoading">
      <ul>
        <li v-for="(item, index) in list" :key="index">
          {{ item.name }}
        </li>
      </ul>

    </div>

  </div>

</template>

<script>


export default {

  data() {
    return {
      list: [],
      isLoading: true
    }
  },
  created() {
    setTimeout(() => {
      this.list = [
        { name: '1111' },
        { name: '2222' },
        { name: '3333' },
        { name: '4444' }

      ]
      this.isLoading = false
    }, 3000)
  },
  methods: {

  },
  directives: {
    // 通过添加或者移除类名 实现添加移除Load效果
    load: {
      //在这个钩子函数中 设置默认状态
      inserted(el, binding) {
        binding.value ? el.classList.add('load') : el.classList.remove('load')
      },
      //这个函数用来更新类名状态
      update(el, binding) {
        binding.value ? el.classList.add('load') : el.classList.remove('load')
      }
    }
  }
}
</script>

<style>
.box {
  width: 500px;
  height: 500px;
  background-color: #f7d6d6;
  position: relative;
}

/* 1.准备一个类名,封装指令v-load 实现请求loading效果*/
.load::before {
  content: '';
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  background: #000 url('./assets/image/load.gif') no-repeat center;
}
</style>
复制代码

 

posted on   萬事順意  阅读(129)  评论(0编辑  收藏  举报
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示