javascript - 检测加载了 JavaScript 的页面

标签 javascript html jquery jquery-events

我有一个充满图像的投资组合页面,我想用 mask 覆盖隐藏这些图像,直到所有图像都有机会完成加载。有没有办法检测页面上所有内容的加载完成?

我想找到一种方法来做到这一点,最好是使用 jQuery 库。有什么想法吗?

最佳答案

假设您希望在删除覆盖之前完成加载的图像都具有类 .theImagesToLoad:

// count the number of images
var imageCount = $(".theImagesToLoad").length;

// init a load counter
var loadCounter = 0;
$(".theImagesToLoad").load(function() {

    // an image has loaded, increment load counter
    loadCounter++;

    // remove overlay once all images have loaded
    if(loadCounter == imageCount) {
        removeOverlay();
    }
}).each(function() {

    // make sure the `.load` event triggers
    // even when the image(s) have been cached
    // to ensure that the overlay is removed
    if(this.complete) $(this).trigger("load");
});

参见: http://api.jquery.com/load-event/

关于javascript - 检测加载了 JavaScript 的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5418477/

相关文章:

javascript - 同一元素上的两次单击处理程序发生冲突

基本遗传算法的PHP或JavaScript开源代码

javascript - JavaScript 函数中未定义字符串?

html - HTML 中多首歌曲的简单自定义播放按钮

javascript - 单击 div 元素以外的任何位置时删除该 div 元素

php - 根据高度保持纵横比

Javascript 排序功能无法正常运行

javascript - 每次我点击 jQuery.append 函数时,HTML 表单都会加倍

php - 我如何在 opencart 中使用 Bootstrap slider

javascript - 外部 JS 文件中的函数未被调用