当前位置:首页 > VUE

vue实现占位加载效果

2026-01-23 14:47:13VUE

Vue 实现占位加载效果的方法

使用骨架屏(Skeleton Screen)

骨架屏是一种常见的占位加载效果,通过模拟内容布局提升用户体验。Vue 中可以通过自定义组件实现。

<template>
  <div class="skeleton-container">
    <div class="skeleton-item" v-for="i in 5" :key="i"></div>
  </div>
</template>

<style>
.skeleton-container {
  width: 100%;
}
.skeleton-item {
  height: 20px;
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  margin-bottom: 10px;
  border-radius: 4px;
}
@keyframes shimmer {
  from { background-position: 200% 0; }
  to { background-position: -200% 0; }
}
</style>

结合 v-if 和加载状态

通过数据加载状态切换真实内容和占位符。

<template>
  <div>
    <div v-if="loading" class="placeholder">
      <!-- 占位内容 -->
    </div>
    <div v-else>
      <!-- 真实内容 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: true
    }
  },
  mounted() {
    setTimeout(() => {
      this.loading = false;
    }, 2000);
  }
}
</script>

使用第三方库

Vue 生态中有专门的库可以快速实现骨架屏效果,例如 vue-skeleton-webpack-pluginvue-content-loading

vue实现占位加载效果

安装 vue-content-loading

npm install vue-content-loading

使用示例:

vue实现占位加载效果

<template>
  <content-loader>
    <rect x="0" y="0" rx="3" ry="3" width="250" height="10" />
    <rect x="0" y="20" rx="3" ry="3" width="150" height="10" />
  </content-loader>
</template>

<script>
import { ContentLoader } from 'vue-content-loading'
export default {
  components: {
    ContentLoader
  }
}
</script>

列表数据加载占位

对于列表数据,可以结合 v-for 实现多个占位项。

<template>
  <ul>
    <li v-for="n in placeholderCount" :key="n" class="list-placeholder">
      <div class="avatar-placeholder"></div>
      <div class="text-placeholder"></div>
    </li>
  </ul>
</template>

<style>
.list-placeholder {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}
.avatar-placeholder {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #eee;
  margin-right: 15px;
}
.text-placeholder {
  width: 200px;
  height: 16px;
  background-color: #eee;
}
</style>

图片懒加载占位

图片加载时可以显示占位背景。

<template>
  <div class="image-container">
    <img 
      v-if="loaded" 
      :src="imageUrl" 
      @load="loaded = true"
    >
    <div v-else class="image-placeholder"></div>
  </div>
</template>

<style>
.image-placeholder {
  width: 100%;
  height: 200px;
  background: #f5f5f5;
}
</style>

以上方法可以根据实际需求组合使用,骨架屏动画和样式可以进一步自定义优化。

标签: 加载效果
分享给朋友:

相关文章

vue实现加载

vue实现加载

Vue 实现加载功能的方法 使用 v-if 或 v-show 控制加载状态 通过数据绑定控制加载动画的显示与隐藏。v-if 会完全移除 DOM 元素,v-show 只是切换 display 属性。…

h5如何实现vr效果

h5如何实现vr效果

使用WebXR API实现VR效果 WebXR是浏览器中实现VR/AR的核心API,支持设备姿态追踪、渲染交互等功能。需在支持WebXR的设备(如Oculus、HTC Vive)或浏览器模拟环境中运行…

vue实现效果

vue实现效果

Vue 实现常见效果的方法 数据绑定与响应式更新 Vue 的核心特性是数据绑定,通过 v-model 实现表单元素与数据的双向绑定。例如输入框与数据的实时同步: <input v-model=…

jquery页面加载

jquery页面加载

jQuery 页面加载事件 在 jQuery 中,页面加载事件通常通过 $(document).ready() 或简写的 $() 来实现。这种方式确保代码在 DOM 完全加载后执行,但无需等待图片等资…

vue实现触底加载

vue实现触底加载

触底加载的实现方法 在Vue中实现触底加载功能,可以通过监听滚动事件或使用Intersection Observer API来实现。以下是两种常见的方法: 监听滚动事件 在组件中监听滚动事件,计算…

react如何添加效果

react如何添加效果

React 添加动画效果的方法 使用 CSS 过渡动画 在 React 组件中直接通过 className 或 style 属性应用 CSS 过渡效果。定义 CSS 类包含 transition 属性…