中文
Hover & pointer · Vanilla JS + CSS

Underline draw

下划线展开

Draw an underline as the pointer arrives.

Also searched as: 鼠标放上去,下划线从左边画过去, 链接下划线从左往右画出来, 导航链接悬停出下划线, 悬停时文字下面画一条线

AI Markdown (.md)
Placement
Navigation
Mood
Crisp
Trigger
悬停
Site types
个人网站, 博客, 品牌官网

How it works & accessibility

Scale or reveal an underline from its transform origin on hover and keyboard focus.

Best for: Navigation (悬停).

Reduced motion: 下划线直接出现、直接消失,不做画线动画

Mobile & touch: 手机上没有悬停:手指按下链接时出现下划线;当前所在的页面可以一直显示下划线

Key parameters

ParameterKeyDefaultRangeNotes
画线时长duration300ms——
线的粗细thickness2px——
缓动easingcubic-bezier(0.4, 0, 0.2, 1)——

Ready-to-paste AI prompt

Add “Underline draw” to the navigation in my project: Scale or reveal an underline from its transform origin on hover and keyboard focus.
Parameters: duration: 300ms, thickness: 2px, easing: cubic-bezier(0.4, 0, 0.2, 1).
Preserve existing content and layout. Respect prefers-reduced-motion by showing the final state. Content must remain visible if JavaScript fails or is disabled.
Reference: https://donglang.linter.top/effects/underline-draw.md (canonical specification and reference code).

Dependency-free reference code

CSS
/* 下划线展开(Underline draw)的样式,配合 underline-draw.js。线的颜色跟文字一样(currentColor),粗细由脚本按参数设置。
   链接要有定位(线按它来摆):原来没有定位的,脚本会补上 position: relative;原来是 absolute 这类的不动。 */
.ud-line {
  position: absolute;
  left: 0;
  right: 0;
  bottom: -3px;
  border-radius: 1px;
  background: currentColor;
  transform: scaleX(0);
  transform-origin: left;
  pointer-events: none;
}
.ud.is-on .ud-line { transform: scaleX(1); }
JavaScript
// 下划线展开(Underline draw)· 原生 JS,不依赖任何库。
// 改写自 StyleKit 的 underline-draw(MIT License,Copyright (c) 2026 AnxForever):动作和默认值来自它。
// links 是一个或一组链接(配合 underline-draw.css)。鼠标停上去或键盘选中时,下划线从左往右画出来;离开时往右收回。
// 返回 { draw(link), erase(link), destroy() };draw、erase 返回的 Promise 播完是 true,被打断是 false。中途离开时,从当前画到的位置收回,不跳。
// 开启"减少动态"时:下划线直接出现、直接消失。脚本没执行时链接照常显示,只是没有这条线。
// listen 为 false 时不监听鼠标和键盘,只用 draw / erase 自己控制(演示用)。ignoreReducedMotion 只给演示用。
export function underlineDraw(links, {
  duration = 300,
  thickness = 2,
  easing = 'cubic-bezier(0.4, 0, 0.2, 1)',
  listen = true,
  ignoreReducedMotion = false,
} = {}) {
  const reduce = !ignoreReducedMotion && matchMedia('(prefers-reduced-motion: reduce)').matches;
  const list = links instanceof Element ? [links] : [...links];
  const lines = new Map();
  const positions = new Map();
  list.forEach((link) => {
    const line = document.createElement('span');
    line.className = 'ud-line';
    line.setAttribute('aria-hidden', 'true');
    line.style.height = `${thickness}px`;
    // 线按链接来摆:链接没有定位时补上 relative(不改变位置);原来是 absolute 这类的不动
    positions.set(link, link.style.position);
    if (getComputedStyle(link).position === 'static') link.style.position = 'relative';
    link.classList.add('ud');
    link.append(line);
    lines.set(link, line);
  });

  // 画出来从左边开始,收回去往右边走;从当前的横向缩放开始,中途改主意也不跳
  const run = (link, on) => {
    const line = lines.get(link);
    if (!line || link.classList.contains('is-on') === on) return Promise.resolve(true);
    const from = new DOMMatrixReadOnly(getComputedStyle(line).transform).a;
    line.getAnimations().forEach((a) => a.cancel());
    link.classList.toggle('is-on', on);
    if (reduce) return Promise.resolve(true);
    const origin = on ? 'left' : 'right';
    return line.animate(
      [{ transform: `scaleX(${from})`, transformOrigin: origin }, { transform: `scaleX(${on ? 1 : 0})`, transformOrigin: origin }],
      { duration, easing },
    ).finished.then(() => true, () => false);
  };
  const draw = (link) => run(link, true);
  const erase = (link) => run(link, false);

  // 鼠标停着、或者用键盘选中(:focus-visible)时画,两样都没有了才收回:键盘选中后鼠标划过再移开,线不会被收掉。
  // 鼠标点击带来的焦点不算,免得点完移开线还留着。手机上手指按下时出现、抬起时收回(触屏也会发 pointerenter、pointerleave)
  const events = [];
  if (listen) {
    list.forEach((link) => {
      let hover = false;
      let focus = false;
      const update = () => (hover || focus ? draw(link) : erase(link));
      [
        ['pointerenter', () => { hover = true; update(); }],
        ['pointerleave', () => { hover = false; update(); }],
        ['focus', () => { focus = link.matches(':focus-visible'); update(); }],
        ['blur', () => { focus = false; update(); }],
      ].forEach(([type, fn]) => {
        link.addEventListener(type, fn);
        events.push([link, type, fn]);
      });
    });
  }

  return {
    draw,
    erase,
    destroy() {
      events.forEach(([link, type, fn]) => link.removeEventListener(type, fn));
      lines.forEach((line, link) => { line.remove(); link.style.position = positions.get(link); link.classList.remove('ud', 'is-on'); });
    },
  };
}
HTML
<!-- 下划线展开:给一组链接调用一次 -->
<nav class="site-nav">
  <a href="/work">作品</a>
  <a href="/posts">文章</a>
  <a href="/about">关于</a>
</nav>
<script type="module">
  import { underlineDraw } from './underline-draw.js';
  underlineDraw(document.querySelectorAll('.site-nav a'));
</script>

More in Hover & pointer

Browse all 86 effects →

Shown once per release · Always available in changelog