From 047ec5c63031d3285245db99c752b6a525670f5c Mon Sep 17 00:00:00 2001 From: Sped0n Date: Sun, 29 Oct 2023 22:12:06 +0800 Subject: [PATCH] feat(container.scss): add container styles for fixed position and scrolling behavior feat(container.ts): create container module to handle scrollable behavior and add/remove disableScroll class fix(customCursor.ts): update append target for cursor element to use container instead of main fix(stage.ts): update append target for stage element to use container instead of main fix(stageNav.ts): update append target for navOverlay element to use container instead of main feat(main.ts): initialize container module and conditionally initialize stage and stageNav modules based on device type fix(nav.ts): update references to state module functions to use state.get() instead of getState() --- assets/scss/_partial/_container.scss | 19 +++++++++++++++++++ assets/ts/container.ts | 14 ++++++++++++++ assets/ts/desktop/customCursor.ts | 3 ++- assets/ts/desktop/stage.ts | 15 ++++++++------- assets/ts/desktop/stageNav.ts | 9 +++++---- assets/ts/main.ts | 22 +++++++++++++++++----- assets/ts/nav.ts | 8 ++++---- layouts/index.html | 2 +- 8 files changed, 70 insertions(+), 22 deletions(-) create mode 100644 assets/scss/_partial/_container.scss create mode 100644 assets/ts/container.ts diff --git a/assets/scss/_partial/_container.scss b/assets/scss/_partial/_container.scss new file mode 100644 index 0000000..330d239 --- /dev/null +++ b/assets/scss/_partial/_container.scss @@ -0,0 +1,19 @@ +.container { + position: fixed; + top: 0; + z-index: 0; + + width: 100vw; + height: var(--window-height); + + overflow-y: scroll; + overflow-x: hidden; + background: white; + + overscroll-behavior: none; + -webkit-overflow-scrolling: none; +} + +.disableScroll { + pointer-events: none; +} diff --git a/assets/ts/container.ts b/assets/ts/container.ts new file mode 100644 index 0000000..8dd3c99 --- /dev/null +++ b/assets/ts/container.ts @@ -0,0 +1,14 @@ +import { scrollable } from './mobile/scroll' + +export let container: HTMLDivElement + +export function initContainer(): void { + container = document.getElementsByClassName('container').item(0) as HTMLDivElement + scrollable.addWatcher(() => { + if (scrollable.get()) { + container.classList.remove('disableScroll') + } else { + container.classList.add('disableScroll') + } + }) +} diff --git a/assets/ts/desktop/customCursor.ts b/assets/ts/desktop/customCursor.ts index ab67c99..8d91739 100644 --- a/assets/ts/desktop/customCursor.ts +++ b/assets/ts/desktop/customCursor.ts @@ -1,4 +1,5 @@ import { active } from './stage' +import { container } from '../container' /** * variables @@ -32,7 +33,7 @@ export function initCustomCursor(): void { // append cursor inner to cursor cursor.append(cursorInner) // append cursor to main - document.getElementById('main')!.append(cursor) + container.append(cursor) // bind mousemove event to window window.addEventListener('mousemove', onMouse) // add active callback diff --git a/assets/ts/desktop/stage.ts b/assets/ts/desktop/stage.ts index 1598251..0400671 100644 --- a/assets/ts/desktop/stage.ts +++ b/assets/ts/desktop/stage.ts @@ -1,7 +1,8 @@ -import { incIndex, getState } from '../state' +import { incIndex, state } from '../state' import { gsap, Power3 } from 'gsap' import { ImageJSON } from '../resources' import { Watchable } from '../utils' +import { container } from '../container' /** * types @@ -29,7 +30,7 @@ function getElTrail(): HTMLImageElement[] { } function getElTrailCurrent(): HTMLImageElement[] { - return getElTrail().slice(-getState().trailLength) + return getElTrail().slice(-state.get().trailLength) } function getElTrailInactive(): HTMLImageElement[] { @@ -52,12 +53,12 @@ function onMouse(e: MouseEvent): void { const cord = { x: e.clientX, y: e.clientY } const travelDist = Math.hypot(cord.x - last.x, cord.y - last.y) - if (travelDist > getState().threshold) { + if (travelDist > state.get().threshold) { last = cord incIndex() - const newHist = { i: getState().index, ...cord } - cordHist.set([...cordHist.get(), newHist].slice(-getState().length)) + const newHist = { i: state.get().index, ...cord } + cordHist.set([...cordHist.get(), newHist].slice(-state.get().length)) } } @@ -70,7 +71,7 @@ function setPositions(): void { x: (i: number) => cordHist.get()[i].x - window.innerWidth / 2, y: (i: number) => cordHist.get()[i].y - window.innerHeight / 2, opacity: (i: number) => - i + 1 + getState().trailLength <= cordHist.get().length ? 0 : 1, + i + 1 + state.get().trailLength <= cordHist.get().length ? 0 : 1, zIndex: (i: number) => i, scale: 0.6 }) @@ -192,5 +193,5 @@ function createStage(ijs: ImageJSON[]): void { e.alt = 'image' stage.append(e) } - document.getElementById('main')!.append(stage) + container.append(stage) } diff --git a/assets/ts/desktop/stageNav.ts b/assets/ts/desktop/stageNav.ts index 7b60c6a..61e42b7 100644 --- a/assets/ts/desktop/stageNav.ts +++ b/assets/ts/desktop/stageNav.ts @@ -1,7 +1,8 @@ import { setCustomCursor } from './customCursor' -import { decIndex, incIndex, getState } from '../state' +import { decIndex, incIndex, state } from '../state' import { increment, decrement } from '../utils' import { cordHist, isOpen, isAnimating, active, minimizeImage } from './stage' +import { container } from '../container' /** * types @@ -71,7 +72,7 @@ export function initStageNav() { navOverlay.classList.remove('active') } }) - document.getElementById('main')!.append(navOverlay) + container.append(navOverlay) window.addEventListener('keydown', handleKey) } @@ -83,7 +84,7 @@ function nextImage() { if (isAnimating.get()) return cordHist.set( cordHist.get().map((item) => { - return { ...item, i: increment(item.i, getState().length) } + return { ...item, i: increment(item.i, state.get().length) } }) ) @@ -94,7 +95,7 @@ function prevImage() { if (isAnimating.get()) return cordHist.set( cordHist.get().map((item) => { - return { ...item, i: decrement(item.i, getState().length) } + return { ...item, i: decrement(item.i, state.get().length) } }) ) diff --git a/assets/ts/main.ts b/assets/ts/main.ts index 9d209bf..5e82b59 100644 --- a/assets/ts/main.ts +++ b/assets/ts/main.ts @@ -1,13 +1,25 @@ -import { initResources } from './resources' -import { initState } from './state' +import { initContainer } from './container' import { initCustomCursor } from './desktop/customCursor' -import { initNav } from './nav' import { initStage } from './desktop/stage' import { initStageNav } from './desktop/stageNav' +import { initCollection } from './mobile/collection' +import { initGallery } from './mobile/gallery' +import { initNav } from './nav' +import { initResources } from './resources' +import { initState } from './state' +import { isMobile } from './utils' +initContainer() initCustomCursor() const ijs = initResources() initState(ijs.length) -initStage(ijs) -initStageNav() + initNav() + +if (!isMobile()) { + initStage(ijs) + initStageNav() +} else { + initCollection(ijs) + initGallery(ijs) +} diff --git a/assets/ts/nav.ts b/assets/ts/nav.ts index 833aa7e..25282d4 100644 --- a/assets/ts/nav.ts +++ b/assets/ts/nav.ts @@ -1,4 +1,4 @@ -import { getState, incThreshold, decThreshold } from './state' +import { decThreshold, incThreshold, state } from './state' import { expand } from './utils' /** @@ -48,15 +48,15 @@ export function initNav() { // helper export function updateThresholdText(): void { - const thresholdValue: string = expand(getState().threshold) + const thresholdValue: string = expand(state.get().threshold) thresholdDispNums.forEach((e: HTMLSpanElement, i: number) => { e.innerText = thresholdValue[i] }) } export function updateIndexText(): void { - const indexValue: string = expand(getState().index + 1) - const indexLength: string = expand(getState().length) + const indexValue: string = expand(state.get().index + 1) + const indexLength: string = expand(state.get().length) indexDispNums.forEach((e: HTMLSpanElement, i: number) => { if (i < 4) { e.innerText = indexValue[i] diff --git a/layouts/index.html b/layouts/index.html index 9fe8cfc..5e9a693 100644 --- a/layouts/index.html +++ b/layouts/index.html @@ -37,6 +37,6 @@ {{ end }} - {{ partial "nav.html" . }} +
{{ partial "nav.html" . }}