javascript - 用于加载图像的预加载器

标签 javascript jquery animation svg

我目前正在研究 SVG SMIL 动画,其中使用一些 .png 和 .gif 文件来简化 IE 中的动画。对于这个动画,我试图在动画及其其他内容加载之前获取预加载器。

问题是我无法让预加载器正常工作。我的 .html 页面首先被加载,然后预加载器正在启动...在预加载器中,我也使用了网络上可用的几个预加载器。但它们对我没有帮助。

脚本和 .html 文件加载时间可以通过 domContentLoaded 来计算,但对于图像而言。我不知道该怎么做。如果有人可以建议我一种方法,那就太好了。

这是我在网上找到的 preloader.js 代码:

$(document).ready(function () {
"use strict"
//indexOf support for IE8 and below. 
if (!Array.prototype.indexOf){
  Array.prototype.indexOf = function(elt /*, from*/){
    var len = this.length >>> 0;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++){
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

//bgImg for holding background images in the page & img array for images present in the document(<img src="">).
var bgImg = [], img = [], count=0, percentage = 0;

//Creating loader holder. 
$('<div id="loaderMask"><span>0%</span></div>').css({
    position:"fixed",
    top:0,
    bottom:0,
    left:0,
    right:0,
    background:'#fff'
}).appendTo('body');

//Using jQuery filter method we parse all elemnts in the page and adds background image url & images src into respective arrays.
$('*').filter(function() {

    var val = $(this).css('background-image').replace(/url\(/g,'').replace(/\)/,'').replace(/"/g,'');
    var imgVal = $(this).not('image').attr('xlink:href');

    //Getting urls of background images.
    if(val !== 'none' && !/linear-gradient/g.test(val) && bgImg.indexOf(val) === -1){
        bgImg.push(val)
    }

    //Getting src of images in the document.
    if(imgVal !== undefined && img.indexOf(imgVal) === -1){
        img.push(imgVal)
    }
});

//Merging both bg image array & img src array
var imgArray = bgImg.concat(img); 
console.log(imgArray.length);
//Adding events for all the images in the array.
$.each(imgArray, function(i,val){ 
    //Attaching load event 
    $("<image />").attr("xlink:href", val).bind("load", function () {
        console.log('val'+val);
        completeImageLoading();
    });

    //Attaching error event
    $("<image />").attr("xlink:href", val).bind("error", function () {
        imgError(this);
    });
})

//After each successful image load we will create percentage.
function completeImageLoading(){
    count++;
    percentage = Math.floor(count / imgArray.length * 100);
    console.log('percentage:'+percentage);
    $('#loaderMask').html('<span>'+percentage + '%'+'</span>');

    //When percentage is 100 we will remove loader and display page.
    if(percentage == 100){
        $('#loaderMask').html('<span>100%</span>')
        $('#loaderMask').fadeOut(function(){
            $('#loaderMask').remove()
        })
    }
}

//Error handling - When image fails to load we will remove the mask & shows the page. 
function imgError (arg) {
    $('#loaderMask').html("Image failed to load.. Loader quitting..").delay(3000).fadeOut(1000, function(){
        $('#loaderMask').remove();
    })
}

});

最佳答案

在开始执行 js 代码之前,我为确保加载我的或外部数据或图像而采取的一个小技巧是,我创建一个带有 display:none 的 div 并用我需要加载的所有标签填充它。

<body>

    <span id="loadingText">Loading...</span>

    <div style="display:none">
        <img src="pathtoimage1">
        <img src="pathtoimage2">
        <img src="pathtoimage3">
    </div>

    <script>
        window.onload = function(){
           //This gets called when all the items in that div has been loaded and cached.
           document.getElementById("loadingText").style.display = "none";
        }
    </script>

</body>

关于javascript - 用于加载图像的预加载器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25256927/

相关文章:

javascript - AnimationEnd 隐藏另一个 div

javascript - 类型错误 : infos[i] is undefined

javascript - 如何在表单中刷新页面时重新排序按钮?

javascript - 如何让元素而不是元素漂浮在主体内部

ios - CALayer 路径动画不起作用

javascript - 如何使用 jquery 或 javascript 为两张图片制作动画?

javascript - 如何为 Handlebars 模板设置默认参数?

javascript - 单击禁用按钮时添加 CSS

javascript - FormData 追加不起作用

javascript - 高度可变的div中已知高度的垂直居中图片,允许图片溢出div上下