javascript - 悬停时动画 SVG - 悬停时不要做微笑
问题描述
我有一个 SVG 文件,其中点在直线上。我想要实现的是当用户将鼠标悬停在 svg 文件上时,点会变成像附加图像一样的微笑。
过渡需要平稳。
这里最好的方法是什么?我可以只使用 css 还是我也应该使用 js?
谢谢
解决方案
使用SMIL 动画,无需任何脚本即可实现。它将三次贝塞尔路径从直线变为弯曲并返回。动画由位于线条顶部的透明矩形上的事件mouseover
触发。mouseout
该行本身使用了两个小技巧的组合:您可以将 a 设置pathLength
为属性,stroke-dasharray
然后根据它计算破折号长度。对于 astroke-dasharray: 0 1
与 的组合stroke-linecap
,长度为零的破折号显示为点,笔划宽度为它们的直径。只需使用pathLength
和stroke-width
更改点的距离及其大小即可。
#line {
fill: none;
stroke: black;
stroke-width: 4;
stroke-dasharray: 0 1;
stroke-linecap: round;
}
#overlay {
opacity: 0;
}
<svg viewBox="0 0 100 100" width="150">
<path id="line" d="M 10 50 C 35 50 65 50 90 50" pathLength="15">
<animate attributeName="d" begin="overlay.mouseover" dur="0.3s"
fill="freeze" restart="whenNotActive"
from="M 10 50 C 35 50 70 50 90 50"
to="M 15 30 C 20 70 80 70 85 30"/>
<animate attributeName="d" begin="overlay.mouseout" dur="0.3s"
fill="freeze" restart="whenNotActive"
from="M 15 30 C 20 70 80 70 85 30"
to="M 10 50 C 35 50 65 50 90 50"/>
</path>
<rect id="overlay" width="100%" height="100%" />
</svg>
推荐阅读
- java - How can I access the text of the SMS sent thru Twilio to my Spring Boot Controller?
- godot - Godot - 父母可以与其孩子共享 CollisionShape 吗?
- html - 如何制作圆形边框角(不是圆角)
- python - 如何在任何 JIRA 上添加仅限团队成员的评论
- arduino-esp8266 - 将 Aurdiuno ESP-01 wifi 与 LED 矩阵 MAX7219 连接
- json - 如何在按钮 id 中传递一个变量,然后将该 id 发送到 ajax 数据?
- javascript - TypeError:不是构造函数
- laravel - 如何同时使用 Laravel 认证和 vue-router
- java - 在双变量点后删除数字(Java)
- c - 为什么 getchar() 返回垃圾值?