For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt, and this page is available as Markdown at /next/zh/ui/motion.md.
lynx-ui logo
lynx-ui
  • 简体中文
  • @lynx-js/motion

    @lynx-js/motion 是一个轻量适配层,直接复用 Motion 的源码,将其命令式 JavaScript API 带到 Lynx。所有 API 都运行在主线程脚本环境中,动画可以立即更新视觉属性,无需经过后台线程中转。如果你在 Web 上用过 Motion 的 animate()motionValue()spring,在 Lynx 中的用法完全一样。如果你还不熟悉 Motion,可以先阅读 Motion 快速上手指南

    什么是 Mini 入口,什么时候应该使用它?

    这个包提供两个入口。完整入口尚未达到与 Motion 的完全特性对等,且引擎版本要求较高。Mini 是裁剪版本,兼容性更广,也是目前 lynx-ui 使用的入口。

    @lynx-js/motion@lynx-js/motion/miniMini
    Animation数字、颜色、带单位字符串、关键帧数组仅数字
    Motion valuesmotionValue()useMotionValue 暂不支持)useMotionValueRef()
    Springtype: 'spring'type: 'spring'
    Staggerstagger() + 选择器--
    styleEffect从 motion value 派生样式--
    Lynx Engine>= 3.5无最低要求
    ReactLynx>= 0.115.2无最低要求
    Bundle 体积~275 KB~135 KB

    安装

    npm
    yarn
    pnpm
    bun
    deno
    npm install @lynx-js/motion

    从主包入口导入:

    import { animate, motionValue, styleEffect } from '@lynx-js/motion';

    为元素添加动画

    animate() 函数接受与 Web 端 Motion 相同的选项,包括 durationeasedelayrepeattype: 'spring'。完整选项列表见 Motion animate() 参考

    常见用法如下:

    1. 使用 useMainThreadRef() 创建主线程元素引用。
    2. 通过 main-thread:ref 绑定到元素。
    3. 在主线程函数中调用 animate() 启动动画。
    4. 将动画控制对象保存在另一个主线程引用中,方便清理时调用 stop()

    使用弹簧动画

    当值需要以弹簧物理效果收敛,而不是按固定时长的缓动曲线变化时,设置 type: 'spring'。Motion 的所有 spring 选项均受支持,包括 stiffnessdampingmassbounce

    使用 MotionValue

    motionValue() 用于保存值,可以直接传给 animate() 做动画,并且可以订阅变化。当多个效果或事件处理函数需要共享同一个动画值时,可以使用它。

    构建手势效果

    对于高频触摸输入,应使用 main-thread:bindtouchstartmain-thread:bindtouchmovemain-thread:bindtouchend 将事件处理保留在主线程。再结合 MotionValue 和 styleEffect(),从手势状态派生视觉状态。

    Info

    Motion 的 Web 专属手势 API,如 scroll()inView()hover()press(),不包含在 @lynx-js/motion 中。请使用上方展示的主线程触摸事件。

    除非另有说明,本项目采用知识共享署名 4.0 国际许可协议进行许可,代码示例采用 Apache License 2.0 许可协议进行许可。