计算首屏加载完毕的时间

function getOffsetTop(ele) {
        var offsetTop = ele.offsetTop;
        if (ele.offsetParent !== null) {
            offsetTop += getOffsetTop(ele.offsetParent);
        }
        return offsetTop;
    }

    var windowHeight = window.innerHeight,
        firstScreenImgs = [],
        ary = [],
        allImgLoaded = false,
        allImg = document.getElementsByTagName('img');

    for(var i= 0,len=allImg.length;i<len;i++){//筛选出首屏区域内的图片
        var offsetTop = getOffsetTop(allImg[i]);
        if(offsetTop < windowHeight){//首屏区域内
            firstScreenImgs.push(allImg[i]);
        }
    }
    var len = firstScreenImgs.length;

    for(var i=0;i<firstScreenImgs.length;i++){
        (function(j){
          if(firstScreenImgs[j].complete){
              len--;
          }
          else{
            firstScreenImgs[j].onload = function(){
                len--;
                ary.push({index:j,time:parseInt(+new Date)});
                if(len == 0){
                    window.firstScreenEndTime = 0;
                    for(var k in ary){
                        if(ary[k].time > firstScreenEndTime){
                            firstScreenEndTime = ary[k].time;
                        }
                    }
                }
            }
          }
        })(i)
    }

 

posted @ 2015-08-06 19:56  Joy Ho  阅读(579)  评论(0编辑  收藏  举报