javascript - 如何设置页面之间转换的加载图标?

标签 javascript php jquery html loading

$(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();
});

关于javascript - 如何设置页面之间转换的加载图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29028459/

相关文章:

javascript - 从 JavaScript 中的数组中删除双引号

javascript - gulp-clean-css 不能在一个特定的目录中工作?

javascript - Symfony2::加载自定义表单类型时添加 Assets

php - FIND_IN_SET 在 codeigniter 的查询中自动添加 IS NULL 并且还需要在我的查询中添加括号以进行分组

php - 防止Mp3在浏览器PHP中播放

javascript - 如何用 jquery 清除文本区域?

javascript - 如何在 React 中的组件之间移动

javascript - 如何正确实现 jQuery Sifr 插件?

javascript - 添加 n 删除表中具有元素唯一 id 的行

javascript - 单击事件未在子 div 中捕获