图片延迟加载

window.onload=window.onresize=window.onscroll=function()
{
                showImg();
 };

function getPos(obj)
{
    var l=0;
    var t=0;

    while(obj)
    {
        l+=obj.offsetLeft;
        t+=obj.offsetTop;

        obj=obj.offsetParent;
    }

    return {left: l, top: t};
}
function showImg()
{
   var aTmg=document.getElementsByTagName('img');
   var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;
   var scrollBottom=scrollTop+document.documentElement.clientHeight;

    for(var i=0;i<aTmg.length;i++)
    {
        var p=getPos(aTmg[i]);
        if(p.top<scrollBottom)
        {
            //alert(aTmg);
            aTmg[i].src=aTmg[i].getAttribute('_src');
        }
    }
}

 

你可能感兴趣的:(延迟加载)