vue loading动画效果
时间: 2023-10-01 07:04:56 浏览: 164
Vue中可以使用第三方库或者自己编写loading动画组件来实现loading效果。以下是一个简单的loading组件示例:
```html
<template>
<div class="loading-container" v-if="loading">
<div class="loading-wrapper">
<div class="loading-spinner"></div>
<div class="loading-text">{{ text }}</div>
</div>
</div>
</template>
<script>
export default {
props: {
loading: {
type: Boolean,
default: false
},
text: {
type: String,
default: '加载中...'
}
}
}
</script>
<style>
.loading-container {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
z-index: 9999;
display: flex;
justify-content: center;
align-items: center;
}
.loading-wrapper {
display: flex;
align-items: center;
}
.loading-spinner {
border: 4px solid rgba(255, 255, 255, 0.3);
border-top-color: #fff;
border-radius: 50%;
width: 40px;
height: 40px;
animation: spin 0.8s ease-in-out infinite;
margin-right: 10px;
}
.loading-text {
color: #fff;
font-size: 16px;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
</style>
```
使用时,只需要在需要loading效果的组件中引入该组件并传入loading和text属性即可:
```html
<template>
<div>
<loading :loading="loading" text="加载中..."></loading>
<!-- 其他内容 -->
</div>
</template>
<script>
import Loading from '@/components/Loading.vue'
export default {
components: {
Loading
},
data() {
return {
loading: false
}
},
methods: {
fetchData() {
this.loading = true
// 发送请求,获取数据
// ...
this.loading = false
}
}
}
</script>
```
阅读全文