Vue 实现骨架屏(skeleton)

Vue实现骨架屏的步骤:
1.安装骨架屏插件:

npm install vue-skeleton-webpack-plugin

2.由于骨架屏插件依赖服务端渲染,再安装vue-server-renderer

npm install vue-server-renderer

3.在src>components新建skeleton目录,里面创建index.vue跟entry.skeleton.js,如下图:
在这里插入图片描述
4.在index,vue中写入骨架屏的样式,通常与首页的布局一致,例如:


<template>
    <div class="skeleton">
        <section></section>
        <div class="center">
          <ul class="dec">
              <li v-for="n in 2" :key="n">
                  <span></span>
                  <span></span>
              </li>
              <li>
                  <span></span>
              </li>
          </ul>
            <ul class="features">
                <li v-for="n in 3" :key="n">
                    <i></i>
                    <span></span>
                </li>
            </ul>
        </div>
        <div class="detail">
            <ul>
                <li v-for="n in 3" :key="n">
                    <span></span>
                    <span></span>
                </li>
            </ul>
            <p></p>
        </div>
    </div>
</template>
<style lang="less">
    @import "../less/public";
    @skeleton:#eee;
    .skeleton{
        width: 100%;
        height: 100vh;
        overflow: hidden;
        background: #fff;
        section{
            width: 100%;
            height: 205px;
            background:@skeleton;
            animation: sport .2s;
        }
        .center{
            .dec{
                li{
                    width: 100%;
                    height: 34px;
                    margin: 30px 0;
                    padding: 0 @padding20px;
                    box-sizing: border-box;
                    display: flex;
                    justify-content: space-between;
                    span{
                        width: 100px;
                        background: @skeleton;
                    }
                }
                li:first-of-type{
                    height: 44px;
                }
            }
            .features{
                display: flex;
                align-content: center;
                li{
                    flex: 1;
                    text-align: center;
                    i{
                        display: inline-block;
                        width: 60px;
                        height: 60px;
                        background: @skeleton;
                        border-radius: 50%;
                        vertical-align: middle;
                    }
                    span{
                        position: relative;
                        left:-20px;
                        top:-6px;
                        display: inline-block;
                        width: 120px;
                        height: 40px;
                        background: @skeleton;
                    }
                }
            }
        }
        .detail{
            margin-top: @padding20px;
            ul{
                li{
                    width: 100%;
                    display: flex;
                    height: 44px;
                    padding: 0 @padding20px;
                    box-sizing: border-box;
                    justify-content: space-between;
                    margin-bottom: 10px;
                    span{
                        width: 300px;
                        background: @skeleton;
                    }
                    span:first-of-type{
                        width: 150px;
                    }
                }
                li:first-of-type{
                    height: 54px;
                }
            }
            p{
                width: 60%;
                height: 54px ;
                background: @skeleton;
                margin-left: @padding20px;
                margin-top: @padding20px;
            }
        }
    }
    @keyframes sport {
        0%{background: #fff}
        100%{background: @skeleton}
    }
</style>

5.配置entry.skeleton.js的入口文件:

import Vue from 'vue'
import Skeleton from './index'
export default new Vue({
  components: {
    Skeleton
  },
  template: '<Skeleton/>'
})

6.在build文件夹中创建一个webpack.skeleton.conf.js:

'use strict';
const path = require('path')
const merge = require('webpack-merge')
const baseWebpackConfig = require('./webpack.base.conf')
const nodeExternals = require('webpack-node-externals')

const config = require('../config')
const utils = require('./utils')
const isProduction = process.env.NODE_ENV === 'production'
const sourceMapEnabled = isProduction
  ? config.build.productionSourceMap
  : config.dev.cssSourceMap

function resolve(dir) {
  return path.join(__dirname, dir)
}

let skeletonWebpackConfig = merge(baseWebpackConfig, {
  target: 'node',
  devtool: false,
  entry: {
    app: resolve('../src/components/skeleton/entry-skeleton.js')
  },
  output: Object.assign({}, baseWebpackConfig.output, {
    libraryTarget: 'commonjs2'
  }),
  externals: nodeExternals({
    whitelist: /\.css$/
  }),
  plugins: []
})

//important: enable extract-text-webpack-plugin,让颜色生效
// 重点配置
skeletonWebpackConfig.module.rules[0].options.loaders = utils.cssLoaders({
  sourceMap: sourceMapEnabled,
  extract: true
})
module.exports = skeletonWebpackConfig

7.在webpack.dev.conf.js跟webpack.pro.conf.js或者统一在webpack.base.conf.js中分别引入:

const SkeletonWebpackPlugin=require('vue-skeleton-webpack-plugin');

然后在plugins中分别导入:

    new SkeletonWebpackPlugin({
      webpackConfig: require('./webpack.skeleton.conf'),
      quiet: true,
    }),

以上为Vue项目骨架屏的全部配置过程!!!

  • 5
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vue中,可以使用第三方库vue-skeleton-webpack-plugin来实现骨架。以下是一个简单的示例: 1. 安装vue-skeleton-webpack-plugin库: ```bash npm install vue-skeleton-webpack-plugin --save-dev ``` 2. 在vue.config.js中配置插件: ```js const SkeletonWebpackPlugin = require('vue-skeleton-webpack-plugin'); module.exports = { configureWebpack: { plugins: [ new SkeletonWebpackPlugin({ webpackConfig: require('path/to/your/webpack.config'), quiet: true, minimize: true, router: { mode: 'hash', routes: [ { path: '/', skeletonId: 'skeleton-home' }, { path: '/about', skeletonId: 'skeleton-about' }, ] } }) ] } } ``` 这里配置了插件的相关选项,包括webpackConfig(指定webpack配置文件)、quiet(是否开启静默模式)、minimize(是否压缩骨架代码)和router(指定骨架对应的路由信息)等。 3. 在组件中使用骨架: ```vue <template> <div> <div v-if="$skeletonId === 'skeleton-home'"> <SkeletonHome /> </div> <div v-else> <!-- 实际内容 --> </div> </div> </template> <script> import SkeletonHome from '@/components/SkeletonHome.vue'; export default { components: { SkeletonHome } } </script> ``` 这里使用了$v-skeleton-id指令来判断当前组件是否需要显示骨架,如果需要,则渲染对应的骨架组件SkeletonHome。 在SkeletonHome组件中,可以自定义骨架的样式和内容,例如: ```vue <template> <div class="skeleton-home"> <div class="skeleton-header"></div> <div class="skeleton-content"> <div class="skeleton-item"></div> <div class="skeleton-item"></div> <div class="skeleton-item"></div> </div> </div> </template> <style scoped> .skeleton-home { background-color: #f2f2f2; border-radius: 5px; padding: 20px; } .skeleton-header { height: 50px; width: 100%; background-color: #ddd; border-radius: 5px; margin-bottom: 20px; } .skeleton-content { display: flex; flex-direction: column; } .skeleton-item { height: 20px; width: 100%; background-color: #ddd; border-radius: 5px; margin-bottom: 10px; } </style> ``` 这里的骨架和之前的示例很类似,只是把HTML和CSS放到了组件中。在实际使用中,可以根据不同的需求和布局进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值