Home arrow HTML arrow Page 3 - Maximizing and Restoring HTML Images with the Image Map Method
HTML

Maximizing and Restoring HTML Images with the Image Map Method


This article is a follow up to the article "Maximizing and Restoring HTML Images with the Absolute Method." It offers an alternative method, which I call "Image Map." So if you want another way to display an image on your web page with maximization and restoration capabilities, then by all means read this article.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
April 16, 2008
TABLE OF CONTENTS:
  1. · Maximizing and Restoring HTML Images with the Image Map Method
  2. · The Method
  3. · Image Map
  4. · Testing and Finalizing

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Maximizing and Restoring HTML Images with the Image Map Method - Image Map
(Page 3 of 4 )

I assume you already know how to create an image map. We shall call the clickable area "clickMaxRes" (meaning, click to maximize or restore). With simple calculation you can figure out that the start of a clickable area of 16 X 16 pixels, at the bottom right end of the normal sized image, will begin at approximately 171px, 208px of the image. It will end at the bottom right end of the main image, which is approximately 187px, 224px. With this in mind, replace the image code in the body element of the HTML file with the following:


<img id="w1" src="watch.jpg" usemap ="#clickMaxRest" class="watch" />

<map id ="clickMaxRest" name="clickMaxRest">

<area id = "a1" shape ="rect" coords ="171,208,187,224" onclick="maximizeRestore()" title="Maximize" />

</map>


The usemap attribute has been given the value #clickMaxRest, so the ID for the map tag has been given the value clickMaxRest. Its name attribute has been given the same value, clickMaxRest, for backward compatibility with older browsers. The area tag has been given the ID a1 (we shall see its function shortly). As indicated above, the start x and y coordinates for the clickable area are (171px, 208px). The end x and y coordinates are (187px, 224px), which are the bottom right end coordinates. Would it not be nice for a tool tip to display the text "Maximize" when the mouse pointer is over the clickable maximization area? This is why the area tag ends with title="Maximize".

When the triangle is clicked, a JavaScript function will be called. This is the only function in the JavaScript. It will be called maximizeRestore(). That is why you have onclick="maximizeRestore()" for the onclick event in the area tag above. A code block in this function will increase the dimensions of the image using the style sheet. It will also give the image new image-map coordinates, where the clickable area will be at the bottom right end of the enlarged (maximized) image. The maximized image is twice the size of the normal-sized image, so the area containing the triangle will be approximately 32px X 32px.

When the triangle is clicked at the maximized size, the same JavaScript function will be called. Another code block in this function will decrease the dimensions of the image back to its normal size using the style sheet. It will also give the image the old image-map coordinates. Type the following JavaScript in the head element of the HTML file below the style sheet:


<script type="text/javascript">


//flag to indicate whether the image is maximized or restored

var restored = true; 

 

//function to maximize or restore the main image.

function maximizeRestore()

{

if (restored == true)

{//block to maximize the image.

document.getElementById("w1").style.width="374";

document.getElementById("w1").style.height="448";

document.getElementById("a1").coords="342,416,374,448";

document.getElementById("a1").title="Restore";

restored = false;

}

else

{//block to restore the image.

document.getElementById("w1").style.width="187";

document.getElementById("w1").style.height="224";

document.getElementById("a1").coords="171,208,187,224";

document.getElementById("a1").title="Maximize";

restored = true;

}

}


</script>


There is a variable in the script called "restored." Its value at the start of the script is Boolean TRUE. This is a flag that indicates whether the image is restored or maximized. When the web page is loaded, the image is initially at the restored size, so the value is initially TRUE. If you click the triangle, the JavaScript function maximizeRestore() will be called. This is the only function in the script. Since the Restored variable is true, the first code block will be executed. The first line in this code increases the width of the image. The second line in the code increases the height of the image. The third line sets the new clickable area of the image. The fourth line gives the new clickable area the title "Restored," so that if the user's mouse pointer is over it, the word "Restored" will appear to indicate that clicking the area will restore the image. Since the image is now maximized, the fifth line sets the Restored flag to False.

If you click the triangle while the image is maximized,, the JavaScript function will be called. Since the Restored flag has been set to False, the second code block of the function will be executed. The first line of this block decreases the width of the image. The second line in the code decreases the height of the image. The third line sets the former clickable area of the image back. The fourth line gives the former clickable area its former title, "Maximize," so that if the user's mouse pointer is over it, the word "Maximize" will appear to indicate that clicking the area will maximize the image. Since the image is now restored, the fifth line sets the Restored flag to True, as it was before.


blog comments powered by Disqus
HTML ARTICLES

- HTML5 Boilerplate: Working with jQuery and M...
- HTML5 Boilerplate Introduction
- New API Platform for HTML5
- BBC Adopts HTML 5, Mozilla Addresses Issues
- Advanced Sticky Footers in HTML and CSS
- HTML and CSS Sticky Footers
- Strategy Analytics Predicts HTML5 Phones to ...
- HTML5 Guidelines for Web Developers
- Learning HTML5 Game Programming
- More Engaging CSS3 and HTML Background Effec...
- Engaging HTML and CSS3 Background Effects
- More Web Columns with CSS3 and HTML
- Columns with CSS3 and HTML
- Creating Inline-Block HTML Elements with CSS
- Drag and Drop in HTML5: Parsing Local Files

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials