Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / Javascript
Article

OO DOM Image Rollover

Rate me:
Please Sign up or sign in to vote.
4.18/5 (8 votes)
6 Jun 20051 min read 52.9K   7   8
This script makes it easy to add rollover/ mousedown effects to any image on the page, including image submit buttons. It automatically preloads images as well. Script works in all DOM capable browsers- IE5+, NS6+, Opera7+.

Sample Image - sample3.gif

Introduction

This is an updated script from my personal library. The script can easily add rollovers and mousedown effects to any image through properties of the image. It can also automatically preload images on page load. This script also allows you to specify OnMouseDown and OnMouseOut properties without comprising the effect of srcover and srcdown.

MouseOver Effect

To create a mouse over image is as simple as adding srcover="mouseoverimage.jpg" to your image HTML code. For example:

HTML
<img src="images/button1.gif" width="148" height="24" 
          srcover="images/button1_over.gif">

Example 1

MouseDown Effect

To create a mouse down effect is also very simple, just by adding srcdown="mousedownimage.jpg" to your HTML. For example:

HTML
<img src="images/button1.gif" width="148" 
       height="24" srcdown="images/button1_down.gif">

Example 2

Combined MouseOver and MouseDown Effect

The script can also handle mouse over effects and mouse down effects together. By specifying srcover="mouseoverimage.jpg" srcdown="mousedownimage.jpg" together, your images can now handle both effects. For example:

HTML
<img src="images/button1.gif" width="148" height="24" 
  srcover="images/button1_over.gif" srcdown="images/button1_down.gif">

Example 3

Image Submit Buttons

Besides regular images, this script can also do the same effects to image submit buttons. It's as easy as this:

HTML
<input type="image" name="submitimg" src="images/submitbutton.gif" 
  srcover="images/submitbutton_over.gif" 
  srcdown="images/submitbutton_down.gif" border="0">

Extra Functions

This script will preload all of your src, srcover and srcdown images for faster loading. The script will not preload all images and waste precious client side memory. It will only preload the image HTML that includes a srcover or srcdown property.

The script will also forward and run all OnMouseOver, OnMouseOut and OnMouseUp property events without disturbing the private ones.

Full Source Code

JavaScript
function imageholderclass(){
  this.over=new Array();
  this.down=new Array();
  this.src=new Array();
  this.store=store;
  
  function store(src, down, over){
    var AL=this.src.length;
    this.src[AL]=new Image(); this.src[AL].src=src;
    this.over[AL]=new Image(); this.over[AL].src=over;
    this.down[AL]=new Image(); this.down[AL].src=down;
  }
}

var ih = new imageholderclass();
var mouseisdown=0;

function preloader(t){
  for(i=0;i<t.length;i++){
    if(t[i].getAttribute('srcover')||t[i].getAttribute('srcdown')){
      
      storeimages(t[i]);
      var checker='';
      checker=(t[i].getAttribute('srcover'))?checker+'A':checker+'';
      checker=(t[i].getAttribute('srcdown'))?checker+'B':checker+'';
      
      switch(checker){
      case 'A' : mouseover(t[i]);mouseout(t[i]); break;
      case 'B' : mousedown(t[i]); mouseup2(t[i]); break;
      case 'AB' : mouseover(t[i]);mouseout(t[i]); 
                  mousedown(t[i]); mouseup(t[i]); break;
      default : return;      
      }
      
      if(t[i].src){t[i].setAttribute("oldsrc",t[i].src);}
    }
  }
}

function mouseup(t){
  var newmouseup;
  if(t.onmouseup){
    t.oldmouseup=t.onmouseup;
    newmouseup= 
      function(){mouseisdown=0;
                 this.src=this.getAttribute("srcover");
                 this.oldmouseup();}

  }
  else{
    newmouseup = 
      function(){mouseisdown=0;
                 this.src=this.getAttribute("srcover");}
  }
  t.onmouseup=newmouseup;
}

function mouseup2(t){
  var newmouseup;
  if(t.onmouseup){
    t.oldmouseup=t.onmouseup;
    newmouseup=function(){mouseisdown=0;
               this.src=this.getAttribute("oldsrc");
               this.oldmouseup();}
    }
  else{newmouseup=function(){mouseisdown=0;
                  this.src=this.getAttribute("oldsrc");}}
  t.onmouseup = newmouseup;
}

function mousedown(t){
  var newmousedown;
  if(t.onmousedown){
    t.oldmousedown=t.onmousedown;
    newmousedown=
      function(){if(mouseisdown==0)
                 {this.src=this.getAttribute("srcdown");
                  this.oldmousedown();}}
  }
  else{newmousedown=
       function(){if(mouseisdown==0){
                      this.src=this.getAttribute("srcdown");}}}
  t.onmousedown=newmousedown;
}

function mouseover(t){
  var newmouseover;
  if(t.onmouseover){
    t.oldmouseover=t.onmouseover;
    newmouseover= function(){this.src=this.getAttribute("srcover");
                             this.oldmouseover();}
  }
  else{newmouseover=function(){this.src=this.getAttribute("srcover");}}
  t.onmouseover=newmouseover;
}

function mouseout(t){
  var newmouseout;
  if(t.onmouseout){
    t.oldmouseout=t.onmouseout;
    newmouseout = 
   function(){this.src=this.getAttribute("oldsrc");this.oldmouseout();}
  }
  else{newmouseout=function(){this.src=this.getAttribute("oldsrc");}}
  t.onmouseout=newmouseout;
}

function storeimages(t){
  var s=(t.getAttribute('src'))?t.getAttribute('src'):'';
  var d=(t.getAttribute('srcdown'))?t.getAttribute('srcdown'):'';
  var o=(t.getAttribute('srcover'))?t.getAttribute('srcover'):'';
  ih.store(s,d,o);
}

function preloadimgsrc(){
  if(!document.getElementById) return;
  var it=document.getElementsByTagName('IMG');
  var it2=document.getElementsByTagName('INPUT');
  preloader(it);
  preloader(it2);
}

if(window.addEventListener)
  {window.addEventListener("load", preloadimgsrc, false);} 
else{
  if(window.attachEvent){window.attachEvent("onload", preloadimgsrc);}
  else{if(document.getElementById){window.onload=preloadimgsrc;}}
}

Conclusion

Well I hope you like my code, let me know if you find any bugs :P

Latez

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Codevendor
United States United States
Please visit my personal website https://codevendor.com for my latest codes and updates.

Comments and Discussions

 
GeneralMy vote of 5 Pin
kip_is25-Dec-10 23:47
kip_is25-Dec-10 23:47 
GeneralMy vote of 5 Pin
kip_is31-Oct-10 10:32
kip_is31-Oct-10 10:32 
GeneralWe know that the challenge is more important Pin
Glauco.Basilio5-Nov-06 9:34
Glauco.Basilio5-Nov-06 9:34 
GeneralFor rollovers I usually go for the simpler: Pin
frumbert14-Jun-05 19:26
frumbert14-Jun-05 19:26 
Generalwrong use of HTML semantics Pin
Rui Dias Lopes6-Jun-05 22:19
Rui Dias Lopes6-Jun-05 22:19 
what's wrong:

1. Using custom attributes isn't valid html;
2. Using img tags for site navigation is wrong. Should be a list/anchor combo;
3. Images rollovers can be implemented with 100% CSS;


benefits from using html standards the right way:

1. Validate your site as HTML 4.01 or XHTML 1.0;
2. Leverage computing resources (no scripts are run);
3. Make your site more acessibility-friendly;



just my 2 cents.

is this a sig?
GeneralRe: wrong use of HTML semantics Pin
M@dHatter7-Jun-05 9:00
M@dHatter7-Jun-05 9:00 
GeneralRe: wrong use of HTML semantics Pin
Rui Dias Lopes7-Jun-05 23:10
Rui Dias Lopes7-Jun-05 23:10 
GeneralRe: wrong use of HTML semantics Pin
Anonymous17-Jun-05 15:53
Anonymous17-Jun-05 15:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.