Components/Scroll

Scroll

A scroll container driven by BetterScroll or native scrolling.

VerifiedSince 0.3.4

Usage

Basic

Loading demo...

Horizontal

direction picks the scroll axes: vertical, horizontal, or both.

Loading demo...

Bounce and Persistent Scrollbar

With scrollbarAlwaysVisible, the scrollbar stays visible even when the content fits.

Loading demo...

Scroll Chaining

Scrolling doesn't pass to the outer container by default; scrollChaining hands it over once the inner one hits an edge.

Loading demo...

Native Scrolling

native skips BetterScroll and keeps the same container structure.

Loading demo...

Pull to Refresh and Load More

Listen for pulling-down / pulling-up, and call finishPullDown() / finishPullUp() when the async work ends, whether it succeeds or fails.

Loading demo...

Best Practices

  • Use native mode for plain article or document scrolling; reach for BetterScroll only when you need consistent scrollbars, wheel bridging, bounce, or pull plugins.
  • Keep scrollChaining=false in nested panels; enable it only when the parent-child handoff is intentional and tested.
  • Set noPadding when the child owns its spacing, such as virtualized lists, tables, and full-bleed media.
  • Give horizontal or both-axis content a definite width, or BetterScroll can't reliably detect horizontal overflow.
  • Don't pass large mutable objects through options; configure documented behavior through its props.

API Reference

Props

PropTypeDefaultDescription
nativebooleanfalseForces native scrolling and skips BetterScroll initialization.
unifiedbooleanfalseForces BetterScroll over Safari / Chromium auto-native; native still wins.
nativeAutoFallbackbooleantrueSwitches to native scrolling on macOS + Chromium 145+; doesn't affect Safari.
noPaddingbooleanfalseRemoves content padding; horizontal and both-axis content becomes max-content wide.
scrollChainingbooleanfalseHands scrolling to the outer container at the edges.
direction'vertical' | 'horizontal' | 'both''vertical'Scroll axes.
scrollbarbooleantrueEnables the BetterScroll scrollbar plugin; native mode uses browser scrollbars.
scrollbarFadebooleantrueFades the scrollbar when idle.
scrollbarInteractivebooleantrueLets users drag the scrollbar.
scrollbarAlwaysVisiblebooleanfalseKeeps the scrollbar visible, for bounce or content that fits.
scrollbarMinSizenumber18Minimum thumb size, written to --tx-scrollbar-min-size.
probeType0 | 1 | 2 | 33BetterScroll probeType; sets how often scroll fires.
bouncebooleantrueEdge bounce and wheel overshoot.
clickbooleantruePasses through BetterScroll's click option.
wheelbooleantrueWheel bridge in BetterScroll mode; ignores ctrl wheel gestures.
refreshOnContentChangebooleantrueCalls refresh() after content changes.
pullDownRefreshboolean | Record<string, unknown>falseEnables pull to refresh; an object becomes the BetterScroll plugin options.
pullDownThresholdnumber70Pull distance that fires pulling-down.
pullDownStopnumber56Hold position while refreshing; BetterScroll mode only.
pullUpLoadboolean | Record<string, unknown>falseEnables load more; an object becomes the BetterScroll plugin options.
pullUpThresholdnumber0Distance from the bottom that fires pulling-up.
optionsRecord<string, unknown>{}Extra BetterScroll options; the component consumes wheelOvershoot itself.

Events

EventParamsDescription
scroll{ scrollTop: number; scrollLeft: number }Fires on scroll with absolute offsets.
pulling-down-Fires on pull to refresh; not again until finishPullDown().
pulling-up-Fires on load more; not again until finishPullUp().

Slots

SlotDescription
defaultMain content, rendered inside .tx-scroll__content.
headerBefore the main content: ahead of .tx-scroll__content in native mode, inside it in BetterScroll mode.
footerAfter the main content; often a loading status.

Exposed Methods

NameTypeDescription
nativeScrollRefRef<HTMLElement | null>The scroll element in native mode.
scrollTo(x, y, time?)(x: number, y: number, time?: number) => voidScrolls to absolute offsets; time applies in BetterScroll mode only.
getScrollInfo()() => TxScrollInfoCurrent offsets, scroll size, and client size.
refresh()() => voidRecomputes BetterScroll's scrollable range; no-op in native mode.
finishPullDown()() => voidEnds the current pull-down so it can fire again.
finishPullUp()() => voidEnds the current pull-up so it can fire again.

Overview

  • The mode is chosen in order: native → unified (BetterScroll) → native on macOS Safari → native when nativeAutoFallback is on and macOS + Chromium ≥ 145 → otherwise BetterScroll.
  • Native mode maps direction to overflow-x/y and scrollChaining=false to overscroll-behavior: contain; BetterScroll mode maps it to scrollX, scrollY, and freeScroll.
  • Size and content changes are coalesced into one frame before refresh(); refreshOnContentChange=false turns off only the content-change refreshes, not the resize ones.
  • Native pull to refresh is a touch-threshold fallback at scrollTop=0.
  • On macOS, with both wheel and bounce on, useTransition: false is injected by default; set useTransition in options to override.

Technologies

  • BetterScroll mode lazy-loads @better-scroll/core and @better-scroll/scroll-bar; the wheel goes through the component's own bridge.
  • Source: packages/tuffex/packages/components/src/scroll/.
查看源码
packages/tuffex/packages/components/src/scroll/index.ts