答案是使用map和area标签可创建图像热点,但响应式设计中因坐标固定易错位,需用JavaScript动态调整或改用SVG、CSS覆盖层等现代方案更优。

map
area
map
area
map
要使用
map
area
<img>
<map>
<img>
usemap
<map>
name
<map>
<area>
我们来看一个基本结构:
<img src="path/to/your/image.jpg" alt="描述图片内容" usemap="#myimagemap"> <map name="myimagemap"> <!-- 矩形区域 --> <area shape="rect" coords="0,0,100,50" href="link1.html" alt="区域1"> <!-- 圆形区域 --> <area shape="circle" coords="150,75,25" href="link2.html" alt="区域2"> <!-- 多边形区域 --> <area shape="poly" coords="200,10,250,10,275,50,225,50" href="link3.html" alt="区域3"> </map>
这里有几个关键点:
<img>
usemap
#
<map>
name
<map>
name
usemap
<area>
shape
rect
circle
poly
default
area
default
coords
rect
x1,y1,x2,y2
circle
x,y,radius
poly
x1,y1,x2,y2,...,xn,yn
href
alt
target
<a>
target
_blank
实际操作中,确定
coords
map
area
说实话,
map
area
首先,响应式设计的兴起是最大的冲击。
area
coords
其次,交互的灵活性。现代网页对交互性的要求越来越高,我们不只是需要一个简单的点击链接,可能还需要悬停效果、更复杂的动画、甚至是基于用户行为的动态热点。
map
area
map
area
再者,开发效率和维护成本。对于复杂的图像,手动计算
poly
coords
position
div
button
当然,这并不意味着
map
area
area
coords
确定
area
coords
对于矩形(shape="rect"
coords
x1,y1,x2,y2
x1,y1
x2,y2
对于圆形(shape="circle"
coords
x,y,radius
x,y
radius
对于多边形(shape="poly"
coords
x,y
x1,y1,x2,y2,...,xn,yn
那么,具体怎么获取这些坐标呢?
div
div
position: absolute;
left
top
width
height
div
rect
circle
coords
map
area
我的经验是,对于简单的矩形和圆形,手动记录坐标很快。但遇到复杂的多边形,我通常会先用图片编辑软件把关键点都找出来,列成一个清单,然后再填入
coords
map
area
map
area
coords
具体来说,挑战主要有:
coords="100,100,200,200"
map
coords
那么,有哪些应对策略呢?
JavaScript动态调整(最常见但有代价):
这是解决
map
area
area
coords
比如,如果图片原始宽度是
originalWidth
currentWidth
scale = currentWidth / originalWidth
coords
scale
这个方法需要编写JavaScript代码来遍历
area
coords
coords
一个简化的JavaScript思路可能如下:
function resizeImageMap() {
const img = document.querySelector('img[usemap="#myimagemap"]');
if (!img) return;
const originalWidth = img.naturalWidth; // 图片的原始宽度
const currentWidth = img.clientWidth; // 图片当前在页面上的宽度
const scale = currentWidth / originalWidth;
const areas = document.querySelectorAll('map[name="myimagemap"] area');
areas.forEach(area => {
const originalCoords = area.dataset.originalCoords || area.coords; // 存储原始坐标
area.dataset.originalCoords = originalCoords; // 第一次运行时保存原始坐标
const newCoords = originalCoords.split(',').map(coord => Math.round(parseFloat(coord) * scale));
area.coords = newCoords.join(',');
});
}
window.addEventListener('resize', resizeImageMap);
window.addEventListener('load', resizeImageMap);这段代码只是一个示意,实际应用中可能需要更健壮的错误处理和对不同
shape
coords
使用专门的JavaScript库:
image map
image-map-resizer
map
area
替代方案的考虑(更推荐的现代做法):
onclick
position: absolute
div
div
div
我的个人观点是,如果不是非常简单的场景,或者有历史包袱,我通常会倾向于避免在现代响应式设计中直接使用
map
area
以上就是map和area标签怎么用的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号