feat: 添加滚动环境和媒体状态管理,优化头部组件功能,支持背景音乐和主题切换
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
"use client";
|
||||
|
||||
/**
|
||||
* Scroll environment — module singleton mirroring the original
|
||||
* `scrollEnv` (module 29680): holds the Lenis instance so header
|
||||
* shortcuts (coords click → top, [T] top, [B] bottom, Work/Contact
|
||||
* anchors) can scroll from anywhere. SmoothScroller registers the
|
||||
* instance on mount.
|
||||
*/
|
||||
|
||||
type LenisLike = {
|
||||
scrollTo: (
|
||||
target: number | string | HTMLElement,
|
||||
opts?: Record<string, unknown>
|
||||
) => void;
|
||||
} | null;
|
||||
|
||||
let lenis: LenisLike = null;
|
||||
|
||||
export const scrollEnv = {
|
||||
setLenis(instance: LenisLike) {
|
||||
lenis = instance;
|
||||
},
|
||||
getLenis() {
|
||||
return lenis;
|
||||
},
|
||||
scrollToTop(behavior: "smooth" | "auto" = "smooth") {
|
||||
if (lenis) lenis.scrollTo(0, { lerp: 0.1, immediate: behavior === "auto" });
|
||||
else window.scrollTo({ top: 0, behavior });
|
||||
},
|
||||
scrollToBottom(behavior: "smooth" | "auto" = "smooth") {
|
||||
const limit = Math.max(
|
||||
0,
|
||||
(document.documentElement?.scrollHeight ?? 0) - window.innerHeight
|
||||
);
|
||||
if (lenis) lenis.scrollTo(limit, { lerp: 0.1, immediate: behavior === "auto" });
|
||||
else window.scrollTo({ top: limit, behavior });
|
||||
},
|
||||
scrollToSection(selector: string) {
|
||||
const el = document.querySelector(selector);
|
||||
if (!el) return;
|
||||
if (lenis) lenis.scrollTo(el as HTMLElement, { lerp: 0.1 });
|
||||
else (el as HTMLElement).scrollIntoView({ behavior: "smooth" });
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user