You need to enable JavaScript to run this app. 如何为SVG元素实现仅:hover状态显示的下拉阴影效果
优惠活动
大模型
产品
解决方案
定价
更多

如何为SVG元素实现仅:hover状态显示的下拉阴影效果

SVG元素hover触发阴影的实现方案

核心逻辑

inline的SVG属性无法绑定:hover伪类实现状态切换,因此将原本写在path上的阴影滤镜,改由CSS选择器控制启用时机,即可实现仅hover时显示阴影的效果。

修改步骤

  • 删除<path>标签上的内联属性filter="url(#poly)"
  • 在CSS中为a#sun默认设置filter: none,为a#sun:hover设置filter: url(#poly)
  • 原有伪类选择器a:hover#sun可调整为a#sun:hover,书写更规范,选择器优先级完全一致

修改后的完整代码

<!DOCTYPE html>
<html>
    <head>
        <link href="https://fonts.googleapis.com/css2?family=Unica+One&display=swap" rel="stylesheet">
    </head>
    <style>
        svg {
            border: 3px solid red;
        }
        text {
            font-family: 'Unica One', cursive;
            font-size:25px;
        }
        a#sun {
            fill: #D8A089;
            filter: none;
        }
        a#sun:hover {
            fill:#FBA618;
            filter: url(#poly);
        }
        a#seagull:hover {
            opacity: .8;
        }
    </style>
<body>
<svg width="185" height="200"> 
    <defs>
        <filter id="poly" x="0" y="0" width="200%" height="200%"> 
          <feOffset result="offOut" in="SourceGraphic" dx="15" dy="15" />
          <feGaussianBlur result="blurOut" in="offOut" stdDeviation="5" />
          <feBlend in="SourceGraphic" in2="blurOut" mode="normal" /> 
        </filter>
    </defs>
    <a id="sun" href="#" target="_blank">
        <path d="M145,122 a65,65 0 1,0 -115,0"></path>
    </a>
    <a id="seagull" href="#" target="_blank">
        <polygon points="0,0 8,16 40,22" style="fill:#494949" />
        <polygon points="8,16 40,22 80,41 45,42 " style="fill:#E3E4DF" />
        <polygon points="80,41 45,42 60,65" style="fill:#EFEFEF" />
        <polygon points="80,41 60,65 70,90 100,70" style="fill:#EBEAE5" />
        <polygon points="80,75 45,95 50,105 110,105" style="fill:#E3E4DF" />
        <polygon points="45,95 10,100 50,105" style="fill:#535257" />
        <polygon points="80,75 110,105 120, 90" style="fill:#EAE9E4" />
        <polygon points="120,90 80,75 110,55 130,60 " style="fill:#EFEFEF" />
        <polygon points="110,55 130,60 132,50" style="fill:#EBEAE5" />
        <polygon points="132,50 130,60 155, 48" style="fill:#E4E5E0" />
        <polygon points="155, 48 165, 35 140, 49.5" style="fill:#505052" />
        <polygon points="120,90 123,80 136,86 135,89 " style="fill:#3F3F3F" />
        <polygon points="136,86 135,89 141,88 " style="fill:#C55F48" />
    </a>
    <g fill="none" stroke="black" stroke-width="2">
        <path stroke-dasharray="20,10,5,5,5,10" stroke-linecap="round" d="M5 125 l170 0" />
        <path stroke-dasharray="20,10,5,5,5,10" stroke-linecap="round" d="M25 135 l140 0" />
        <path stroke-dasharray="20,10,5,5,5,10" stroke-linecap="round" d="M50 145 l90 0" />
    </g>
    <text x="85" y="170" style="fill:black;">R
        <tspan x="85" y="190">C</tspan>
    </text>
</svg>
</body>
</html>

补充说明

  • 滤镜直接应用在父级<a id="sun">上即可,SVG的<a>标签原生支持hover事件,效果和直接加在path上无区别
  • 原有滤镜的宽高设置无需修改,预留的200%扩展区域足够容纳偏移、模糊后的阴影,不会被SVG画布裁切
  • 所有交互态的样式切换都需要放在CSS规则中实现,不要尝试在内联style里写伪类,浏览器不会识别

内容的提问来源于stack exchange,提问作者Richard Chen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.19 20:45:04