Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A basic image pool should allow you to recycle img elements for re-use. Since you don't know how many images there will be total ahead of time, just have the pool size expand as necessary.</p> <p>Something like this should work:</p> <pre><code> function getImg( id, src, alt ) { var pool = document.getElementById( 'image_pool' ); var img = ( pool.children.length &gt; 0 ) ? pool.removeChild( pool.children[0] ) : document.createElement( 'img' ); img.id = id; img.src = src; img.alt = alt; return img; } function recycleImg( id ) { var img = document.getElementById( id ); document.getElementById( 'image_pool' ).appendChild( img.parentNode.removeChild( img ) ); } </code></pre> <p>Place a hidden "image_pool" container on your page somewhere, to hide the recycled images between usage:</p> <pre><code>&lt;div id="image_pool" style="display:none;"&gt;&lt;/div&gt; </code></pre> <p>Then any time you need a new img element, call:</p> <pre><code>document.getElementById( 'some_element_id' ).appendChild( getImg( 'my_image_id', 'images/hello.gif', 'alt_text' ) ); </code></pre> <p>And when you are done with it:</p> <pre><code>recycleImg( 'my_image_id' ); </code></pre> <hr> <p><strong>jQuery alternative</strong></p> <pre><code> function getImg( id, src, alt ) { var img; if( $("#image_pool").children().length &gt; 0 ) { return $("#image_pool img:first-child").attr( { 'id': id, 'src': src, 'alt': alt } ).detach(); } return $( "&lt;img /&gt;'" ).attr( { 'id': id, 'src': src, 'alt': alt } ); } function recycleImg( id ) { $( "#" + id ).detach().appendTo( $("#image_pool") ); } </code></pre> <p>When you need a new img element:</p> <pre><code>getImg( 'my_image_id', 'images/hello.gif', 'alt_text' ).appendTo( $( "#some_element_id" ) ); </code></pre> <p><em>(recycling works the same as above)</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload