11<script setup>
22import DefaultTheme from " vitepress/theme" ;
3- import { onMounted } from " vue" ;
4- import { useRouter } from " vitepress" ;
3+ import { onMounted , onUnmounted , watch } from " vue" ;
4+ import { useRouter , useData } from " vitepress" ;
55import mediumZoom from " medium-zoom" ;
66
77const { Layout } = DefaultTheme;
88const router = useRouter ();
9+ const { frontmatter } = useData ();
910
1011// Setup medium zoom with the desired options
1112const setupMediumZoom = () => {
@@ -14,11 +15,79 @@ const setupMediumZoom = () => {
1415 });
1516};
1617
18+ // Mark html with home class for CSS to work immediately
19+ const updateHomeClass = () => {
20+ const isHome = frontmatter .value .layout === ' home' ;
21+ if (isHome) {
22+ document .documentElement .classList .add (' is-home-page' );
23+ document .body .classList .add (' is-home-page' );
24+ } else {
25+ document .documentElement .classList .remove (' is-home-page' );
26+ document .body .classList .remove (' is-home-page' );
27+ }
28+ };
29+
30+ // Handle navbar scroll state on home page with gradual opacity
31+ const handleScroll = () => {
32+ const navbar = document .querySelector (' .VPNavBar' );
33+ if (! navbar) return ;
34+
35+ const isHome = frontmatter .value .layout === ' home' ;
36+ if (! isHome) {
37+ navbar .classList .remove (' scrolled' );
38+ navbar .style .removeProperty (' --navbar-opacity' );
39+ return ;
40+ }
41+
42+ const scrollY = window .scrollY ;
43+ const fadeStart = window .innerHeight * 0.25 ;
44+ const fadeEnd = window .innerHeight * 0.85 ;
45+
46+ if (scrollY <= fadeStart) {
47+ navbar .style .setProperty (' --navbar-opacity' , ' 0' );
48+ navbar .classList .remove (' scrolled' );
49+ } else if (scrollY >= fadeEnd) {
50+ navbar .style .setProperty (' --navbar-opacity' , ' 1' );
51+ navbar .classList .add (' scrolled' );
52+ } else {
53+ const progress = (scrollY - fadeStart) / (fadeEnd - fadeStart);
54+ navbar .style .setProperty (' --navbar-opacity' , progress .toString ());
55+ navbar .classList .remove (' scrolled' );
56+ }
57+ };
58+
59+ const setupNavbarTransparency = () => {
60+ updateHomeClass ();
61+ handleScroll ();
62+ window .addEventListener (' scroll' , handleScroll);
63+ };
64+
65+ const cleanupNavbarTransparency = () => {
66+ window .removeEventListener (' scroll' , handleScroll);
67+ document .documentElement .classList .remove (' is-home-page' );
68+ document .body .classList .remove (' is-home-page' );
69+ const navbar = document .querySelector (' .VPNavBar' );
70+ if (navbar) {
71+ navbar .classList .remove (' scrolled' );
72+ }
73+ };
74+
1775// Apply medium zoom on load
18- onMounted (setupMediumZoom);
76+ onMounted (() => {
77+ setupMediumZoom ();
78+ setupNavbarTransparency ();
79+ });
1980
20- // Subscribe to route changes to re-apply medium zoom effect
21- router .onAfterRouteChanged = setupMediumZoom;
81+ onUnmounted (() => {
82+ cleanupNavbarTransparency ();
83+ });
84+
85+ // Subscribe to route changes
86+ router .onAfterRouteChanged = () => {
87+ setupMediumZoom ();
88+ updateHomeClass ();
89+ setTimeout (handleScroll, 50 );
90+ };
2291 </script >
2392
2493<template >
0 commit comments