Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including scrollbooster with all npm packages installed. Try it out:

var scrollbooster = require("scrollbooster")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

scrollbooster v3.0.2

Enjoyable content drag-to-scroll library

ScrollBooster

Enjoyable drag-to-scroll micro library (~2KB gzipped). Supports smooth content scroll via mouse/touch dragging, trackpad or mouse wheel. Zero dependencies.

Easy to setup yet flexible enough to support any custom scrolling logic.

Installation

You can install it via npm or yarn package manager or via script tag:

npm i scrollbooster
yarn add scrollbooster
<script src="https://unpkg.com/scrollbooster@2/dist/scrollbooster.min.js"></script>

Usage

The most simple setup with default settings:

import ScrollBooster from 'scrollbooster';

new ScrollBooster({
    viewport: document.querySelector('.viewport'),
    scrollMode: 'transform'
});

Please note that in order to support IE11 you should replace arrow functions and string templates from code examples to supported equivalents or just use Babel.

Options

OptionTypeDefaultDescription
viewportDOM NodenullContent viewport element (required)
contentDOM Nodeviewport child elementScrollable content element inside viewport
scrollModeStringundefinedScroll technique - via CSS transform or natively. Could be 'transform' or 'native'
directionString'all'Scroll direction. Could be 'horizontal', 'vertical' or 'all'
bounceBooleantrueEnables elastic bounce effect when hitting viewport borders
textSelectionBooleanfalseEnables text selection inside viewport
inputsFocusBooleantrueEnables focus for elements: 'input', 'textarea', 'button', 'select' and 'label'
pointerModeString'all'Specify pointer type. Supported values - 'touch' (scroll only on touch devices), 'mouse' (scroll only on desktop), 'all' (mobile and desktop)
frictionNumber0.05Scroll friction factor - how fast scrolling stops after pointer release
bounceForceNumber0.1Elastic bounce effect factor
emulateScrollBooleanfalseEnables mouse wheel/trackpad emulation inside viewport
preventDefaultOnEmulateScrollStringfalsePrevents horizontal or vertical default when emulateScroll is enabled (eg. useful to prevent horizontal trackpad gestures while enabling vertical scrolling). Could be 'horizontal' or 'vertical'.
lockScrollOnDragDirectionStringfalseDetect drag direction and either prevent default mousedown/touchstart event or lock content scroll. Could be 'horizontal', 'vertical' or 'all'
dragDirectionToleranceNumber40Tolerance for horizontal or vertical drag detection
onUpdateFunctionnoopHandler function to perform actual scrolling. Receives scrolling state object with coordinates
onClickFunctionnoopClick handler function. Here you can, for example, prevent default event for click on links. Receives object with scrolling metrics and event object. Calls after each click in scrollable area
onPointerDownFunctionnoopmousedown/touchstart events handler
onPointerUpFunctionnoopmouseup/touchend events handler
onPointerMoveFunctionnoopmousemove/touchmove events handler
onWheelFunctionnoopwheel event handler
shouldScrollFunctionnoopFunction to permit or disable scrolling. Receives object with scrolling state and event object. Calls on pointerdown (mousedown, touchstart) in scrollable area. You can return true or false to enable or disable scrolling

List of methods

MethodDescription
setPositionSets new scroll position in viewport. Receives an object with properties x and y
scrollToSmooth scroll to position in viewport. Receives an object with properties x and y
updateMetricsForces to recalculate elements metrics. Useful for cases when content in scrollable area change its size dynamically
updateOptionsSets option value. All properties from Options config object are supported
getStateReturns current scroll state in a same format as onUpdate
destroyRemoves all instance's event listeners

Full Example

const viewport = document.querySelector('.viewport');
const content = document.querySelector('.scrollable-content');

const sb = new ScrollBooster({
  viewport,
  content,
  bounce: true,
  textSelection: false,
  emulateScroll: true,
  onUpdate: (state) => {
    // state contains useful metrics: position, dragOffset, dragAngle, isDragging, isMoving, borderCollision
    // you can control scroll rendering manually without 'scrollMethod' option:
    content.style.transform = `translate(
      ${-state.position.x}px,
      ${-state.position.y}px
    )`;
  },
  shouldScroll: (state, event) => {
    // disable scroll if clicked on button
    const isButton = event.target.nodeName.toLowerCase() === 'button';
    return !isButton;
  },
  onClick: (state, event, isTouchDevice) => {
    // prevent default link event
    const isLink = event.target.nodeName.toLowerCase() === 'link';
    if (isLink) {
      event.preventDefault();
    }
  }
});

// methods usage examples:
sb.updateMetrics();
sb.scrollTo({ x: 100, y: 100 });
sb.updateOptions({ emulateScroll: false });
sb.destroy();

Live ScrollBooster Examples On CodeSandbox

Browser support

ScrollBooster has been tested in IE 11, Edge and other modern browsers (Chrome, Firefox, Safari).

Special thanks

David DeSandro for his talk "Practical UI Physics".

License

MIT License (c) Ilya Shubin

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free