I would advise to use jQuery for this purpose:
http://en.wikipedia.org/wiki/JQuery[
^],
http://jquery.com/[
^].
All you need is to handle the event
hover
which requires two handles: the first function is called when a mouse pointer goes over the image, the second function — when it goes out. For changes in border, you can use the functions
.addClass
and
.removeClass
. It will look something like this:
<html>
<head>
<title>...</title>
<script type="text/javascript" src="scripts/jquery-1.7.1.min.js"></script>
<style type="text/css"></style>
</head>
<body>
<img alt="Image" id="image" src = "someImage.png" />
<script type="text/javascript">
$(document).ready(function() {
borderClass = "border";
imageElement = $("#image"); //element found by its attribute "id"
imageElement.hover(
function() { // mouse pointer goes over the imageElement:
imageElement.addClass(borderClass);
},
function() { // mouse pointer goes out:
imageElement.removeClass(borderClass);
}
);
});
</script>
</body>
</html>
Please see:
http://api.jquery.com/category/events/[
^],
http://api.jquery.com/category/css/[
^],
http://api.jquery.com/hover/[
^],
http://api.jquery.com/addClass/[
^],
http://api.jquery.com/removeClass/[
^].
Your design has one noticeable flaw: when a border is added, the total size of the element is increased by 2 in each direction, which makes the image jumping by 1 pixel. You can introduce a bit more sophisticated style change to avoid this effect. For example, you can use the border of the same size in both cases, but change its color. This is simple and totally related to CSS, so I would leave this simple task for your home exercise.
—SA