简体   繁体   English

加快img加载

[英]speed up img loading

I have an array of images which pre-load and I have nine img tags on my page. 我有一个预加载的图像数组,我的页面上有9个img标签。 I want to randomly replace and scramble the nine imgs with images from the array when i hover over a button. 我想随机替换,并与来自该阵列的图像加扰的九个IMGS当我悬停在一个按钮。 this all works but, it runs slowly, the imgs re-load the src. 所有这些都可以,但是运行缓慢,img重新加载src。 how do i 我如何

var arrayImg = new Array();

arrayImg[0] = new Image();

<img id="img0" >

then do: 然后做:

document.getElementbyID("img0").src = arrayImg[random_ndx].src;

there are 9 images. 有9张图片。

I am running ie8 on a new fast pc but there is a very noticeable lag in the images changing. 我在新的快速PC上运行ie8,但是图像更改存在非常明显的滞后。 So, my question; 所以,我的问题; how do I assign a cached image (arrayImg[random_ndx]) directly to an element () on the page? 如何将缓存的图像(arrayImg [random_ndx])直接分配给页面上的元素()?

Use .replaceChild . 使用.replaceChild

local old = document.getElementbyID("img0")
old.parent.replaceChild(arrayImg[random_ndx], old)

Note that any element can't be in more than one place on page, so if you place element from arrayImg somewhere and then use it to replace another image you won't have two images, it will move from its previous location to new instead. 请注意,任何元素都不能在页面上超过一个位置,因此,如果将arrayImg元素放置在某个位置,然后用它替换另一个图像,则不会有两个图像,它将从其先前位置移至新位置。

You can load the images and make them hidden. 您可以加载图像并将其隐藏。 Then you need the hidden image to display. 然后,您需要显示隐藏的图像。 That's how I would simulate a cached array. 这就是我模拟缓存数组的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM