Agile & TDD, Java, Python, PHP, Open Source, ...and Sailing
How To: Random hover effect for links

Here is a simple way, using Javascript and CSS, to randomly choose the hover behavior of your links. I chose to only change the color but you can easily modify the CSS in order to achieve your desired effect.

Step 1: The CSS

In a CSS file, create several CSS classes that describe the behaviors you would like to achieve when your user hovers over your link. Again, I chose to only change the color of my links when hovered.

.blue { color : #0000FF; } .purple { color : #FF00FF; } .green { color : #00FF00; } .yellow { color : #FFFF00; } .orange { color : #FF6600; }


Step 2: The JavaScript

Create a JavaScript file (or imbedded in your HTML with <script> tags) with an Array declaration and two functions. The Array will be a list of the class names created in the CSS from above.

var colors = new Array(5); colors[0]="blue"; colors[1]="purple"; colors[2]="green"; colors[3]="yellow"; colors[4]="orange";

The first function will return an int between 0 and the size of the array minus 1. This is done by calling the Math.random() method and multiplying it's result by the size of the array then using the Math.floor() function to represent the random float to an "integer". Note, JavaScript always uses floats, there is no integer type.

function getRandom(max) { return Math.floor(Math.random()*max); }

The second function will return the item in the array generated from the random number from the previous method.

function rdmColor() { return colors[getRandom(colors.length)]; }

Step 3: The HTML

Now the tricky part. The hardest part about this implementation is writing the HTML. It does make your <a> tags a bit messy, but it sure looks pretty to your user. Plus, who is really looking at your HTML source anyway?

For each of your links that you choose to use this random effect, you must add an 'onmouseover' event. The 'onmouseover' event must call the second function in your JavaScript to set the CSS className of the tag.

<a href="http://www.google.com" onmouseover="this.className=rdmColor()">Google</a>

Example link

If you keep it like this you'll notice one minor flaw. When you hover your mouse it works all well and good until your mouse leaves the link. The effect will stay present. To fix this, the 'onmouseout' event must be added to your tag. I use the default className for the tag. Typically, links will use the <a> tag so the default className is 'a'.

<a href="http://www.google.com" onmouseover="this.className=rdmColor()" onmouseout="this.className='a'">Google</a>

This way, when your user's mouse leaves the link, it will return to it's default effect.

Example link

Voilà!


0 Comments

Post a Comment
Your comment has been sucessfully added.
Sorry, an unexpected error occured. Please try again later.





CAPTCHA

portrait

Matthew Boston is a software developer and freelance web developer located in Columbus, OH.

Currently, Matthew is working on an enterprise test harness utilizing Python, Java, XML, WSDL, Maven, and Hudson CI.

Also, Matthew is developing a Dropbox-like application that expands upon the idea of a network-shared drive.

More about Matthew.

my LinkedIn profile
Search
Coming Soon
Tag filtering
Comments for readers
Reader-driven ratings for blog entries
Site-wide searching
Tag cloud
RSS feed
Reply-to comments