<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>