当前位置:首页 > VUE

vue骨架屏实现

2026-03-09 19:54:56VUE

Vue 骨架屏实现方法

使用专用库(如vue-skeleton-webpack-plugin)

安装插件依赖:

npm install vue-skeleton-webpack-plugin --save-dev

创建骨架屏组件(Skeleton.vue):

<template>
  <div class="skeleton">
    <div class="skeleton-head"></div>
    <div class="skeleton-body">
      <div class="skeleton-title"></div>
      <div class="skeleton-content"></div>
    </div>
  </div>
</template>

<style scoped>
.skeleton {
  padding: 15px;
}
.skeleton-head {
  height: 40px;
  background: #f2f2f2;
}
.skeleton-title {
  height: 20px;
  margin-top: 10px;
  background: #f2f2f2;
}
.skeleton-content {
  height: 60px;
  margin-top: 10px;
  background: #f2f2f2;
}
</style>

配置webpack:

// vue.config.js
const SkeletonWebpackPlugin = require('vue-skeleton-webpack-plugin')

module.exports = {
  configureWebpack: {
    plugins: [
      new SkeletonWebpackPlugin({
        webpackConfig: {
          entry: require.resolve('./src/skeleton.entry.js')
        },
        minimize: true,
        quiet: true
      })
    ]
  }
}

纯CSS实现方案

通过v-if控制显示状态:

<template>
  <div>
    <div v-if="loading" class="skeleton">
      <!-- 骨架屏结构 -->
    </div>
    <div v-else>
      <!-- 实际内容 -->
    </div>
  </div>
</template>

<style>
.skeleton-item {
  background: linear-gradient(90deg, #f2f2f2 25%, #e6e6e6 37%, #f2f2f2 63%);
  background-size: 400% 100%;
  animation: skeleton-loading 1.4s ease infinite;
}

@keyframes skeleton-loading {
  0% { background-position: 100% 50% }
  100% { background-position: 0 50% }
}
</style>

基于路由的动态骨架屏

创建路由对应骨架组件:

// router.js
const routes = [
  {
    path: '/product',
    component: () => import('@/views/Product.vue'),
    meta: {
      skeleton: () => import('@/components/ProductSkeleton.vue')
    }
  }
]

路由守卫中处理:

router.beforeEach((to, from, next) => {
  if (to.meta.skeleton) {
    store.commit('SET_SKELETON', to.meta.skeleton())
  }
  next()
})

服务端渲染(SSR)方案

Nu.js中配置:

// nuxt.config.js
export default {
  loading: {
    color: '#3B8070',
    height: '2px'
  },
  loadingIndicator: {
    name: 'circle',
    color: '#3B8070',
    background: 'white'
  }
}

自定义加载组件:

<!-- components/loading.vue -->
<template>
  <div class="skeleton-wrapper">
    <content-loader :width="400" :height="150">
      <rect x="0" y="0" rx="3" ry="3" width="250" height="10" />
      <rect x="20" y="20" rx="3" ry="3" width="220" height="10" />
      <rect x="0" y="40" rx="3" ry="3" width="350" height="10" />
    </content-loader>
  </div>
</template>

关键实现要点:

  • 保持骨架屏结构与真实DOM结构一致
  • 使用CSS动画增强视觉效果
  • 合理控制骨架屏显示时机
  • 移动端需考虑响应式布局
  • 可结合vuex管理加载状态

性能优化建议:

vue骨架屏实现

  • 避免过多DOM节点
  • 使用will-change优化动画
  • 对重复骨架元素使用v-for
  • 分块加载复杂页面区域

标签: 骨架vue
分享给朋友:

相关文章

vue实现管道

vue实现管道

Vue 中实现管道(Pipe)功能 在 Vue 中可以通过过滤器(Filters)或计算属性(Computed Properties)实现类似管道的功能,将数据经过多个处理步骤后输出。 使用过滤器(…

vue 实现直播

vue 实现直播

Vue 实现直播的基本方法 在Vue中实现直播功能,通常需要结合WebRTC、RTMP或HLS等技术。以下是几种常见的实现方式: 使用WebRTC实现实时直播 WebRTC适合低延迟的实时直播场景,…

vue菜单实现

vue菜单实现

Vue 菜单实现方法 使用 Element UI 的菜单组件 Element UI 提供了现成的菜单组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用 el-menu 相关组件。…

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…

vue 实现搜索

vue 实现搜索

实现 Vue 搜索功能 在 Vue 中实现搜索功能通常涉及以下几个关键步骤: 数据绑定与输入监听 使用 v-model 双向绑定搜索输入框的值,监听用户输入: <template>…

vue实现slidetoggle

vue实现slidetoggle

Vue 实现 SlideToggle 效果 SlideToggle 是一种常见的交互效果,元素以滑动方式展开或收起。以下是几种实现方法: 使用 CSS Transition 和 v-show 通过…