HTML SVG 和 CSS路径动画

要使箭头沿着整个路径来回移动,可以使用 SVG 和 CSS 动画。这种方法可以更精确地控制路径和动画。以下是一个完整的示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Arrow Animation</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f0f0f0;
        }

        .path {
            width: 300px;
            height: 300px;
        }

        .arrow {
            fill: red;
            animation: moveAlongPath 4s linear infinite alternate;
        }

        @keyframes moveAlongPath {
            0% {
                offset-distance: 0%;
            }
            100% {
                offset-distance: 100%;
            }
        }
    </style>
</head>
<body>
    <svg class="path" viewBox="0 0 300 300">
        <path id="linePath" d="M10 10 L290 10 L290 290 L10 290 Z" stroke="black" stroke-width="2" fill="none"/>
        <circle r="5" class="arrow">
            <animateMotion repeatCount="indefinite" dur="4s" keyPoints="0;1" keyTimes="0;1">
                <mpath href="#linePath" />
            </animateMotion>
        </circle>
    </svg>
</body>
</html>
  1. 使用了一个 SVG 元素来定义路径。<path> 元素定义了一个矩形路径。
  2. <circle> 元素中使用 <animateMotion> 来沿着路径动画移动。
  3. dur="4s" 指定动画持续时间,repeatCount="indefinite" 使动画无限循环。

这个方法使得箭头能够沿着指定的路径平滑地移动,并且可以适应任意复杂的路径形状。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值