You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现U型虚线边框的持续左右循环动画效果?

U型虚线边框左右循环移动动画实现方案

直接用原生border实现虚线位移会因缺失上边框导致效果不协调,推荐用背景渐变模拟虚线,配合background-position实现平滑的左右循环移动效果,代码如下:

.curved-dots-path {
  align-self: center;
  width: 70%;
  height: 25rem;
  margin-top: -30rem;
  border-top: none;
  border-radius: 0 0 50% 50%;
  /* 线性渐变绘制虚线:10px实线+10px空白 */
  background-image: linear-gradient(to right, var(--org-primary-color) 50%, transparent 50%);
  background-size: 20px 5px;
  /* 定位背景覆盖下、左、右三边,模拟U型 */
  background-position: 0 100%, 0 0, 100% 0;
  background-repeat: repeat-x, repeat-y, repeat-y;
  /* 添加循环移动动画 */
  animation: dashMove 2s linear infinite;
}

/* 定义动画:通过改变背景位置实现虚线整体移动 */
@keyframes dashMove {
  0% {
    background-position: 0 100%, 0 0, 100% 0;
  }
  100% {
    background-position: 20px 100%, 20px 0, calc(100% - 20px) 0;
  }
}

调整说明:

  • 若要修改虚线粗细,调整background-size的第二个数值(当前是5px)
  • 若要修改虚线间距/长度,调整background-size的第一个数值(当前总尺寸20px,对应10px实线+10px空白)
  • 若要调整动画速度,修改animation中的时长(当前是2s)

内容的提问来源于stack exchange,提问作者Horthman Odame

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 07:52:10