DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> DIV十CSS布局 >> 布局實例
用 CSS 的 map 標籤實現單圖多區域點擊
編輯:布局實例     

map 標籤

用來定義一個客戶端圖像映射。圖像映射(image-map)指帶有可點擊區域的一幅圖像。area 元素永遠嵌套在 map 元素內部,用來定義圖像映射中的區域。

img 標籤的 usemap 屬性可以引用 map 標籤中的 id 或 name 屬性(取決於瀏覽器),所以我們應同時給 map 標籤添加 id 和 name 屬性。

示例

比如我們想在下面這張圖上實現九個熱點區域,又不切圖,就可以用 map 標籤:

首先用 PS 量出幾個坐標:

然後代碼實現:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. </head>
  7. <body>
  8. <img src="cat.jpg" alt="" usemap="#catmap" >
  9. <map name="catmap">
  10. <area shape="rect" coords="0,0,148,139" href ="https://www.baidu.com" target ="_blank" alt="">
  11. <area shape="rect" coords="148,139,295,0" href ="https://www.sina.com" target ="_blank" alt="">
  12. <area shape="rect" coords="295,0,439,140" href ="https://www.qq.com" target ="_blank" alt="">
  13. <area shape="rect" coords="148,139,0,340" href ="https://www.163.com" target ="_blank" alt="">
  14. <area shape="rect" coords="148,139,296,340" href ="https://www.soso.com" target ="_blank" alt="">
  15. <area shape="rect" coords="296,340,439,140" href ="https://sf.gg" target ="_blank" alt="">
  16. <area shape="rect" coords="0,340,148,493" href="https://www.zhihu.com" target ="_blank" alt="">
  17. <area shape="rect" coords="148,493,296,340" href="https://z.cn" target ="_blank" alt="">
  18. <area shape="rect" coords="296,340,436,490" href="https://jd.com" target ="_blank" alt="">
  19. </map>
  20. </body>
  21. </html>

就是這麼簡單。

關於 area

area 可以是圓形(circle)、多邊形(polygon)或矩形(rect),不同形狀要選取不同的坐標(coords):

圓形:shape="circle",coords="x,y,z",其中 x,y 為圓心坐標,z 為圓的半徑;

多邊形:shape="polygon",coords="x1,y1,x2,y2,x3,y3,...",每一對 x,y 坐標定義多邊形的一個頂點((0,0) 是圖像左上角坐標)。定義三角形至少需要三組坐標,邊數越多則需要越多頂點;

矩形:shape="rectangle",coords="x1,y1,x2,y2",第一對坐標是矩形一個角的頂點,另一對是對角頂點,(0,0) 是圖像左上角坐標。也就是說,只要知道對角兩點的坐標即可——定義矩形其實是定義帶四個頂點的多邊形的一種簡化方法。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved