vue中實現圖片預加載及可視化百分比loading

首先根據項目需求在index.html裏面佈局loading頁面,在app.vue實現圖片預加載,圖片資源加載完畢後移除loading的div

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <title>vue中實現圖片預加載及可視化百分比loading</title>
    <style>
      .loading{
              width: 100%;
              height: 100%;
              background: #fef0d1;
              position: fixed;
              top: 0;
              left: 0;
              right: 0;
              bottom: 0;
              display: flex;
              justify-content: center;
              align-items: center;
              flex-direction: column;
              z-index: 9999999;
          }
          .lotxt{width: 50%;}
          .outbg{
            width: 5rem;
            height: 1rem;
            margin-top: 20px;
            background: url(static/images/outb.png) no-repeat;
            background-size: 100% 100%;
            display: flex;
            justify-content: center;
            align-items: center;
          }
          .inbg{
            width: 4rem;
            height: 0.5rem;
            background: url(static/images/inb.png) no-repeat;
            background-size: 100% 100%;
          }
          .bar{
            width: 4rem;
            height: 0.5rem;
            background: url(static/images/bar.png) no-repeat;
            background-size: 0 100%;
          }
    </style>
  </head>
  <body>
    <div id="Loading">
        <div class="loading">
            <img class="lotxt" src="static/images/loading.png" alt="">
            <div class="outbg">
                <div class="inbg">
                	<-- 進度條 -->
                    <div id="_bar" class="bar"></div>
                </div>
            </div>
        </div>
    </div>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body
</html>

app.vue

<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>
export default {
  name: 'App',
  data(){
    return{
      count: 0,
      percent:'',
      imglength:0
    }
  },
  mounted(){
    this.preload()
  },
  methods:{
     preload: function() {
      let imgs = [
        "static/images/bgbottom.png",
        "static/images/bgbtn.png",
        "static/images/bgtop.png",
        "static/images/center.png",
        "static/images/hdxz.png",
        "static/images/main.png",
        "static/images/pan.png",
        "static/images/point.png",
        "static/images/pop.png",
        "static/images/title.png",
        "static/images/uping.png",
        "static/images/xiong.png",
        "static/images/xpstyle.png", 
        "static/images/loading.png", 
        "static/images/bar.png",
        "static/images/inb.png", 
        "static/images/outb.png", 
        "static/images/share.jpeg", 
      ]
      this.imglength = imgs.length
      for (let img of imgs) {
        let image = new Image()
        image.src = img
        image.onload = () => {
          this.count++
           // 計算圖片加載的百分數,綁定到percent變量
          let percentNum = Math.floor(this.count / this.imglength * 100)
          this.percent = `${percentNum}%`
          document.getElementById('_bar').style.backgroundSize = `${percentNum}% 100%`
          // console.log(this.percent)
        }
      }

    },
  },
  watch: {
    count: function(val) {
      if (val === this.imglength) {
        document.body.removeChild(document.getElementById('Loading'))
      }
    }
  }
}
</script>

在這裏插入圖片描述
注意:圖片需要放在static裏面

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章