阿里云主机折上折
  • 微信号
Current Site:Index > <map> - image mapping

<map> - image mapping

Author:Chuan Chen 阅读数:35745人阅读 分类: HTML

The <map> tag is used to define a client-side image map, which is an image with clickable areas. Image maps allow users to trigger different actions or links by clicking on different regions of the image. The <map> tag is typically used in conjunction with the usemap attribute of the <img> tag and defines specific clickable areas using the <area> tag.

Basic Syntax of the <map> Tag

The basic structure of the <map> tag is as follows:

<img src="image.jpg" alt="Example Image" usemap="#mapName">
<map name="mapName">
  <area shape="rect" coords="x1,y1,x2,y2" href="url1" alt="Area 1">
  <area shape="circle" coords="x,y,radius" href="url2" alt="Area 2">
  <area shape="poly" coords="x1,y1,x2,y2,x3,y3,..." href="url3" alt="Area 3">
</map>
  • The name attribute defines the name of the <map> tag, and the usemap attribute of the <img> tag references this name using the # symbol.
  • The <area> tag defines clickable areas within the image and supports various shapes (rect, circle, poly).

Attributes and Shapes of the <area> Tag

The shape attribute of the <area> tag supports the following three shapes:

Rectangle (rect)

A rectangular area is defined by the coords attribute, specifying the top-left corner (x1,y1) and the bottom-right corner (x2,y2). For example:

<img src="worldmap.jpg" alt="World Map" usemap="#world">
<map name="world">
  <area shape="rect" coords="100,50,200,150" href="europe.html" alt="Europe">
</map>

Circle (circle)

A circular area is defined by the coords attribute, specifying the center (x,y) and the radius radius. For example:

<img src="solarsystem.jpg" alt="Solar System" usemap="#solar">
<map name="solar">
  <area shape="circle" coords="300,150,50" href="sun.html" alt="Sun">
</map>

Polygon (poly)

A polygonal area is defined by the coords attribute, specifying multiple vertex coordinates (x1,y1,x2,y2,...). For example:

<img src="country.jpg" alt="Country Map" usemap="#country">
<map name="country">
  <area shape="poly" coords="10,20,50,30,40,80,20,60" href="region.html" alt="Region">
</map>

Practical Examples

Example 1: Navigation Menu Image Map

Suppose there is an image of a navigation menu where clicking different areas leads to different pages:

<img src="navmenu.png" alt="Navigation Menu" usemap="#navmap">
<map name="navmap">
  <area shape="rect" coords="0,0,100,50" href="home.html" alt="Home">
  <area shape="rect" coords="100,0,200,50" href="about.html" alt="About">
  <area shape="rect" coords="200,0,300,50" href="contact.html" alt="Contact">
</map>

Example 2: Interactive Map

A world map where clicking different countries displays detailed information:

<img src="worldmap.png" alt="World Map" usemap="#worldmap">
<map name="worldmap">
  <area shape="poly" coords="120,50,150,70,180,60,200,90" href="france.html" alt="France">
  <area shape="circle" coords="300,120,30" href="japan.html" alt="Japan">
  <area shape="rect" coords="400,100,500,200" href="usa.html" alt="USA">
</map>

Dynamically Generating Image Maps

You can dynamically generate <map> and <area> tags using JavaScript. For example:

<img id="dynamicImage" src="dynamic.jpg" alt="Dynamic Image">
<script>
  const map = document.createElement('map');
  map.name = 'dynamicMap';
  
  const area1 = document.createElement('area');
  area1.shape = 'rect';
  area1.coords = '0,0,100,100';
  area1.href = 'page1.html';
  area1.alt = 'Area 1';
  
  const area2 = document.createElement('area');
  area2.shape = 'circle';
  area2.coords = '150,150,50';
  area2.href = 'page2.html';
  area2.alt = 'Area 2';
  
  map.appendChild(area1);
  map.appendChild(area2);
  document.body.appendChild(map);
  
  document.getElementById('dynamicImage').usemap = '#dynamicMap';
</script>

Compatibility and Considerations for Image Maps

  • Modern browsers support the <map> tag, but ensure accurate coordinate calculations.
  • Mobile devices may have limited support for clickable areas in image maps; consider optimizing for touch events.
  • Use the alt attribute for each <area> to improve accessibility.

Comparison Between Image Maps and SVG

SVG can achieve similar effects to image maps and supports more complex interactions and animations. For example:

<svg width="500" height="300">
  <a href="france.html">
    <polygon points="120,50 150,70 180,60 200,90" fill="blue" />
  </a>
  <a href="japan.html">
    <circle cx="300" cy="120" r="30" fill="red" />
  </a>
</svg>

SVG offers advantages like vector scaling and dynamic effects, while <map> is more suitable for simple static image maps.

本站部分内容来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn

Front End Chuan

Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.