为何出现‘direction不存在于LenisOptions类型’的TypeScript错误?
问题解答
这个TypeScript错误的核心原因是:你当前使用的@studio-freight/lenis版本中,LenisOptions类型定义已经移除了direction配置项——该属性在Lenis的v1及以上版本里被废弃,因为组件默认就是垂直滚动模式,无需额外声明。
解决步骤:
直接从Lenis的初始化参数中删除direction: "vertical"这一行即可,修改后的代码如下:
"use client"; import { ReactNode, useEffect } from "react"; import Lenis from "@studio-freight/lenis"; export default function SmoothScroll({ children }: { children: ReactNode }) { useEffect(() => { const lenis = new Lenis({ duration: 1.2, easing: (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)), smooth: true, smoothTouch: false, touchMultiplier: 2, }); function raf(time: number) { lenis.raf(time); requestAnimationFrame(raf); } requestAnimationFrame(raf); return () => { lenis.destroy(); }; }, []); return <>{children}</>; }
如果你的需求是实现水平滚动,新版本的Lenis不需要通过direction配置,只需给目标滚动容器设置CSS属性overflow-x: auto并确保容器有足够的宽度,Lenis会自动适配滚动方向。
内容的提问来源于stack exchange,提问作者Tasfiq.Asif
相关产品推荐
相关产品推荐

