Image Rollovers

Feb1109Feb 11, 09

Author's note: This technique is ancient and should not be used. Use CSS ":hover", and if you absolutely must img tags, swap between two so the browser can load them when necessary.

Image Rollovers are a common visual effect used by websites to encourage user mouse interaction within a document. The most common method for creating image rollovers became outdated as of XHTML 1.0, though most websites have not yet modified their chosen script to conform to the web standard. This tutorial will show you how to create the same effect achieved by that old inline scripting, with the added perk of built-in image pre-loading.

To make sure this script runs AFTER the page finishes downloading, we have to add it to the list of "window.onload" events. It is important to preserve other functions that might be scheduled to execute "onload", so we set the old "window.onload" event to a variable for later use:

window.image_mouseover_effect_oldLoad = window.onload ? window.onload : function(){};

Now we design our own onload function, which will find all the images that have "image_mouseover_effect" in their class (called "className"), and then modify each image so it has the mouseover effect:

window.onload = function() { // our script will be here };

First, store an array of all elements in the document:

var allImages = document.getElementsByTagName("img");

Then loop through each img element in that array, and pick out the ones that have our special class name "image_mouseover_effect":

for(var i=0; i<allImages.length; i++) { if(allImages[i].className.indexOf("image_mouseover_effect") > -1) { // Code from next step } }

Each img needs a function for the onmouseover (mouse entering) event. The function will change the image's src attribute (the image's location) to something like "imageX_over.jpg", from an original "imageX.jpg":

allImages[i].oldonmouseover = allImages[i].onmouseover ? allImages[i].onmouseover : function(){}; allImages[i].onmouseover = function(e) { var li = this.src.lastIndexOf("."); this.src = this.src.substring(0,li) + "_over" + this.src.substring(li, this.src.length); this.oldonmouseover(e); };

Like the onmouseover event, we need to handle the onmouseout (mouse leaving) event. This next part reverts the image's src attribute back to the original "imageX.jpg", but only if it has "_over" in it (note that 5 is the length of "_over"):

allImages[i].oldonmouseout = allImages[i].onmouseout ? allImages[i].onmouseout : function(){}; allImages[i].onmouseout = function(e) { this.src = this.src.replace(/_over./mi,"."); this.oldonmouseout(e); };

Lastly, we need to call the "window.onload" function that we replaced, so our image rollover script doesn't disable other scripts on the same page:


That's it! Now, lets see all of those pieces in one clean block of JavaScript code:

window.image_mouseover_effect_oldLoad = window.onload ? window.onload : function(){}; window.onload = function() { var allImages = document.getElementsByTagName("img"); for(var i=0; i<allImages.length; i++) { if(allImages[i].className && allImages[i].className.indexOf("image_mouseover_effect") > -1) { allImages[i].oldonmouseover = allImages[i].onmouseover ? allImages[i].onmouseover : function(){}; allImages[i].onmouseover = function(e) { var li = this.src.lastIndexOf("."); this.src = this.src.substring(0,li) + "_over" + this.src.substring(li, this.src.length); this.oldonmouseover(e); }; allImages[i].oldonmouseout = allImages[i].onmouseout ? allImages[i].onmouseout : function(){}; allImages[i].onmouseout = function(e) { this.src = this.src.replace(/_over./mi,"."); this.oldonmouseout(e); }; } } window.image_mouseover_effect_oldLoad(); };

About Jason Miller:

I am a JavaScript developer from Waterloo, Ontario, Canada. When I am not typing green code onto a black screen, you might find me at the nearest coffee pub checking out the brew. I run a internet firm called developIT and maintain blogs and web apps when I can.
Wow, much better than the old way. Now I don't have to customize my rollover script every time I make new images. And there's no need for those custom ids, onmouseover, and onmouseout events. It's completely re-usable. Thank you so much for posting this!
I'm glad you found it useful!
UGG Outlet# Ultra Boost Uncaged Toms Outlet Jordan Shoes Ray Ban Outlet Oakley Outlet Longchamp Outlet Store Yeezy Keds Shoes For Women Nike UK Jordan 4 Nike Huarache Adidas NMD Nike Factory Outlet MLB Jerseys Ray Ban Outlet Jordan 12 Ray Bans Outlet Adidas Yeezy Ray Ban Outlet Online Ralph Lauren Outlet Online UGG Boots Outlet Under Armour Outlet Yeezy Boost 350 Nike Outlet Store Longchamp Bags Roshe Run Air Max Air Max 2016 Yeezy NFL Jersey Polo Ralph Lauren Outlet Adidas UK Timberland UK Ralph Lauren Yeezy Boost 350 Nike Outlet Yeezy Boost 350 Ray Ban Sunglasses
jlgg# nike shoes australia mk bags coach online ray ban prescription sunglasses moncler jacket michael kors purses on sale adidas yeezy boost 350 burberry online bercrombie Nederland michael kors Oakley Outlet Ralph Lauren Polo oakley sunglasses KD 8 shoes coach factory oakley australia major league baseball baseball jerseys Nike Free pandora rings burberry outlet ray ban sunglasses prada bags outlet michael kors abercrombie paris canada goose outlet Jordan Retro prada outlet nike australia pandora charms yeezy boost 350 burberry outlet beats by dre yeezy boost 350 Ralph Lauren UK moncler coats Hermes Outlet piumini moncler replica Abercrombie Fitch tiffany co moncler clothing moncler jacket mens kate spade outlet paul smith uk michael kors outlet coach outlet prada bags Ray Ban Outlet michael kors outlet kate spade outlet michael kors italy kate spade bags landing gears china factory sale Canada Goose Outlet canada goose sale online Yeezy 350 michael kors outlet ray ban sunglasses kate spade canada moncler sale coach purse prada purses prada factory outlet online authentic prada handbags outlet Adidas Yeezy Boost 350 michael kors bags Cheap Ray Ban ugg australia nike jordan polo outlet prada handbags coach outlet nike shox shoes Jordan Retro Shoes coach carter pandora charms sale clearance Kevin Durant Shoes coach outlet ray ban sunglasses coach purses michael kors uk prada handbags Nike Hyperdunk 2015 moncler outlet michael kors handbags outlet B Derbyshire nike id ray ban aviator kate spade black friday burberry purse Michael Kors Outlet michael kors jet set tote prada factory outlet online louis vuitton outlet prada outlet online Longchamp Outlet kate spade bags nike basketball shoes ray ban glasses kate spade outlet pandora charms sale moncler outlet prada outlet clk benz burberry outlet canada nike shoes coach bags abercrombie us moncler uk ray-ban sunglasses Nike KD 8 coach usa adidas shoes abercrombie outlet prada outlet prices michael kors canada veronique Billat michael kors outlet michael kors tote abercrombie fitch coach outlet nike lebron shoes wholesale pandora rings burberry outlet online Kobe X Shoes pandora rings ray ban prescription glasses Nike Kobe 9 pandora charms Burberry outlet online coach outlet nike jordan shoes sunglasses hut michael kors outlet bags michael kors online burberry scarfs coach outlet ugg australia true religion uk burberry us kate spade outlet coach australia michael kors australia nike outlet huarache nike ray ban clubmaster tiffany co pandora bracelets burberry australia kate spade purses outlet official michael kors pandora rings nike jordan shoes air jordan shoes air jordan shoes ups tracking coach outlet nike free run abercrombie and fitch mcm factory outlet prada outlet online nike com burberry outlet online pandora bracelet adidas australia Pandora bracelet longchamp outlet nike australia burberry online ralph lauren australia Ralph Lauren Hugo Boss Store kate spade black friday abercrombie & fitch asics Australia cheap coach purse michael kors bags outlet pandora charms uk air jordan retro burberry canada true religion uk pandora ring authentic prada outlet online coach handbags coach sunglasses for women nfl jerseys Chanel bags Outlet louis vuitton outlet coach outlet online michael kors outlet oakley sunglasses burberry canada authentic prada outlet online pandora rings mk outlet pandora princess ring hermes bag michael kors handbags prada outlet online longchamp outlet abercrombie outlet kate spade outlet michael kors michael kors us rayban aviator coach factory outlet online ralph lauren uk prada outlet online ray ban online cheap ray ban sunglasses nike air max coach outlet online nfl shop pandora rings sale lebron james shoes ugg uk louis vuitton factory outlet coach outlet coach outlet mlb shop ugg boots michael kors outlet michael kors outlet prada bags ray ban wayfarer ralph lauren uk prada outlet online Oakley Holbrook prada handbags cheap moncler jacket Coach Sunglasses Outlet burberry scarf michael kors outlet online oakley sunglasses basketball jerseys nike lebron james ray ban outlet prada outlet online pandora rings nike store kate spade pandora necklace charms ray ban eyeglasses burberry outlet Nike Air Max pandora bracelet jordan shoes beats headphones pandora rings michael kors outlet abercrombie outlet prada outlet kate spade canada goose sale pandora charms prada outlet prada bags on sale huaraches ray-ban sunglasses nike huarache shoes michael kors outlet kate spade outlet online pandora rings coach bag Nike Air Jordan pandora uk chanel coach outlet michael kors purses michael kors outlet hermes uk yeezy boost 350 shoes ray-ban sunglasses Pandora Charm canada goose jacket Mizuno Wave coach handbags coach factory outlet burberry burberry australia abercrombie and fitch michael kors purses outlet pandora jewelry baseball jerseys louboutin shoes ray ban new wayfarer mk handbags prada outlet online Mizuno Running Shoes coach australia yeezy boost 350 paul smith sale Chanel Outlet CHanel Factory Outlet jordan retro shoes burberry outlet louis vuitton outlet Hyperdunk 2016 pandora charms coach factory outlet Official Ralph Lauren UK Online Store coach outlet store coach outlet landinggear Kate Spade Australia Billat kate spade outlet oakley frogskins sunglasses asics shoes sunglasses hut Coach Outlet Online Ralph Lauren Polo snow boots Mizuno Shop Japan Hugo Boss Online prada bags Kobe 9 Shoes shoes online kate spade bags outlet prada outlet ray ban outlet louis vuitton outlet prada bag pandora charms coach coach australia michael kors outlet burberry scarf coach factory outlet pandora jewelry prada sunglasses timberland boots nike outlet salomon hiking shoes pandora australia michael kors outlet Prada Outlet oakley frogskins michael kors outlet kate spade adidas australia nike jordan shoes burberry outlet Polo Ralph Lauren pandora necklace coach outlet burberry scarf canada goose jacket coach outlet online football jerseys pandora australia hockey jerseys shoes sale paul smith prada outlet online mcm outlet michael kors outlet michael kors black friday mlb shop cheap basketball shoes Mizuno Shop louis vuitton outlet ralph lauren pandora australia Polo Ralph Lauren michael kors tote pandora uk Mizuno Shoes louis vuitton australia michael kors wallet coach outlet coach shoes pandora charms sale online Nike Jordans Prada Factory Outlet jimmy choo australia michael kors outlet online ray ban sunglasses ray ban polarized michael kors outlet prada factory outlet online michael kors factory outlet burberry outlet veronique billat moncler us chanel australia coach outlet polo online huaraches shoes beats studio asics shoes Australia moncler mens jackets pandora necklaces ralph lauren shirts basketball jerseys polo outlet online Bed and Breakfast in chesterfield michael kors bags flash sunglasses shoes online sale salomon shoes abercrombie and fitch Coach outlet online coach outlet abercrombie outlet us Hugo Boss Outlet oakley outlet pandora australia michael kors outlet coach diaper bag baseball jerseys michael kors outlet true religion jeans michael kors handbags abercrombie outlet ugg slippers burberry outlet canada michael kors outlet moncler down jackets nike air huarache pandora rings jewelry kate spade outlet michael kors bags michael kors outlet nike huarache prada official site moncler jacket michael kors outlet Oakley Holbrook sunglasses timberland uk china wholesale coach factory top sunglasses Nike Air Jordan kate spade handbags Jordan retro Michael Kors Outlet kate spade outlet online coach outlet online michael kors outlet official prada site Free Run kate spade purses nike shox michael kors outlet prada outlet Whitton Lodge Chesterfield Derbyshire UK Basketball shoes abercrombie outlet michael kors kate spade handbags Nike Hyperdunk Shoes michael kors uk abercrombie us mcm outlet online pandora jewelry nike shoes michael kors outlet mens sunglasses Nike Jordan Shoes coach factory outlet ralph lauren Michael Kors Canada sunglasses sm pandora charms black friday pandora charms sale football jerseys pandora charms michael kors outlet sunglasses outlet prada us prada bags mlb store coach sunglasses prada factory outlet prada online louis vuitton uk michael kors handbags moncler outlet landing gear official prada site coach bags pandora bracelet chanel bag michael kors totes Coach Outlet Store moncler jacket brand sunglasses michael kors outlet Yeezy Boost 350 prada sale true religion michael kors purses prada glasses Coach Outlet michael kors outlet online MIZUNO Shop US
Shana Phillips#
Scott Jones#
Scott Jones#
Scott Jones#
Ann Tabares#
Leave a Comment

Post Comment