如何设置页面之间过渡的加载图标


How to set loading icon for transition between pages?

$(window).load(function() {
    $('#loading').hide();
    $('#container').show();
});

在我所有的php文件中,我都有上面提到的用于显示加载图标的代码,直到页面加载。例如:如果我执行index.php,将显示加载图标,直到index.php完全加载。如果在重定向时将其重定向到example.php,则不会显示加载图标,其完全空白。如果它被完全重定向,则会显示加载图标,直到该页面完全加载。

预期:重定向到下一页时,同时我也需要显示加载图标。

那么如何在页面过渡之间显示加载图标?

诀窍是在卸载页面时立即启动加载图标。然后,当加载新页面时,必须立即再次显示加载图标,只有当新页面完全加载时,才能隐藏该图标。

// Show the icon immediatly when the script is called.
$('#loading').show();
// show the icon when the page is unloaded
$(window).on('beforeunload', function(event) {
  $('#loading').show();
});
// hide the icon when the page is fully loaded
$(window).on('load', function(event) {
  $('#loading').hide();
});