Javascript rollovers
Introduction
In this tutorial, we will look at different ways to add javascript rollovers effects to HTML elements on a web page
Standard Javascript rollovers
On many web pages, javascript rollovers are handled by adding an onmouseover and onmouseout event on images.
- onmouseover is triggered when the mouse moves over an element
- onmouseout is triggered when the mouse moves away from the element
This is an example of how it works. Move your mouse over the arrow below:
This is the code used:
<img src="images/arrow.gif" onmouseover="this.src='images/arrow-over.gif'" onmouseout="this.src='images/arrow.gif'">
When the mouse moves over the images, the source of the image is set to arrow-over.gif(the green arrow). When the mouse moves away from the image, the source is reverted to the blue arrow(arrow.gif).
CSS rollovers with Javascript
Instead of changing the src attribute of an image, you can also use Javascript to change the css class of an element.
This is an example:
Move your mouse over me
This is the code used:
<style type="text/css">
.arrowOver{
background-repeat:no-repeat;
background-image:url('images/arrow-over.gif');
color:blue;
}
</style>
<p onmouseover="this.className='arrowOver'" onmouseout="this.className=''">Move your mouse over me</p>
When you move the mouse over the text, it will assign the element to the css class "arrowOver". This will apply all the layout defined in the <style> part above to this element, i.e. a background image and blue text. This styling will be removed when the mouse moves away from the text.
CSS styling with mootools
Mootools is one of many great libraries which makes it easier to work with Javascript. This is an example of how you can use mootools to add Javascript rollover effects:
Move your mouse over me
This is the code used for this example:
<style type="text/css">
.arrowOver, .standardArrow{
background-repeat:no-repeat;
}
p.arrowOver{
background-image:url('images/arrow-over.gif');
color:green;
}
.standardArrow{
background-image:url('images/arrow.gif');
color:blue;
}
</style>
<p class="standardArrow" onmouseover="$(this).addClass('arrowOver')" onmouseout="$(this).removeClass('arrowOver')">Move your mouse over me</p>
In this example, we are using two css classes, standardArrow and arrowOver.
An element on a web page can be assigned to more than one css class. In these cases, the styling you end up with will be a mix of these css classes. In the <style> part above, I have added a p. in front of arrowOver in order to give it precedence over standardArrow
When the mouse moves over the element, the css class arrowOver will be added to the element. When the mouse moves away from the element, it will be removed.
Automate it with Mootools
It always feels better to work with small and clean files instead of a large file containing a mix of HTML, Javascript and CSS. This is one of the reason why it's a good idea to have HTML, Javascript and CSS in separate files. It gives you more freedom and it also makes it much easier for you to maintain your site.
This is something we can accomplish by adding events dynamically, using plain Javascript or with a library like mootools.
Here, I want to show you how this can be done using Mootools.
Demo example - Move your mouse over the elements below:
Step 1: Plain HTML
The first thing we do is to write our plain HTML code:
<p class="standardArrow"><a href="#">Menu item 1</a></p>
<p class="standardArrow"><a href="#">Menu item 2</a></p>
<p class="standardArrow"><a href="#">Menu item 3</a></p>
<p class="standardArrow"><a href="#">Menu item 4</a></p>
As you can see, there is no Javascript here. All we have done is to create some <p> tags assigned to the standardArrow css class with some anchor tags inside it.
Step 2: CSS inside separate file
The next thing to do is to create a sub folder called css, and inside it a file called javascript-rollovers.css. This is the content of this file:
.arrowOver, .standardArrow{
background-repeat:no-repeat;
margin-bottom:0px;
margin-top:0px;
}
p.arrowOver{
background-image:url('../images/arrow-over.gif');
color:green;
}
.standardArrow{
background-image:url('../images/arrow.gif');
color:blue;
}
The css is almost identical to the one used in the previous example. We have only added a ../ prefix to the urls in order to have a relative paths from the css file.
Then we include this css file our web page using the <link> tag:
<link rel="stylesheet" href="css/javascript-rollovers.css">
Step 3: Javascript in separate file
We also create a sub folder called js which would be the folder for our Javascript code. Inside it, we create a new file called javascript-rollovers.js. This is the content of this file:
window.addEvent('domready', function() {
var els = $$('.standardArrow');
// Getting size of array as a variable for Javascript speed purpose
var countEls = els.length;
for(i=0;i<countEls;i++) {
els[i].addEvent('mouseover', arrowMouseover);
els[i].addEvent('mouseout', arrowMouseout);
}
});
function arrowMouseover(){
$(this).addClass('arrowOver');
}
function arrowMouseout(){
$(this).removeClass('arrowOver');
}
The first thing we do in this code is to add a domready event. This event will be triggered when the HTML or Document Object Model(DOM) of our web page has been loaded by the browser. When this happens, the function assigned to the event will be executed.
Inside this function, I'm creating an array of all elements on my page assigned to the css class standardArrow
var els = $$('.standardArrow');
The I loop through each of these elements and assign an onmouseover and onmouseout event to each of them.
els[i].addEvent('mouseover', arrowMouseover);
els[i].addEvent('mouseout', arrowMouseout);
When the mouse moves over the element, it will call the function named arrowMouseover and when the mouse moves away from it, it will call the function arrowMouseout.
These two function both consists of only one line. It simply adds and removes the arrowOver css class from the element triggering the event.
function arrowMouseover(){
$(this).addClass('arrowOver');
}
function arrowMouseout(){
$(this).removeClass('arrowOver');
}
The last thing we have to do is to include javascript-rollovers.js on our web page using the <script> tag:
<script type="text/javascript" src="js/javascript-rollovers.js"></script>
Comments















Post your comment
