Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 6c9e833c12
Fetching contributors…

Cannot retrieve contributors at this time

202 lines (170 sloc) 5.321 kB
<!DOCTYPE html>
<html>
<head>
<title>PreloadJS: Queue Example</title>
<script type="text/javascript" src="../src/preloadjs/AbstractLoader.js"></script>
<script type="text/javascript" src="../src/preloadjs/PreloadJS.js"></script>
<script type="text/javascript" src="../src/preloadjs/TagLoader.js"></script>
<script type="text/javascript" src="../src/preloadjs/XHRLoader.js"></script>
<!-- We also provide hosted minified versions of all CreateJS libraries.
http://code.createjs.com -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
var map = {};
var preload;
var loader;
var manifest;
var w = 238; // Item width
var h = 170; // Item height
function init() {
if (window.top != window) {
document.getElementById("header").style.display = "none";
}
$("#loadAnotherBtn").click(loadAnother);
$("#loadAllBtn").click(loadAll);
$("#reloadBtn").click(reload);
reload();
}
// Reset everything
function reload() {
// If there is an open preload queue, close it.
if (preload != null) { preload.close(); }
// Reset the UI
$("#reloadBtn").css("display", "none");
$(".box").remove();
$("#mainProgress .progress").width(0);
$("#loadAnotherBtn").attr("disabled", null);
$("#loadAllBtn").attr("disabled", null);
// Push each item into our manifest
manifest = [
"assets/image0.jpg",
"assets/image1.jpg",
"assets/image2.jpg",
"assets/image3.jpg",
"assets/Autumn.png",
"assets/BlueBird.png",
"assets/Nepal.jpg",
"assets/Texas.jpg"
];
// Create a preloader. There is no manfest added to it up-front, we will add items on-demand.
preload = new PreloadJS();
preload.onFileLoad = handleFileLoad;
preload.onProgress = handleOverallProgress;
preload.onFileProgress = handleFileProgress;
preload.onError = handleFileError;
preload.setMaxConnections(5);
}
function stop() {
if (preload != null) { preload.close(); }
}
function loadAll() {
while (manifest.length > 0) {
loadAnother();
}
}
function loadAnother() {
// Get the next manifest item, and load it
var item = manifest.shift();
preload.loadFile(item);
// If we have no more items, disable the UI.
if (manifest.length == 0) {
$("#loadAnotherBtn").attr("disabled", "disabled");
$("#loadAllBtn").attr("disabled", "disabled");
$("#reloadBtn").css("display","inline");
}
// Create a new loader display item
var div = $("#template").clone();
div.attr("id", ""); // Wipe out the ID
div.addClass("box")
$("#container").append(div);
map[item] = div; // Store a reference to each item by its src
}
// File complete handler
function handleFileLoad(event) {
var div = map[event.src];
div.addClass("complete");
// Get a reference to the loaded image (<img/>)
var img = event.result;
// Resize it to fit inside the item
var r = img.width/img.height;
var ir = w/h
if (r > ir) {
img.width = w;
img.height = w/r;
} else {
img.height = h;
img.width = h;
}
div.append(img); // Add it to the DOM
}
// File progress handler
function handleFileProgress(event) {
var div = map[event.src]; // Lookup the related item
div.children("DIV").width(event.progress*div.width()); // Set the width the progress.
}
// Overall progress handler
function handleOverallProgress(event) {
$("#mainProgress > .progress").width(preload.progress * $("#mainProgress").width());
}
// An error happened on a file
function handleFileError(event) {
var div = map[event.src];
div.addClass("error");
}
</script>
<link rel="stylesheet" type="text/css" href="./assets/demoStyles.css" />
<style type="text/css">
#template {
display:none;
}
#mainProgress {
width: auto;
height: 20px;
float: none;
position: relative;
}
#mainProgress LABEL { position: absolute; }
.item {
height: 170px;
width: 238px;
border: 1px solid #eee;
background-color: #ddd;
padding: 2px;
float:left;
position: relative;
text-align: center;
}
.item .progress {
width: 0;
height: 20px;
background-color: #9c9;
bottom: 0;
}
.complete {}
.error {
background-color: #FAA;
}
.complete DIV, .error DIV { display: none; }
</style>
</head>
<body onload="init()">
<header id="header" class="PreloadJS">
<h1><span class="text-product">Preload<strong>JS</strong></span> Preload Queue</h1>
<p>Click "load another" to add another image the the overall queue. As images are added, the preload progress reflects the overall loaded progress. Click "load all" to queue everything at once.</p>
</header>
<div id="container" class="content">
<input type="button" id="loadAnotherBtn" value="Load Another" disabled="disabled" />
<input type="button" id="loadAllBtn" value="Load All" disabled="disabled" />
<input type="button" id="reloadBtn" value="Reset" />
<div id="mainProgress" class="item">
<label>Overall Progress</label><div class="progress"></div>
</div>
<hr />
<!-- New items will get placed here -->
</div>
<!-- Item Template. This is cloned whenever we want a new one. -->
<div id="template" class="item">
<div class="progress"></div>
</div>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.