html - 如何更改 html 和 css 中 svg 文件中每个路径的颜色?
问题描述
我有这个地图代码,我想更改每个州的颜色。我的意思是:这个 svg 用于地图,当我fill
在 css fpr 中使用路径标签时,地图中有很多状态,它使所有状态都具有相同的颜色,当在每个路径标签中使用它时,它不起作用。你能帮我吗?
这是CSS
<style>
*{
margin: 0;
padding: 0;
}
body{
background-color: #01171b;
}
svg path{
fill:#949596;
stroke:"#ccc";
stroke-width:.25;
}
svg path:hover {
fill:#698994;
transition: 0.6;
cursor: pointer;
}
.description{
pointer-events: none;
position: absolute;
font-size: 18px;
text-align: center;
background: white;
padding: 10px 15px;
z-index: 5;
height: 30px;
line-height: 30px;
margin: 0 auto;
color: #21669e;
border-radius: 5px;
box-shadow: 0 0 0 1px #eee;
display: none;
}
.description.active{
display: block;
}
.description:after{
content: '';
position: absolute;
left: 50%;
top: 100%;
width: 0;
height: 0;
margin-left: -10px;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-top: 10px solid white;
}
这是JS
$description = $(".description");
$('.enabled').hover(function() {
$(this).attr("class", "enabled heyo");
$description.addClass('active');
$description.html($(this).attr('id'));
}, function() {
$description.removeClass('active');
});
$(document).on('mousemove', function(e){
$description.css({
left: e.pageX,
top: e.pageY - 70
});
});
这是 HTML
<?xml version="1.0"?>
<svg style="margin-left: 350px;" width="1100px" viewbox="0 0 1000 825">
<a xlink:title="نجران" class="enabled"xlink:href="https://www.google.com/search?q=%D9%86%D8%AC%D8%B1%D8%A7%D9%86">
<path d="M338 797.8l-0.6 0-0.3-0.2-0.4-0.6-0.4-0.4 0.4-0.6 1.1-1.4 0.6 0.1 0.5 0.4 0.7 0.8 0 0.2 0.2 0.7-0.4 0.5-0.7 0.4-0.7 0.1z m-216.5-409.5l-0.2 0.6-0.6-0.3-0.8-0.3-0.4 0-0.2 0.1 0-0.9 0.2-0.6 0.5 0 0.5 0.3 1 1.1z m-63.3-112.6l0.2 0.5-0.9-1.1-0.7-0.7 0-0.7-0.1-0.5 0.3-0.1 0.9 1.4 0.3 0.8 0 0.4z" id="SAU99" name="">
</path>
<path fill=""d="M610.1 788.2l-0.1 0.1-5.4 3.2-5.8 3.4-1.4 0.6-1.5 0.2-6.9-0.3-0.6-0.4-4-5.6-6.6-9.2-1.8-1-18.3 2.5-20.5-2.1-11.8-1.2-10-1-2.2-0.8-7.7-4.5-2.6-0.5-15-0.1-9.6-0.1-1.1 0.2-2.9 1.2-1.4 0-3.2-0.6-2.9-0.1-4.6 0.4-3.6 0.9-0.9 0.1-2-0.6-1-0.2-0.7 0.6-0.7 1.2-0.7 0.9-0.9 0.4-1.2-0.2-1.9-0.9-0.8-0.1-1 0.5-0.7 0.9-0.6 1-0.8 0.7-1.1 0.2-0.5-0.2-0.3-0.1-0.4-0.6 0.2-1 0-0.6-0.3-0.1-1.1 0.3-0.9 0.1-1.8-0.2-1.8 0.2-0.9 0-0.9-0.3-2-1.6-2.4-2.7 0-1.6 1.3-5-0.1-2.6-0.9-3.1-0.2-1.2 0.1-1.3 0.4-2 1.2-11.1-0.1-2.1-0.5-2-0.7-2.2-0.5-2.2 0-1.3 0.5-1.4 0.9-1.8 1.2-1.6 7.7-8.1 5.3-4.6 1.5-1.6 0.6-0.8 0.6-1 1.1-2.7 0.6-1.2 1-1.2 1.2-0.7 1.1-0.5 3.2-0.7 2.1-0.7 2.2-1.1 2.1-1.4 0.9-0.9 0.7-0.9 0.6-1 0.5-1.3 0.3-1.3 0.2-1.9 0-1.7-0.3-2.3-1.6-6.4 0-1.2 0.3-1.3 1.1-1.8 1.1-1 3.2-1.6 0.7-0.6 0.5-0.7 0.3-1.1 0.2-1.6 0-6.4 13.2 8.3 3.4 1.7 2.5 1 4.5 1.1 12.2 0.8 111-12 6.2-1.8-15 121.8z" id="SAU1096" name="Najran">
</path></a>
</svg>
<div class="description"></div>
</body>
解决方案
您可以fill
为路径标签添加属性,例如:
<path fill="red" d="M610.1 788.2l-0.1 0.1-5.4 3.2-5.8 3.4-1.4 0.6-1.5 0.2-6.9-0.3-0.6-0.4-4-5.6-6.6-9.2-1.8-1-18.3 2.5-20.5-2.1-11.8-1.2-10-1-2.2-0.8-7.7-4.5-2.6-0.5-15-0.1-9.6-0.1-1.1 0.2-2.9 1.2-1.4 0-3.2-0.6-2.9-0.1-4.6 0.4-3.6 0.9-0.9 0.1-2-0.6-1-0.2-0.7 0.6-0.7 1.2-0.7 0.9-0.9 0.4-1.2-0.2-1.9-0.9-0.8-0.1-1 0.5-0.7 0.9-0.6 1-0.8 0.7-1.1 0.2-0.5-0.2-0.3-0.1-0.4-0.6 0.2-1 0-0.6-0.3-0.1-1.1 0.3-0.9 0.1-1.8-0.2-1.8 0.2-0.9 0-0.9-0.3-2-1.6-2.4-2.7 0-1.6 1.3-5-0.1-2.6-0.9-3.1-0.2-1.2 0.1-1.3 0.4-2 1.2-11.1-0.1-2.1-0.5-2-0.7-2.2-0.5-2.2 0-1.3 0.5-1.4 0.9-1.8 1.2-1.6 7.7-8.1 5.3-4.6 1.5-1.6 0.6-0.8 0.6-1 1.1-2.7 0.6-1.2 1-1.2 1.2-0.7 1.1-0.5 3.2-0.7 2.1-0.7 2.2-1.1 2.1-1.4 0.9-0.9 0.7-0.9 0.6-1 0.5-1.3 0.3-1.3 0.2-1.9 0-1.7-0.3-2.3-1.6-6.4 0-1.2 0.3-1.3 1.1-1.8 1.1-1 3.2-1.6 0.7-0.6 0.5-0.7 0.3-1.1 0.2-1.6 0-6.4 13.2 8.3 3.4 1.7 2.5 1 4.5 1.1 12.2 0.8 111-12 6.2-1.8-15 121.8z" id="SAU1096" name="Najran">
</path></a>
推荐阅读
- php - 移动框架时的数据库迁移
- javascript - 返回实例化类 + babel JS
- python - Python 3.6 中没有名为“Matplotlib”的模块错误
- css - 最大宽度媒体查询是否意味着“最多并包括”?
- selenium - 控制台中的“未找到测试。没有运行”消息,无法理解原因
- c# - 如何将 RGB 转换为浮点值?
- netsuite - 使用项目字段填充netsuite中的事务字段
- vb.net - 如何在 ML.Net 管道中将 R4 转换为 U1?
- google-apps-script - 谷歌表格 - 使用列标题的脚本?
- amazon-web-services - 缺少 AWS 负载均衡器安全组