Skip to content

Commit 0c2bcca

Browse files
author
15363205631
committed
layout - optimize header scroll.
1 parent 73cd177 commit 0c2bcca

File tree

7 files changed

+69
-69
lines changed

7 files changed

+69
-69
lines changed

jsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
//
1717
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
1818
// from the referenced tsconfig.json - TypeScript does not merge them in
19-
}
19+
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"type": "module",
3939
"dependencies": {
4040
"dotenv": "^16.4.5",
41-
"gsap": "^3.12.5",
4241
"rehype-slug": "^6.0.0",
4342
"remark-gfm": "4.0.0",
4443
"remark-rehype": "11.1.1",

pnpm-lock.yaml

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/ZSibar/ZSbarContainer.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
}
157157
div.sibar {
158158
min-width: 5rem;
159-
max-height: calc(100vh - 8rem);
159+
max-height: 60vh;
160160
overflow-y: auto;
161161
padding-top: 1rem;
162162
-webkit-mask-image: linear-gradient(180deg, #0000, #000 1rem calc(100% - 0.5rem), #0000);

src/routes/+layout.server.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ export async function load() {
66
// get dir array, pass to `ZSbarContainer.svelte`
77
const baseDir = 'src/routes';
88
const items = await readdir(baseDir, { recursive: true, withFileTypes: true });
9-
const directory = items
9+
let directory = items
1010
.filter((d) => d.isDirectory())
1111
.map((d) => path.join(d.parentPath, d.name).replaceAll('\\', '/').replaceAll(baseDir, ''));
1212

1313
const mdContent = await readFile('docs/PowerShell.md', 'utf-8');
1414

15+
const docDir = 'docs/';
16+
const docItems = await readdir(docDir, { recursive: true, withFileTypes: true });
17+
const docDirectory = docItems.map((d) => path.join(d.parentPath, d.name).replaceAll('\\', '/').replaceAll(docDir, '/docs/'));
18+
19+
directory = [...directory, ...docDirectory];
20+
1521
return { directory, mdContent };
1622
}

src/routes/+layout.svelte

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import { navigating } from '$app/stores';
1717
import { slide } from 'svelte/transition';
1818
import { afterNavigate } from '$app/navigation';
19+
import { spring, tweened } from 'svelte/motion';
1920
2021
let { data, children } = $props();
2122
@@ -74,27 +75,15 @@
7475
*/
7576
let mobileSibar = $state();
7677
78+
/**
79+
* Bind to window.scrollY
80+
*/
81+
let Y = $state();
82+
7783
// Indicate the svg color.
7884
let fill = 'var(--all-svg-color)';
7985
let stroke = 'var(--all-svg-color)';
8086
81-
let lastScrollY;
82-
let timeoutId = null;
83-
function hideHeader() {
84-
if (timeoutId) {
85-
clearTimeout(timeoutId);
86-
}
87-
timeoutId = setTimeout(() => {
88-
const offset = window.scrollY - lastScrollY;
89-
if (offset > 0) {
90-
hideHead = true;
91-
} else if (offset < 0) {
92-
hideHead = false;
93-
}
94-
lastScrollY = window.scrollY;
95-
}, 100);
96-
}
97-
9887
/**
9988
* on mobile terminal, undisplay the sidebar while click outside
10089
* @param {Event} event - event
@@ -105,23 +94,34 @@
10594
}
10695
}
10796
97+
// Scroll header while scrolling.
98+
let top = $state(0);
99+
let lastY = $state();
100+
function scrollHeader() {
101+
let gap = Y - lastY;
102+
if (top - gap < 0 && top - gap > -96) top = top - gap;
103+
lastY = Y;
104+
}
105+
// Timer for header.
106+
let timer;
107+
108108
onMount(() => {
109-
/**
110-
* hide topbar while scroll down, and display it while scroll up.
111-
*/
112-
lastScrollY = window.scrollY;
113-
addEventListener('scroll', hideHeader, { passive: true });
109+
lastY = Y;
114110
115111
/**
116-
* doing things while resizing
112+
* Doing things while resizing
117113
*/
118114
let timeoutId2 = null;
119-
tocDisplayAttriute = window.getComputedStyle(tocBlock).display === 'none' ? false : true;
115+
if (tocBlock)
116+
tocDisplayAttriute = window.getComputedStyle(tocBlock).display === 'none' ? false : true;
117+
else tocDisplayAttriute = false;
120118
addEventListener('resize', () => {
121119
timeoutId2 && clearTimeout(timeoutId2);
122120
timeoutId2 = setTimeout(() => {
123121
// destroy toclist component while its parent container display none
124-
tocDisplayAttriute = window.getComputedStyle(tocBlock).display === 'none' ? false : true;
122+
if (tocBlock)
123+
tocDisplayAttriute = window.getComputedStyle(tocBlock).display === 'none' ? false : true;
124+
else tocDisplayAttriute = false;
125125
}, 100);
126126
});
127127
});
@@ -157,7 +157,10 @@
157157
<!-- #endregion -->
158158
<!-- #region Content
159159
-->
160-
<div class="topContainer" class:hideHead>
160+
<svelte:window bind:scrollY={Y} onscroll={() => scrollHeader()} />
161+
<!-- svelte-ignore a11y_no_static_element_interactions -->
162+
<div id="headHolder" onmouseenter={() => (top = 1)}></div>
163+
<div class="topContainer" class:hideHead style="top: {top}px;">
161164
<div class="topInnerContainer">
162165
<ZHeader>
163166
{#snippet A()}
@@ -194,23 +197,15 @@
194197
</ZHeader>
195198
</div>
196199
</div>
197-
<main>
200+
<main
201+
onmouseenter={() => {
202+
timer = setTimeout(() => (top = -96), 1000);
203+
}}
204+
onmouseleave={() => clearTimeout(timer)}
205+
>
198206
{#if display}
199207
<div
200208
class="sidebar-container"
201-
onoutroend={() => {
202-
// prevent scrolled event listener while animation displaying.
203-
addEventListener('scroll', hideHeader);
204-
}}
205-
onintroend={() => {
206-
addEventListener('scroll', hideHeader);
207-
}}
208-
onoutrostart={() => {
209-
removeEventListener('scroll', hideHeader);
210-
}}
211-
onintrostart={() => {
212-
removeEventListener('scroll', hideHeader);
213-
}}
214209
transition:slide={{ duration: 300, axis: 'x' }}
215210
bind:clientWidth={SibarWidth}
216211
>
@@ -289,6 +284,13 @@
289284
text-rendering: optimizeSpeed;
290285
}
291286
}
287+
#headHolder {
288+
position: fixed;
289+
top: 3px;
290+
width: 100%;
291+
height: calc(var(--header-block-height) - 4px);
292+
z-index: 15;
293+
}
292294
div.topContainer {
293295
color: var(--header-text-color);
294296
background-color: var(--header-nav-bg-color);
@@ -350,7 +352,7 @@
350352
}
351353
& div.sidebar-container {
352354
display: none;
353-
padding: 1rem 0 0 0;
355+
padding: 0;
354356
}
355357
& div.mobilesidebar-container {
356358
display: block;
@@ -398,7 +400,7 @@
398400
display: block;
399401
& .sibar-innercontainer {
400402
position: fixed;
401-
top: calc(var(--header-block-height) + 3rem);
403+
top: calc(var(--header-block-height) + 2rem);
402404
background-color: transparent;
403405
backdrop-filter: none;
404406
min-width: initial;
@@ -426,7 +428,7 @@
426428
display: block;
427429
& .sibar-innercontainer {
428430
position: fixed;
429-
top: calc(var(--header-block-height) + 3rem);
431+
top: calc(var(--header-block-height) + 2rem);
430432
background-color: transparent;
431433
backdrop-filter: none;
432434
min-width: initial;

src/routes/test.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import { readdir } from 'node:fs/promises';
2-
import { join, normalize } from 'node:path';
3-
import { buildTree } from '../lib/ZSibar/Znav.js';
41

5-
const items = await readdir('src/routes', { recursive: true, withFileTypes: true });
2+
import { readFile, readdir } from 'node:fs/promises';
3+
import * as path from 'node:path';
64

7-
const dir = items
5+
// get dir array, pass to `ZSbarContainer.svelte`
6+
const baseDir = 'src/routes';
7+
const items = await readdir(baseDir, { recursive: true, withFileTypes: true });
8+
let directory = items
89
.filter((d) => d.isDirectory())
9-
.map((d) => {
10-
const abp = join(d.parentPath, d.name).replaceAll('\\', '/');
10+
.map((d) => path.join(d.parentPath, d.name).replaceAll('\\', '/').replaceAll(baseDir, ''));
1111

12-
return abp;
13-
});
12+
const mdContent = await readFile('docs/PowerShell.md', 'utf-8');
1413

15-
dir.forEach((d) => {
16-
console.log(d);
17-
});
14+
const docDir = 'docs/';
15+
const docItems = await readdir(docDir, { recursive: true, withFileTypes: true });
16+
const docDirectory = docItems.map((d) => path.join(d.parentPath, d.name).replaceAll('\\', '/').replaceAll(docDir, '/docs/'));
1817

19-
const tree = buildTree({ _link: '/', _title: 'Home' }, dir);
20-
console.log(tree);
18+
directory = [...directory, ...docDirectory];
19+
20+
console.log({ directory });
21+
debugger

0 commit comments

Comments
 (0)