预加载重型GIF图像

发布于 2025-02-12 17:00:32 字数 979 浏览 1 评论 0原文

我有一个重型过程,需要几秒钟才能完成。

这就是为什么我添加了一个动画加载页面以使等待可接受。

<style>
   .modal_gif {
      display:    none;
      position:   fixed;
      z-index:    1000;
      top:        0;
      left:       0;
         height:     100%;
         width:      100%;
         background: rgba( 255, 255, 255, .8 ) 
         url('static/loading.gif') 
         50% 50% 
         no-repeat;
         background-size: 80%, auto;
        }
    body.loading .modal_gif {
         overflow: hidden;   
         }
         body.loading .modal_gif {
         display: block;
         }
        
 </style>
<script>
       $(document).on({
          ajaxStart: function() { $body.addClass("loading");    },
          ajaxStop: function() { $body.removeClass("loading"); }    
    });
</script>

但是,GIF重约1MB,在第一个请求后未显示:屏幕上只有半透明的面纱。

第二个和以下请求运行良好,这使我认为这是一个GIF加载问题。

我希望在要求时始终显示动画加载GIF,即使是第一个请求也是如此。

您知道如何将gif动画文件预加载为“隐藏”并为第一个请求显示?

I have a heavy process that requires several seconds to complete.

That's why I've added an animated loading page to make the wait acceptable.

<style>
   .modal_gif {
      display:    none;
      position:   fixed;
      z-index:    1000;
      top:        0;
      left:       0;
         height:     100%;
         width:      100%;
         background: rgba( 255, 255, 255, .8 ) 
         url('static/loading.gif') 
         50% 50% 
         no-repeat;
         background-size: 80%, auto;
        }
    body.loading .modal_gif {
         overflow: hidden;   
         }
         body.loading .modal_gif {
         display: block;
         }
        
 </style>
<script>
       $(document).on({
          ajaxStart: function() { $body.addClass("loading");    },
          ajaxStop: function() { $body.removeClass("loading"); }    
    });
</script>

However, the gif weigh about 1MB and it is not displayed after the first request: there is only a semi-transparent veil on the screen.

The second and following requests are working well, which makes me think that it is a gif loading problem.

I'd like the animated loading gif to be always displayed when requested, even for the first request.

Do you know how to preload a gif animated file as hidden and display it even for the first request?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水般的淡然安静女子 2025-02-19 17:00:32

您可以尝试使用虚拟img标签(隐藏可见性,而不显示无)预加载它。如果我们这样做,您就可以首先:

.modal_gif {
  visibility: hidden;
  display: block;
  pointer-events: none;
}

body.loading .modal_gif {
  visibility: visible;
  overflow: hidden;
}

甚至使用不透明度0或不透明度1播放,这里的密钥是指针 - 事件:无;,因此可以单击图像。

You can try preloading it using a dummy img tag (with visibility hidden, not display none). If we're at it, you can in the first place:

.modal_gif {
  visibility: hidden;
  display: block;
  pointer-events: none;
}

body.loading .modal_gif {
  visibility: visible;
  overflow: hidden;
}

or even play with opacity 0 or opacity 1, the key here is pointer-events: none; so the image is clickable through.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文