68 lines
1.2 KiB
TypeScript
68 lines
1.2 KiB
TypeScript
![]() |
import { OnAnimationFrame } from "../../../animation/OnAnimationFrame";
|
||
|
import { OnChange, OnHashChange, OnVisibilityChange } from "../../EventListeners";
|
||
|
|
||
|
export class HashScroll
|
||
|
{
|
||
|
static _hasScrolled = false;
|
||
|
static _hasListener = false;
|
||
|
|
||
|
static applyOnDocument( onAnimationFrame:OnAnimationFrame )
|
||
|
{
|
||
|
HashScroll._hasScrolled = false;
|
||
|
HashScroll.scroll();
|
||
|
|
||
|
|
||
|
if ( this._hasListener )
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
OnVisibilityChange.add( document, HashScroll._visibilityHandler );
|
||
|
|
||
|
HashScroll._hasListener = true;
|
||
|
|
||
|
OnHashChange.add( window, ()=> HashScroll.scroll() );
|
||
|
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
static scroll()
|
||
|
{
|
||
|
console.log( "Scrolling:", document.hidden );
|
||
|
let hash = location.hash;
|
||
|
|
||
|
if ( hash )
|
||
|
{
|
||
|
let el = document.querySelector( hash );
|
||
|
|
||
|
if ( el )
|
||
|
{
|
||
|
el.scrollIntoView( { behavior: "smooth", block: "start" } );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
static _doScrollOnce()
|
||
|
{
|
||
|
if ( this._hasScrolled )
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
HashScroll.scroll();
|
||
|
|
||
|
HashScroll._hasScrolled = true;
|
||
|
document.removeEventListener( "visibilitychange", HashScroll._visibilityHandler );
|
||
|
}
|
||
|
|
||
|
static _visibilityHandler = () =>
|
||
|
{
|
||
|
if ( ! document.hidden )
|
||
|
{
|
||
|
HashScroll._doScrollOnce();
|
||
|
}
|
||
|
}
|
||
|
}
|