vue图片预加载

在本次开发的过程中,我遇到了一个问题,就是我的首页是多个壁纸转换,需要提前加载
先展示一下效果,再看代码,我的首页是多个壁纸之间跳转

效果图

在这里插入图片描述
在这里插入图片描述

代码

  <template>
  <div>
    <div class="loading">
      <el-progress type="dashboard" :percentage="percentage" :color="colors"></el-progress>
      <br />
      <span class="zhong">正在加载中</span>
    </div>
    <div v-show="false">
      <img src="/image/o_74911.jpg" alt />
      <img src="/image/o_770659.jpg" alt />
      <img src="/image/o_899029.jpg" alt />
      <img src="/image/sh.jpg" alt />
    </div>
    <div v-show="show">
      <nuxt-child />
    </div>
  </div>
</template>
<script>
export default {
  mounted() {
    var _this = this
    let imgs = document.querySelectorAll('img')
    this.num = imgs.length
    Array.from(imgs).forEach(item => {
      let img = new Image()
      img.onload = () => {
        this.count++
        this.percentage = Math.floor((this.count / imgs.length) * 100)
      }
      img.src = item.getAttribute('src')
    })
  },
  data() {
    return {
      count: 0,
      show: false,
      percentage: 0,
      num: 0,
      colors: [
        { color: '#f56c6c', percentage: 20 },
        { color: '#e6a23c', percentage: 40 },
        { color: '#5cb87a', percentage: 60 },
        { color: '#1989fa', percentage: 80 },
        { color: '#6f7ad3', percentage: 100 }
      ]
    }
  },
  watch: {
    count(val, oldval) {
      if (val == this.num) {
        this.show = true
      }
    }
  }
}
</script>
<style scoped>
.loading {
  height: 200px;
  width: 126px;
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  margin: auto;
}
.zhong {
  text-align: center;
  display: block;
}
</style>

注意: 进度条使用的是el-progress

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值