html - 使用 SVG 编码将鼠标悬停在 PNG 图像上时如何获得不同的颜色/填充/不透明度
问题描述
我有点生疏,十年左右没有在 HTML 方面做太多事情。我买了一个模板用于我的摄影网站,它符合我的喜好。但是,我正在制作一个单独的部分来解决我对美国的印象。在过去使用 AREA MAP 选择特定状态时,我遇到了一个问题,即当图像调整大小时,可点击部分也出现了问题。我设法让它与 SVG 一起工作,这对我来说是新的。到目前为止,一切都很好。为了使它更好,我想在有人悬停在某个状态上时更改颜色/不透明度/填充。它会喜欢这样,所以当悬停在我没有关闭图像的状态上时不会发生任何事情,但是当它确实包含图像时会发生。为了简单起见,我制作了这个测试页面:https ://www.states.nl/TEST4.html表明我的意思。当前仅添加了“蒙大拿”。当然,我首先查看了互联网,但我认为可行的一切都没有。有人知道如何让这个为我工作吗?提前致谢!
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title><br>
</head>
<body>
<script>#backing {
vertical-align: middle;
margin: 0;
overflow: hidden;
}
#backing svg {
display: inline-block;
position: absolute;
top: 0; left: 0;
}
</script>
<figure id='backing'>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 1200 800" preserveAspectRatio="xMinYMin meet" >
<image width="1200" height="800" xlink:href="http://www.states.nl/img/locations/usa_map.png"></image>
<a xlink:href="/MT_relief.html" target="_parent">
<polygon id="Montana" points="238,38,253,41,259,41,259,42,265,42,265,43,272,43,303,48,311,48,353,54,363,54,363,55,372,55,372,56,382,56,382,57,392,57,392,58,401,58,401,59,411,59,411,60,421,60,421,61,433,61,433,62,448,62,448,63,475,64,465,180,427,179,416,179,392,178,392,177,356,175,356,174,346,174,346,173,336,173,336,172,306,169,305,170,303,181,301,179,299,179,297,176,288,177,288,176,286,176,286,175,275,176,275,175,273,175,273,174,271,174,271,173,263,167,261,158,260,158,260,156,257,154,257,152,255,151,254,146,253,146,253,144,252,144,251,141,244,140,243,132,243,128,244,128,246,124,248,124,249,115,242,111,241,106,240,106,240,104,239,104,238,95,234,92,234,90,233,90,234,81,232,80,234,64" fill="#fff" opacity="0"/><title>Montana</title>
</a>
</svg>
</figure>
</body>
</html>`
解决方案
The PNG has nothing to do with the solution, it is a background image.
You have to make the :hover over the Montana polygon work:
<style>
svg {
height: 300px;
background:grey;
}
</style>
<svg viewBox="0 0 1200 800">
<style>
polygon {
pointer-events: all;
cursor: pointer;
}
polygon:hover {
fill: hotpink;
}
</style>
<image href="//www.states.nl/img/locations/usa_map.png"></image>
<a href="/MT_relief.html" target="_parent">
<polygon id="Montana" points="238,38,253,41,259,41,259,42,265,42,265,43,272,43,303,48,311,48,353,54,363,54,363,55,372,55,372,56,382,56,382,57,392,57,392,58,401,58,401,59,411,59,411,60,421,60,421,61,433,61,433,62,448,62,448,63,475,64,465,180,427,179,416,179,392,178,392,177,356,175,356,174,346,174,346,173,336,173,336,172,306,169,305,170,303,181,301,179,299,179,297,176,288,177,288,176,286,176,286,175,275,176,275,175,273,175,273,174,271,174,271,173,263,167,261,158,260,158,260,156,257,154,257,152,255,151,254,146,253,146,253,144,252,144,251,141,244,140,243,132,243,128,244,128,246,124,248,124,249,115,242,111,241,106,240,106,240,104,239,104,238,95,234,92,234,90,233,90,234,81,232,80,234,64" fill="none"></polygon>
<title>Montana</title>
</a>
</svg>
And once you have the polygons for all States, you don't need the background image. Just set the fill color for every State.
Appeltje.. eitje..
推荐阅读
- java - 尝试使用 Microsoft Graph Java SDK 从共享 Outlook 日历中获取空闲/忙碌时段时出现错误 401
- raspberry-pi - 如何在树莓派上使用 omxh264enc 将图像传送到 gstreamer?
- python - 如何在kivy中将标签制作为按钮
- python - 吉普错误!堆栈错误:找不到 Python 可执行文件“C:\Users\Admin\Anaconda3\python.EXE”,您可以设置 PYTHON 环境变量
- c# - 如何使用 .net 核心实体框架迁移更新表(而不是重新创建它们)
- protocol-buffers - 向重复消息添加必填字段的问题
- c# - 是否有任何选项可以使用 MODBUS 协议更改我的设备从站 ID?
- java - 不旋转位图
- r - 如何将损失/指标更改为 mean_absolute_percentage_error (MAPE)?
- c++ - 如何解决这个前向类声明问题?