topical media & game development

talk show tell print

graphic-javascript-effect-image-swap.js / js



  
  <!-- Paste this code into an external JavaScript file named: simpleSwap.js  -->
  
  /* This script and many more are available free online at
  The JavaScript Source :: http://javascript.internet.com
  Created by: Jehiah Czebotar :: http://jehiah.com/ */
  
  /* To read the explanation of how this works,
    visit: http://jehiah.com/archive/simple-swap */
  
  function SimpleSwap(el,which) {
    el.src=el.getAttribute(which || "origsrc");
  }
  
  function SimpleSwapSetup() {
    var x = document.getElementsByTagName("img");
    for (var i=0;i<x.length;i++){
      var oversrc = x[i].getAttribute("oversrc");
      if (!oversrc) continue;
        
      // preload image -
      // comment the next two lines to disable image pre-loading
      x[i].oversrc_img = new Image();
      x[i].oversrc_img.src=oversrc;
  
      // set event handlers
      x[i].onmouseover = new Function("SimpleSwap(this,'oversrc');");
      x[i].onmouseout = new Function("SimpleSwap(this);");
  
      // save original src
      x[i].setAttribute("origsrc",x[i].src);
    }
  }
  
  var PreSimpleSwapOnload =(window.onload)? window.onload : function(){};
  window.onload = function(){PreSimpleSwapOnload(); SimpleSwapSetup();}
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.