From 8804bf447025075f766019e58b008dd7aa5446f1 Mon Sep 17 00:00:00 2001 From: Dmytro Soldatov Date: Tue, 30 Jul 2024 15:10:20 +0300 Subject: [PATCH] Feat: Add scroll event listener to ScrollBox (#187) --- src/ScrollBox.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/ScrollBox.ts b/src/ScrollBox.ts index 3160712..27bf0d4 100644 --- a/src/ScrollBox.ts +++ b/src/ScrollBox.ts @@ -93,6 +93,7 @@ export class ScrollBox extends Container protected lastScrollY!: number | null; protected proximityCheckFrameCounter = 0; public onProximityChange = new Signal<(data: ProximityEventData) => void>(); + public onScroll: Signal<(value: number) => void> = new Signal(); /** * @param options @@ -567,6 +568,9 @@ export class ScrollBox extends Container protected onMouseScroll(event: WheelEvent): void { + const isVertical: boolean = this.options.type !== 'horizontal'; + const oldScrollPos = isVertical ? this.scrollY : this.scrollX; + if (!this.isOver && !this.options.globalScroll) return; this.renderAllItems(); @@ -610,6 +614,13 @@ export class ScrollBox extends Container } this.stopRenderHiddenItems(); + + + const newScrollPos = isVertical ? this.scrollY : this.scrollX; + + if (newScrollPos !== oldScrollPos) { + this.onScroll?.emit(newScrollPos); + } } /** Makes it scroll down to the last element. */ @@ -872,4 +883,12 @@ export class ScrollBox extends Container item.children.forEach((child) => this.revertClick(child)); } } + + get scrollHeight(): number { + return this.list.height; + } + + get scrollWidth(): number { + return this.list.width; + } }