Javascript预加载回调

时间:2013-06-17 12:26:01

标签: javascript html css

首先,我知道很少的javascript。 我需要一个基本的预加载器来从我的网站加载一些沉重的图像,这是我找到的脚本:

<div class="hidden">
            <script type="text/javascript">
                <!--//--><![CDATA[//><!--
                    var images = new Array()
                    function preload() {
                        for (i = 0; i < preload.arguments.length; i++) {
                            images[i] = new Image()
                            images[i].src = preload.arguments[i]
                        }
                    }
                    preload(
                        "img/artistes/01/01.jpg",
                        "img/artistes/01/02.jpg",
                        "img/artistes/01/03.png",
                        "img/artistes/01/04.jpg",
                        "img/artistes/01/05.jpg",
                        "img/artistes/01/06.png",
                        "img/artistes/01/07.jpg",
                        "img/artistes/01/08.jpg",
                        "img/artistes/01/09.jpg",
                        "img/artistes/01/10.jpg",
                        "img/artistes/01/11.jpg",
                        "img/artistes/01/12.jpg",
                        "img/artistes/01/13.jpg",
                        "img/artistes/01/14.jpg",
                        "img/artistes/01/15.png",
                        "img/artistes/01/16.jpg",
                        "img/artistes/01/thumbs/01.jpg",
                        "img/artistes/01/thumbs/02.jpg"
                    )
                //--><!]]>
            </script>
        </div>

我想知道他们是否只是在加载所有这些图片后删除此div的方法?

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

对于jQuery,您可以在页面的头部使用此代码,

function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
    });
}

// Usage:

preload([
    "img/artistes/01/01.jpg",
    "img/artistes/01/02.jpg",             
    "img/artistes/01/03.png",   
    "img/artistes/01/04.jpg",    
    "img/artistes/01/05.jpg",
    "img/artistes/01/06.png",
    "img/artistes/01/07.jpg",
    "img/artistes/01/08.jpg",
    "img/artistes/01/09.jpg",
    "img/artistes/01/10.jpg",
    "img/artistes/01/11.jpg",
    "img/artistes/01/12.jpg",
    "img/artistes/01/13.jpg",
    "img/artistes/01/14.jpg",
    "img/artistes/01/15.png",
    "img/artistes/01/16.jpg",
    "img/artistes/01/thumbs/01.jpg",
    "img/artistes/01/thumbs/02.jpg"
]);

注意:您不需要隐藏或移除任何内容,任何人都看不到。