图片按照指定的顺序预加载

// 图片按照设定的顺序加载
function preload(src, index) {
    index = index || 0;
    if(index >= 100) {
        return false;
    }
    if (src && src.length > index) {
        var img = new Image();
        img.onload = function() {
            preload(src, index + 1);
        }
        img.src = src[index];
    }
}

示例如下:

<div class="demo" style="width: 736px; margin: 0 auto;">

    <img class="lazy" src="images/blank.gif" data-echo="http://www.jq22.com/demo/jsloading-141022114841/images/big-1.jpg">

    <img class="lazy" src="images/blank.gif" data-echo="http://www.jq22.com/demo/jsloading-141022114841/images/big-2.jpg">

    <img class="lazy" src="images/blank.gif" data-echo="http://www.jq22.com/demo/jsloading-141022114841/images/big-3.jpg">

    <img class="lazy" src="images/blank.gif" data-echo="http://www.jq22.com/demo/jsloading-141022114841/images/big-4.jpg">

    <img class="lazy" src="images/blank.gif" data-echo="http://www.jq22.com/demo/jsloading-141022114841/images/big-5.jpg">

    <img class="lazy" src="images/blank.gif" data-echo="http://www.jq22.com/demo/jsloading-141022114841/images/big-6.jpg">

    <img class="lazy" src="images/blank.gif" data-echo="http://www.jq22.com/demo/jsloading-141022114841/images/big-7.jpg">

</div>

<script>

var src = new Array();
src[0] = 'http://www.jq22.com/demo/jsloading-141022114841/images/big-1.jpg';
src[1] = 'http://www.jq22.com/demo/jsloading-141022114841/images/big-2.jpg';
src[2] = 'http://www.jq22.com/demo/jsloading-141022114841/images/big-3.jpg';
src[3] = 'http://www.jq22.com/demo/jsloading-141022114841/images/big-4.jpg';
src[4] = 'http://www.jq22.com/demo/jsloading-141022114841/images/big-5.jpg';
src[5] = 'http://www.jq22.com/demo/jsloading-141022114841/images/big-6.jpg';
src[6] = 'http://www.jq22.com/demo/jsloading-141022114841/images/big-7.jpg';


// 图片按照设定的顺序加载
function preload(src, index) {
    index = index || 0;
    if(index >= 100) {
        return false;
    }
    if (src && src.length > index) {
        var img = new Image();
        img.onload = function() {
                preload(src, index + 1);
        }
        img.src = src[index];
    }
}
preload(src);

发表评论

邮箱地址不会被公开。 必填项已用*标注