How mukiturrahman/locomotive-scroll works
1. What this repo is
- This is a browser-side JavaScript library, not an application backend.
- Its core product is a scroll controller that combines viewport detection, smooth scrolling, parallax, sticky behavior, and programmatic scrolling.
2. Public package boundary
- The main entry points are in src/locomotive-scroll.js and the built artifacts in dist/.
- Consumers import the library as ES module or UMD, and optionally include the generated stylesheet dist/locomotive-scroll.css.
3. Runtime architecture
- The source is split into a shared core plus mode-specific implementations.
- Core.js appears to hold common lifecycle and element-tracking behavior.
- Main.js and NativeMain.js likely represent the top-level orchestration for smooth vs native scrolling modes.
- Smooth.js and Native.js split the actual scroll mechanics by mode.
4. Scroll modes and control flow
- In native mode, the library observes normal browser scrolling and focuses on element detection and events.
- In smooth mode, it intercepts scroll input, applies inertia/lerp, and manages a virtual scroll position.
- The mode choice is driven by instance options such as smooth, direction, lerp, gestureDirection, and device-specific overrides.
5. Element detection and in-view state
- The library scans DOM elements marked with data-scroll and related attributes.
- It computes viewport intersection, progress, offsets, and repeat behavior.
- Current in-view elements are exposed through the scroll event payload, and data-scroll-call triggers custom callbacks/events.
6. Parallax, sticky, and motion features
- Smooth mode adds animation-like behavior through attributes such as data-scroll-speed, data-scroll-delay, data-scroll-direction, data-scroll-sticky, and data-scroll-target.
- These features depend on transform calculations rather than layout reflow, which is why utilities like transform.js matter to the architecture.
7. Shared utilities and configuration
- options.js centralizes default option handling and likely normalizes user configuration.
- utils/html.js, utils/maths.js, and utils/transform.js provide DOM, math, and transform helpers used across the scroll implementations.
8. Event model
- The library exposes instance events such as scroll and call.
- That event surface is the main integration boundary for app code: animation libraries, UI state updates, analytics, or custom routing logic subscribe there rather than coupling directly to internals.
9. Styling subsystem
- The scroll behavior depends on stylesheet support in src/locomotive-scroll.scss and src/styles/_base.scss, plus scrollbar styles in _scrollbar.scss.
- These styles define the structural classes and container behavior required for the scrolling container and custom scrollbar in smooth mode.
10. Build and packaging pipeline
- The repo uses a Gulp-based build system in gulpfile.babel.js and the build/ scripts to compile, bundle, copy, serve, watch, and notify.
- Tooling includes Babel and Sass, with browser targeting defined by .browserslistrc and style formatting governed by .prettierrc.
- The release outputs are placed in dist/ as distributable JS/CSS bundles.
11. Docs/demo site as a separate front-end
- docs/ is a standalone demo/documentation site with its own source tree under docs/src/ and compiled assets in docs/dist/.
- It has separate scripts, styles, images, and HTML entry pages like docs/index.html and docs/horizontal.html, so the demo is architecturally distinct from the library itself.
12. External dependencies that shape architecture
- The scroll engine relies on virtual-scroll for momentum-like input handling, bezier-easing for animated scrollTo, and a viewport-detection approach derived from modularScroll.
- These dependencies reinforce that the library is entirely client-side and focused on DOM input/output, not data fetching or server interaction.
Main entry points: Core.js Core · Smooth.js Smooth · locomotive-scroll.js Public API · locomotive-scroll.scss SCSS Entry
Components 19
Library
Core browser-side scroll controller and public package entry points
- Public API · ESM/UMD entry points and distributable assets for consumers
src/locomotive-scroll.js - Core · Common lifecycle, element tracking, and scroll state behavior
src/scripts/Core.js - Main · Top-level coordination for smooth scrolling mode
src/scripts/Main.js - Native Main · Top-level coordination for native scrolling mode
src/scripts/NativeMain.js - Smooth · Virtual scrolling, inertia, parallax, sticky, and transformed motion
src/scripts/Smooth.js - Native · Browser scroll observation and in-view detection in native mode
src/scripts/Native.js - Options · Default options and device-specific configuration handling
src/scripts/options.js - HTML Utils · DOM and element helper functions shared by scroll modes
src/scripts/utils/html.js - Math Utils · Numeric helpers for offsets, progress, and interpolation
src/scripts/utils/maths.js - Transform Utils · Transform calculations used for parallax and sticky motion
src/scripts/utils/transform.js
Styles
SCSS source and generated CSS for scroll container and scrollbar
- Public CSS · Generated stylesheet consumed alongside the JavaScript bundle
dist/locomotive-scroll.css - Base Styles · Core structural styles for the scrolling container
src/styles/_base.scss - Scrollbar Styles · Custom scrollbar styles used by smooth scrolling mode
src/styles/_scrollbar.scss - SCSS Entry · Primary stylesheet entry that assembles the library styles
src/locomotive-scroll.scss
Tooling
Build, bundling, and release pipeline
- Build System · Compilation, bundling, copying, serving, watching, and notifications
gulpfile.babel.js - Build Scripts · Individual build tasks for scripts, styles, assets, and dev workflow
build/scripts.js
Docs Demo
Standalone documentation and demo site
- Docs Site · Standalone documentation and demo application
docs/index.html - Docs JS · Demo-site behavior and examples rendered in the docs frontend
docs/src/scripts/main.js - Docs Styles · Docs site styling and layout system
docs/src/styles/main.scss
Connections 22
- Public API → Core: exports · Public bundle exposes the shared scroll runtime
- Public API → Main: smooth mode · Entry selects the smooth-mode orchestration path
- Public API → Native Main: native mode · Entry selects the native-mode orchestration path
- Main → Smooth: uses · Smooth orchestration delegates to the smooth engine
- Native Main → Native: uses · Native orchestration delegates to the native engine
- Core → Options: config · Core behavior is driven by normalized options
- Core → HTML Utils: dom · Common runtime relies on DOM helpers
- Core → Math Utils: math · Shared state computations use numeric helpers
- Smooth → Transform Utils: motion · Smooth mode uses transform calculations for parallax and sticky effects
- Smooth → Math Utils: compute · Smooth mode computes offsets, interpolation, and progress
- Smooth → HTML Utils: scan · Smooth mode scans and manages scroll elements in the DOM
- Native → HTML Utils: observe · Native mode tracks elements and viewport state in the DOM
- Native → Math Utils: compute · Native mode computes visibility, offsets, and progress
- SCSS Entry → Base Styles: includes · Library stylesheet assembles base structural rules
- SCSS Entry → Scrollbar Styles: includes · Library stylesheet assembles smooth-mode scrollbar rules
- SCSS Entry → Public CSS: builds · SCSS source is compiled into the distributable CSS bundle
- Build System → Build Scripts: orchestrates · Gulp task runner coordinates build steps
- Build Scripts → Public API: bundles · Build pipeline produces distributable JavaScript bundles
- Build Scripts → SCSS Entry: compiles · Build pipeline compiles source SCSS into CSS output
- Build System → Docs Site: serves · Build tooling supports the docs/demo site workflow
- Docs Site → Docs JS: runs · Demo site behavior is implemented in its own script entry
- Docs Site → Docs Styles: styles · Demo site uses its own stylesheet tree