优化幻灯片与数百个图像
Optimizing slideshow with hundreds of images
我正在制作一个包含数百张图像的幻灯片,用户可以通过单击'下一步'或'上一步'按钮进行导航。我的代码目前设置的方式是,每次用户做出导航决定点击next或prev时,都会加载一个新的图像(图像路径存储在数组中)。为了提高每个图像导航的加载时间,我试图想出一种方法来加载前5张图像和下5张图像在任何给定的时间。
所以如果一个用户当前在imageAry[i]
上,我想加载imageAry[i-5]
直到imageAry[i+5]
。这样做将显著增加幻灯片的工作感觉。
提前谢谢你。
我JavaScript var index=0;
$('.catalog-img-container').attr("src", img_array[index]);
$('#dialog').jqm();
$(".next").click(function(){
$('.catalog-img-container').attr("src", img_array[++index%arrayLength]);
});
$(".previous").click(function(){
if (--index < 0) index = arrayLength - 1;
$('.catalog-img-container').attr("src", img_array[index%arrayLength]);
});
我标记 <div class="catalogFrame">
<img class="catalog-img-container" src="">
</div>
您应该在这种情况下预加载images
,我知道您正在询问如何做。在不涉及太多细节和创建过于奢侈的东西的情况下,我将做以下作为起点:
var index=0;
//Array to hold your preloaded img objects
var img = new Array();
//Initial preload
preload(img_array, index, 5);
$('.catalog-img-container').attr("src", img_array[index]);
$('#dialog').jqm();
$(".next").click(function(){
$('.catalog-img-container').attr("src", img_array[++index%arrayLength]);
preload(img_array, index, 5);
});
$(".previous").click(function(){
if (--index < 0) index = arrayLength - 1;
$('.catalog-img-container').attr("src", img_array[index%arrayLength]);
preload(img_array, index, 5);
});
//Basic function to preload images in array, from currentIndex to desired +-range
function preload(array, currentIndex, range) {
for (i = currentIndex; i <= currentIndex + range; i++) {
if(i<array.length){
img[i] = new Image();
img[i].src = array[i];
}
}
for (i = currentIndex; i >= currentIndex - range; i--) {
if(i>=0){
img[i] = new Image();
img[i].src = array[i];
}
}
}
当然,你可以创建一个比这更复杂和详细的解决方案,但这是在你展示图片之前预加载图片的基本思想。
一旦你掌握了它的窍门,你就可以疯狂地动态创建和插入新的DOM
元素,因为图像加载允许你创建一个滑动/滚动的UI。但是现在,你的代码设置的方式,这应该工作得很好。
约翰的解决方案很好。更重要的是,你可以异步运行'preload'函数,以便在后台加载图像:
setTimeout(function() { preload(img_array, index, 5); }, 0);
此外,您可以使用'preload'函数中的位数组来检查图像是否先前加载过。它应该节省时间,我的意思是:
var bitArray = new Array(NUM_IMAGES);
for(i=0;i<NUM_IMAGES;i++) bitArray[i] = 0;
//Basic function to preload images in array, from currentIndex to desired +-range
function preload(array, currentIndex, range) {
for (i = currentIndex; i <= currentIndex + range; i++) {
if(i<array.length){
if(bitArray[i] == 1) continue;
img[i] = new Image();
img[i].src = array[i];
bitArray[i] = 1;
}
}
for (i = currentIndex; i >= currentIndex - range; i--) {
if(i>=0){
if(bitArray[i] == 1) continue;
img[i] = new Image();
img[i].src = array[i];
bitArray[i] = 1;
}
}
}
相关文章:
- 如何使用url加载程序在webpack中导入多个图像
- Phonegap-(安卓/iphone)多个图像的图像库出现问题
- 单击时切换两个图像
- 从手机中选择多个图像'使用phonegap的多媒体资料
- 使用php-mysqljavascript上传多个图像
- 当多个图像在源文件中时,如何从源文件中选择特定的图像
- 使用jquery上传多个图像
- 由多个图像文件组成的响应地图
- 如何从 javascript 中可能具有一个或多个图像的字符串中仅获取图像名称
- Jssor滑块停留在第一个图像上,直到我单击它
- 一个具有两个图像按钮的表单在只能按下一个按钮的情况下发送两个按钮值
- 是否可以在jquery中使用拖动滑块在4个图像之间滑动
- 每第n个图像具有下一个循环的余数偏移
- 使用nodeJS上传多个图像时,POST net::ERR_INSUFFICIENT_RESOURCES
- 数百个标记同时移动.糟糕的表现
- 如何使用jquery淡出多个图像
- 使用javascript获取多个图像的位置
- 上传前预览多个图像
- 在多个元素上运行的函数插入多个图像
- 优化幻灯片与数百个图像