使用javascript缓存页面加载之前从数组缓存动态图像 [英] Cache dynamic images from array before page loads using javascript

查看:188
本文介绍了使用javascript缓存页面加载之前从数组缓存动态图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作4个滑动画廊,但是我必须在画面形式显示图像之前,预先加载(高速缓存)所有的图像。我一直在尝试使用jPreLoader v2 - http:/ /www.inwebson.com/jquery/jpreloader-a-preloading-screen-to-preload-images 但没有运气。

I am trying to make 4 sliding galleries but I need to preload (cache) all images behind a splash screen before displaying the images in gallery form. I have been trying to use "jPreLoader v2 - http://www.inwebson.com/jquery/jpreloader-a-preloading-screen-to-preload-images" but with no luck.

下面的代码是我如何将每个图库目录中的所有图像预加载到jpreloader后面的单个库中,然后一旦加载完成,将一次删除整个图库并显示每个图库。

The code below is how I have been trying to preload all images from each gallery directory on to a single gallery behind jpreloader then once load complete remove the whole gallery and display each gallery at a time.

var pictures =  [
    "1.jpg",
    "2.jpg",
    "3.jpg",
    "4.jpg",
    .......,
    "30.jpg"
 ]

$(window).load(function(){

preLoad();

function preLoad(){
    var max = 30;
    var pic;
    picture = '<table id="table" style="dipslay:table;"><tr>';
    for (var i=0;i < max; i++){
        if(i < 30){
            pic = "images/art/"+pictures[i];
            appendCell(pic);
        }
        if(i < 17){
            pic = "images/street/"+pictures[i];
            appendCell(pic);
        }
        if(i < 19){
            pic = "images/doc/"+pictures[i];
            appendCell(pic);
        }
        if(i < 16){
            pic = "images/commercial/"+pictures[i];
            appendCell(pic);
        }
    };
    picture += '</tr></table>';
    $('#imageScroller').append(picture);
}

function appendCell(pic){
    picture +="<td class='image'><img class='i' src='"+pic+"'></td>";
    return;
}
});

我不太清楚如何在上面的动态图像加载循环中实现jpreloader,而不是已经插入这个dom。

Im not quite sure how to implement the jpreloader on a dynamic image loading loop above instead of already inserted into the dom.

$(document).ready(function () {
$('body').jpreLoader();
});

谢谢。

推荐答案

自定义进度条,更新为图像下载

Custom progress bar that updates as images are downloaded

var pictures = [
                    "1.jgp",
                    "2.jpg"
                ];

var loadedSoFar = 0;

function loaded( ) {
    // do stuff
}

function updateProgress( ) {

    loadedSoFar++;

    var newWidth = $("#progress").width() * ( loadedSoFar / pictures.length );
    $("#bar").stop( true, true );
    $("#bar").animate({ width: newWidth }, 500, function( ) {
        if( loadedSoFar === pictures.length ) { loaded() }
    });
}

for( var i = 0; i < pictures.length; i++ ) {
    var img = new Image();
    img.src = pictures[ i ] + i;

    img.onload = function( ) {
        updateProgress();
    }
}

演示 这里

这篇关于使用javascript缓存页面加载之前从数组缓存动态图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆