0

我在 jquery 中编写的这个函数是预加载的好方法吗?谢谢你

function loader(){

        imgTag('#check');

        function imgTag(whereToAppend){
            var image = 'image.jpg';

            $(whereToAppend).append('<img id="imgWrapper">');

            $('#imgWrapper').attr('src', image).hide().one('load',function(){
                $(this).fadeIn('slow');
            })
        }


    }
4

1 回答 1

1

我建议使用以下解决方案来预加载图像:

function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
        // Alternatively you could use:
        // (new Image()).src = this;
    });
}

// Usage:

preload([
    'img/imageName.jpg',
    'img/anotherOne.jpg',
    'img/blahblahblah.jpg'
]);

来源:使用 jQuery 预加载图像

于 2012-04-22T18:09:05.783 回答