图片预加载

function importAll (r) {
  return r.keys().map(r)
}
loadImage (url) {
      var img = new window.Image()
      img.onload = img.onerror = () => {
        this.loaded++
        this.checkLoadComplete()
      }
      img.src = url
    },
checkLoadComplete () {
      this.percent = Math.floor(this.loaded / this.total * 100)
      if (this.percent > 100) {
        this.percent = 100
      }
      if (this.loaded === this.total) {
        this.loadComplete()
      }
    },
loadComplete () {
      this.$refs.enterVideo.play()
      setTimeout(() => {
        this.moveIn()
      }, 2500)
    },

startLoad () {
      let imgs = importAll(require.context('@/img/pc/index/', false, /\.(png|jpe?g|svg)$/))
      this.total = imgs.length
      this.loaded = 0
      for (let i = 0; i < this.total; i++) {
        this.loadImage(imgs[i])
      }
    },

你可能感兴趣的:(图片预加载)