0

I have an array of images which pre-load and I have nine img tags on my page. I want to randomly replace and scramble the nine imgs with images from the array when i hover over a button. this all works but, it runs slowly, the imgs re-load the 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.

I am running ie8 on a new fast pc but there is a very noticeable lag in the images changing. So, my question; how do I assign a cached image (arrayImg[random_ndx]) directly to an element () on the page?

Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
  • 1
    This question looks like a duplicate, see if the url helps you http://stackoverflow.com/questions/476679/preloading-images-with-jquery – roacher Oct 30 '12 at 09:55

2 Answers2

0

Use .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.

Oleg V. Volkov
  • 21,719
  • 4
  • 44
  • 68
0

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.

Micromega
  • 12,486
  • 7
  • 35
  • 72