npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

element-scroller

v1.0.1

Published

Improved and extended interface for smooth DOM element scrolling.

Downloads

8

Readme

element-scroller

Improved and extended interface for smooth DOM element scrolling.

Native scrolling APIs are horrendous when using behavior: 'smooth' because when scrollBy is called while previous one is still animating, it starts animating from the current position, not from the target of the ongoing animation. This effectively eats up all but the last call when this method is called shortly one after another, resulting in unresponsive, and downright broken scrolling.

Features:

  • Tiny, ~2KB min, ~1KB gz.
  • Configurable scrolling friction coefficient.
  • Can take over native mouse wheel (to match scrolling friction).
  • Ability to flip wheel direction: vertical scrolling will scroll element horizontally.
  • Scroll gliding, useful for implementing "scroll while cursor hovers edge".
  • Scroll to/by and gliding can all be called while others are still active, everything works together seamlessly.

Install

npm install element-scroller

Usage

import {makeScroller} from 'element-scroller';

const container = document.getElementById('container');
const scroller = makeScroller(container);

scroller.scrollBy({left: 100, top: 200, friction: 0.2});
scroller.scrollTo({top: 500});
scroller.glide({top: -100}); // up
scroller.stop(); // stop gliding or any other ongoing scrolling
scroller.dispose(); // unbinds everything and makes all methods impotent

API

makeScroller

function makeScroller(element: HTMLElement, options: Options): ElementScroller;

element

Any DOM HTML Element.

options

interface Options {
	/**
	 * Coefficient of friction between 0 and 1, where 0 means infinite, and 1
	 * means instant animations. Default: 0.2
	 */
	friction: number;
	/**
	 * Wether to take over wheel scrolling.
	 */
	handleWheel: boolean;
	/**
	 * Wether vertical scrolls should scroll the element horizontally, and vice
	 * versa.
	 */
	flipWheel: boolean;
	/**
	 * Coefficient of friction to use when wheel scrolling. Default: 0.25
	 */
	wheelFriction: number;
}

Return

See Scroller below.

Scroller

interface Scroller {
	readonly element: HTMLElement;
	flipWheel: boolean;
	friction: number;
	wheelFriction: number;
	scrollTo(options: ScrollOptions): void;
	scrollBy(options: ScrollOptions): void;
	glide(options: Partial<Position>): void;
	stop(): void;
	dispose(): void;
	readonly disposed: boolean;
}

interface Position {
	left: number;
	top: number;
}

type ScrollOptions = Partial<Position> & {friction?: number};

element

Type: HTMLElement readonly

The DOM element this scroller is working with.

flipWheel

Type: boolean

Controls wether wheel scrolling (if handleWHeel was requested) should be flipped.

friction

Type: number

Default friction to use for scrollBy/scrollTo when one is not passed.

wheelFriction

Type: number

Friction to use when wheel scrolling.

scrollTo

Type: scrollTo(options: ScrollOptions): void

Scroll element to a specific coordinates.

options
interface ScrollOptions {
	left?: number;
	top?: number;
	friction?: number;
}

You can use Infinity for left and top to scroll to the end.

scrollBy

Type: scrollBy(options: ScrollOptions): void

Scroll element by an amount of pixels.

options
interface ScrollOptions {
	left?: number;
	top?: number;
	friction?: number;
}

glide

Type: glide(options: GlideOptions): void

Scroll element continuously in linear motion by configured number of pixels per second until stop() is called.

options
interface GlideOptions {
	left?: number;
	top?: number;
}

Use negative numbers to scroll back.

stop

Type: stop(): void

Stops gliding or any other ongoing scrolling.

dispose

Type: dispose(): void

Stops any ongoing animation, unbinds everything, and makes all methods impotent.

disposed

Type: boolean readonly

Wether this scroller was already disposed.