Bootstrap

pdf.js 加载文件过程添加loading等待动画

在加载一些大文件时,页面会先出现几秒的空白,给其加一个loading效果

参考链接 pdf.js 添加自定义loading动画 - jewely - 博客园

1、在 viewer.html文件中添加

  <div id="loadingContainer">
    <div class="spinner">
      <div class="spinner-container container1">
        <div class="circle1"></div>
        <div class="circle2"></div>
        <div class="circle3"></div>
        <div class="circle4"></div>
      </div>
      <div class="spinner-container container2">
        <div class="circle1"></div>
        <div class="circle2"></div>
        <div class="circle3"></div>
        <div class="circle4"></div>
      </div>
      <div class="spinner-container container3">
        <div class="circle1"></div>
        <div class="circle2"></div>
        <div class="circle3"></div>
        <div class="circle4"></div>
      </div>
    </div>
  </div>

2、在viewer.css中添加

#loadingContainer {
  width: 100%;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.7);
  position: absolute;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  flex: 1;
  z-index: 999;
}

.spinner {
  margin: 100px auto;
  width: 20px;
  height: 20px;
  position: relative;
}

.spinner .container1>div,
.spinner .container2>div,
.spinner .container3>div {
  width: 6px;
  height: 6px;
  background-color: #409eff;
  border-radius: 100%;
  position: absolute;
  -webkit-animation: bouncedelay 1.2s infinite ease-in-out;
  animation: bouncedelay 1.2s infinite ease-in-out;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
}

.spinner .spinner-container {
  position: absolute;
  width: 100%;
  height: 100%;
}

.spinner .container2 {
  -webkit-transform: rotateZ(45deg);
  transform: rotateZ(45deg);
}

.spinner .container3 {
  -webkit-transform: rotateZ(90deg);
  transform: rotateZ(90deg);
}

.spinner .circle1 {
  top: 0;
  left: 0;
}

.spinner .circle2 {
  top: 0;
  right: 0;
}

.spinner .circle3 {
  right: 0;
  bottom: 0;
}

.spinner .circle4 {
  left: 0;
  bottom: 0;
}

.spinner .container2 .circle1 {
  -webkit-animation-delay: -1.1s;
  animation-delay: -1.1s;
}

.spinner .container3 .circle1 {
  -webkit-animation-delay: -1s;
  animation-delay: -1s;
}

.spinner .container1 .circle2 {
  -webkit-animation-delay: -0.9s;
  animation-delay: -0.9s;
}

.spinner .container2 .circle2 {
  -webkit-animation-delay: -0.8s;
  animation-delay: -0.8s;
}

.spinner .container3 .circle2 {
  -webkit-animation-delay: -0.7s;
  animation-delay: -0.7s;
}

.spinner .container1 .circle3 {
  -webkit-animation-delay: -0.6s;
  animation-delay: -0.6s;
}

.spinner .container2 .circle3 {
  -webkit-animation-delay: -0.5s;
  animation-delay: -0.5s;
}

.spinner .container3 .circle3 {
  -webkit-animation-delay: -0.4s;
  animation-delay: -0.4s;
}

.spinner .container1 .circle4 {
  -webkit-animation-delay: -0.3s;
  animation-delay: -0.3s;
}

.spinner .container2 .circle4 {
  -webkit-animation-delay: -0.2s;
  animation-delay: -0.2s;
}

.spinner .container3 .circle4 {
  -webkit-animation-delay: -0.1s;
  animation-delay: -0.1s;
}

@-webkit-keyframes bouncedelay {

  0%,
  80%,
  100% {
    -webkit-transform: scale(0);
  }

  40% {
    -webkit-transform: scale(1);
  }
}

@keyframes bouncedelay {

  0%,
  80%,
  100% {
    transform: scale(0);
    -webkit-transform: scale(0);
  }

  40% {
    transform: scale(1);
    -webkit-transform: scale(1);
  }
}

3、在view.js中  移除loading

修改前

loadingTask.onUnsupportedFeature = this.fallback.bind(this);
return loadingTask.promise.then(pdfDocument => {
this.load(pdfDocument);
},reason => {

修改后 

loadingTask.onUnsupportedFeature = this.fallback.bind(this);
return loadingTask.promise.then(pdfDocument => {
this.load(pdfDocument);
//加载完成移除loading动画
var loading = document.getElementById('loadingContainer');
loading && loading.remove();

}, reason => {

4、动画效果

文件加载中

 文件加载完成后

 

;