[英]Preload images image.onload script MVC
My application is a MVC, using MS SQL to get the image file names. 我的应用程序是MVC,使用MS SQL来获取图像文件名。 I am using the following script to preload images; 我正在使用以下脚本预加载图像; would like to know How I can display hourglass cursor while waiting to complete the preload. 想知道如何在等待完成预加载时显示沙漏光标。
function preload_images() {
var i, count = 0;
$('#selector').css('cursor', 'wait');
for (i = 1; i < sl; i++) {
images[i] = new Image();
images[i].onload = images[i].onerror = function () {
count++;
if (count == sl) {
$('#selector').css('cursor', 'default');
}
}
images[i].src = impath.toString() + im.toString() + i + ".jpg";
}
}
It works great first time, however, when I change the file name from a dropdownlist, the hourglass does not stop even all images are load. 第一次运行效果很好,但是,当我从下拉列表中更改文件名时,即使加载了所有图像,沙漏也不会停止。 Thanks in advance. 提前致谢。
从上面的代码中,您正在加载sl-1
图像,但是停止等待光标的条件是sl
图像( if (count == sl)
),因此请将其更改为if (count == (sl-1))
Thanks to Musa for providing the initial solution, here is the way I got it to work, just in case someone is looking for it: 感谢Musa提供的初始解决方案,这是我使它起作用的方法,以防万一有人在寻找它:
var images = [];
function preload_images() {
showLoadingImage();
$('#selector').css('cursor', 'wait');
for (i = tochar; i < sl; i++) {
images[i] = new Image();
var name = impath.toString() + imStart.toString() + i + ext;
images[i].src = 'ImageHandler.ashx?img=' + name + '&window=50&level=50 &slice=0';
if (i == sl - 1) {
images[i].onload = images[i].onerror = function () {
$('#selector').css('cursor', 'Default');
hideLoadingImage(this);
};
}
}
}
function showLoadingImage() {
$('#imagediv').append('<div id="imagediv1"><img src="../Content/images/ajax-loader2.gif" alt="Loading..." /></div>');
$('#imagediv1').show();
}
function hideLoadingImage(img) {
$('#imagediv1').hide();
img.onload = img.onerror = null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.