# 琴弦 Elastic Line > 分割线被鼠标划过时像琴弦一样被拨弯,滑脱后来回弹几下再停。 分类:鼠标特效 · 角色:点缀(贴在某个元素上,一页两三个) [全部组件和流程](https://www.dachuiai.com/llms.txt) · [鼠标特效选型规则](https://www.dachuiai.com/guides/cursor.md) ## 给 AI:用户把这个组件发给了你 1. 先看用户的项目:框架、页面类型、现在的风格、手机还是电脑为主。 2. 对照下面的「适合 / 不适合」判断合不合适。不合适就直接说,并从[鼠标特效选型规则](https://www.dachuiai.com/guides/cursor.md)或[全部组件](https://www.dachuiai.com/llms.txt)里另挑一个。 3. 合适就按「装进项目」装好,示例里的文字和图片换成用户自己的。 4. 告诉用户用了什么、放在哪个文件、以后想改快慢、颜色、大小改哪个参数。 ## 什么时候用 - 适合:杂志、作品集、内容站的目录和列表分割线,页脚的横线。 - 不适合:表格和后台列表(会分心);分割线又多又密(一页 3 到 8 条够了);严肃的金融、医疗网站。 - 适合的网站:作品集、内容站、官网 - 放在:列表、页脚 - 手机上:没效果:手机上是普通的线。 - 性能代价:低:只在被拨动时更新。 - 常搭配:[滚动出现 Scroll Reveal](https://www.dachuiai.com/c/scroll-reveal.md)、[悬停预览 Hover Reveal](https://www.dachuiai.com/c/hover-reveal.md) ## 装进项目 - 写法:React 18 以上 + TypeScript + Tailwind CSS v4,一个文件就能用,不引用别的组件。 - 依赖:只用 react,不用另外安装 - 文件:原样存成 `elastic-line.tsx`(比如 `src/components/elastic-line.tsx`)。能跑命令就下载[原文](https://www.dachuiai.com/code/elastic-line.tsx),比手抄准:`curl -fsSL -o src/components/elastic-line.tsx https://www.dachuiai.com/code/elastic-line.tsx`(Windows 写 `curl.exe`)。下载完看一眼第一行是 `import`,不是网页。 - 导出:`ElasticLine` 是可以直接用的部分;最后的 `export default` 是演示示例,只作参考。 - 注意:宽度跟着外面的容器。它本身高度是 0,弯出去的部分画在外面、不占位置;外面不要用 overflow: hidden 把弯出去的部分切掉。 - 保留代码里 `prefers-reduced-motion` 的处理:用户开了「减弱动态效果」时内容照样完整显示。 - Next.js(有 app 文件夹):文件第一行加 `'use client';`。 - 用 JavaScript 不用 TypeScript:Vite 和 Next.js 都能直接用 .tsx 文件;也可以删掉类型标注,改名 .jsx。 - 没有 Tailwind:先装 Tailwind CSS v4(Vite 项目 `npm i tailwindcss @tailwindcss/vite`,vite 配置的 plugins 里加 `tailwindcss()`,入口引入的那个 CSS 文件(一般是 `src/index.css`)第一行写 `@import "tailwindcss";`),或者把类名改写成项目用的样式。 - 已经装了 Tailwind v3:别为了一个组件升级。类名大多通用,少数 v4 写法要换,比如焦点框 `outline-2` 在 v3 里要写成 `outline outline-2`。 - 不是 React(Vue、Svelte、小程序、纯 HTML):照着代码的做法改写,时长、曲线、参数保持一样。Wix、有赞这类建站平台装不进代码,告诉用户去平台自带的动效设置里找。 ## 参数 | 参数 | 默认值 | 作用 | | --- | --- | --- | | `className` | `''` | 加在最外层的类名,用来调大小、位置、间距 | | `pull` | `74` | 拉多远会滑脱(px) | | `stiffness` | `430` | 弦有多紧,越大弹得越快 | | `damping` | `5.4` | 阻尼,越小弹得越久 | | `color` | `'currentColor'` | 线的颜色 | | `thickness` | `2` | 线的粗细(px) | ## 你可以说 > 用 SVG 画一条横线:鼠标划过时把线拨弯,离开后像琴弦一样来回弹几下再停住。 ## 完整代码 ```tsx import {useEffect, useRef, useState} from 'react'; /* --------------------------------------------------------------- * ElasticLine 琴弦 * 一条 SVG 横线(二次曲线,只有一个控制点):鼠标从线上划过时把线勾住、带弯, * 拉过一定距离(或滑出两头)就滑脱,线像琴弦一样来回弹几下再停住(欠阻尼弹簧)。 * 宽度跟着外面的容器走,一页放几条都行;没在弹的时候就停下,不占动画。 * 只在有鼠标的设备上能拨;手机、平板上就是一条普通的线。开了「减弱动态效果」松开后直接回直,不来回弹。 * pull:拉多远会滑脱(px) * stiffness:弦有多紧,越大弹得越快 * damping:阻尼,越小弹得越久 * color:线的颜色 * thickness:线的粗细(px) * ------------------------------------------------------------- */ type Props = {className?: string; pull?: number; stiffness?: number; damping?: number; color?: string; thickness?: number}; export function ElasticLine({className = '', pull = 74, stiffness = 430, damping = 5.4, color = 'currentColor', thickness = 2}: Props) { const root = useRef(null); const line = useRef(null); const [width, setWidth] = useState(0); useEffect(() => { const el = root.current!; const ro = new ResizeObserver(() => setWidth(el.clientWidth)); ro.observe(el); return () => ro.disconnect(); }, []); useEffect(() => { if (!width || !matchMedia('(pointer: fine)').matches) return; const el = root.current!, p = line.current!; const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches; const k = stiffness, c = reduce ? 2 * Math.sqrt(stiffness) : damping; // 减弱动态效果:临界阻尼,直接回直 const s = {cx: width / 2, off: 0, v: 0, held: false, mx: 0, my: 0}; let raf = 0, prev = 0, last: {x: number; y: number} | null = null; const draw = () => p.setAttribute('d', `M0 0Q${s.cx.toFixed(1)} ${s.off.toFixed(2)} ${width} 0`); const frame = (now: number) => { const dt = Math.min(0.05, Math.max(0, now - prev) / 1000); prev = now; if (s.held) { // 勾着的时候,控制点放在两倍的位置,曲线的顶点正好在鼠标下面 s.cx = Math.min(width - 30, Math.max(30, s.mx)); const to = s.my * 2; s.v = ((to - s.off) / Math.max(dt, 1e-3)) * 0.25; s.off = to; } else { for (let i = 0, n = Math.ceil(dt / 0.004), h = dt / n; i < n; i++) { s.v += (-k * s.off - c * s.v) * h; s.off += s.v * h; } } draw(); const resting = !s.held && Math.abs(s.off) < 0.05 && Math.abs(s.v) < 0.5; if (resting) { s.off = 0; s.v = 0; draw(); raf = 0; } else raf = requestAnimationFrame(frame); }; const kick = () => { if (!raf) { prev = performance.now(); raf = requestAnimationFrame(frame); } }; const move = (e: PointerEvent) => { if (e.pointerType === 'touch') return; const r = el.getBoundingClientRect(), x = e.clientX - r.left, y = e.clientY - r.top; s.mx = x; s.my = y; // 勾住:鼠标这一下正好从线的一边划到另一边 if (!s.held && last && x > 0 && x < width && last.y !== y && last.y * y <= 0) { s.held = true; s.v = 0; kick(); } // 滑脱:拉得太远,或者滑出了两头 if (s.held && (Math.abs(y) > pull || x < 0 || x > width)) s.held = false; if (s.held) kick(); last = {x, y}; }; const out = () => { last = null; if (s.held) { s.held = false; kick(); } }; draw(); addEventListener('pointermove', move, {passive: true}); document.documentElement.addEventListener('pointerleave', out); return () => { cancelAnimationFrame(raf); removeEventListener('pointermove', move); document.documentElement.removeEventListener('pointerleave', out); }; }, [width, pull, stiffness, damping]); return (
); } /* ---------------- 示例:杂志网站的目录页 ---------------- */ const SERIF = '"Noto Serif SC", "Source Han Serif SC", "Songti SC", "STSong", serif'; const DISPLAY = '"Playfair Display", "Didot", "Bodoni 72", Georgia, serif'; const GROTESK = '"Space Grotesk", "SF Pro Display", ui-sans-serif, system-ui, sans-serif'; const PAPER = `url("data:image/svg+xml;utf8,")`; const ROWS = [ {no: '01', zh: '慢一点的城市', en: 'Slow Cities', page: 'P. 08'}, {no: '02', zh: '手艺人', en: 'The Makers', page: 'P. 24'}, {no: '03', zh: '一张桌子的故事', en: 'One Table', page: 'P. 42'}, {no: '04', zh: '周末去山里', en: 'Into the Hills', page: 'P. 60'}, ]; export default function ElasticLineDemo() { return (

目录

Contents 2026 · 秋
); } ```