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

drag-to-scroll

v1.2.0

Published

Class to use on front end to enable drag with the mouse to scroll a page or a scrollable element.

Downloads

5

Readme

DragToScroll

DragToScroll is a TypeScript package that provides a draggable element with scroll functionality. It extends the functionality of the DragToBlank class and adds additional features such as direction control, slide animation, and event propagation control.

Installation

npm install drag-to-scroll

Usage

Instantiation without configurations

You can create a DragToScroll instance without any configurations. The default configurations will be used.

import { DragToScroll } from 'drag-to-scroll';

const element = document.getElementById('my-element');

const draggable = new DragToScroll(element);

Instantiation with configurations

You can also provide a configuration object when creating a DragToScroll instance.

import { DragToScroll } from 'drag-to-scroll';

const element = document.getElementById('my-element');
const config = {
	direction: { x: true, y: false },
	animation: {
		timing: {
			duration: 500,
			easingFactor: 4,
			maxSpeed: 10,
		},
		slide: true,
	},
	preventDefault: true,
	stopPropagation: true,
};

const draggable = new DragToScroll(element, config);

Using the apply method

The apply method allows you to apply the DragToScroll functionality to all elements with a specific class. By default, it applies to elements with the class 'drag-to-scroll', but you can provide a different class as an argument.

import { DragToScroll } from 'drag-to-scroll';

DragToScroll.apply(); // Applies to all elements with the class 'drag-to-scroll'

DragToScroll.apply('my-custom-class'); // Applies to all elements with the class 'my-custom-class'

The destroy instance method

This method removes the event listeners from the instance and removes the instance from the static instances array.

const element = document.getElementById('my-element');
const draggable = new DragToScroll(element);
// ...
draggable.destroy();

The destroy static method

This method finds an instance with a specific DOM element from the static instances array and destroys it using its instance method.

const element = document.getElementById('my-element');
const draggable = new DragToScroll(element);
// ...
DragToScroll.destroy(element);

The destroyAll static method

This method loops over the static instances array and destroys all instances.

DragToScroll.destroyAll();

Configuration

The DragToScroll constructor takes a configuration object with the following optional properties:

| Property | Type | Description | Default value | | ----------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------- | ---------------------- | | direction | Object | An object with x and y properties, indicating whether the element should be draggable in the x and y directions respectively. | { x: true, y: true } | | animation | Object | An object controlling the slide animation. See Animation Configuration for more details. | | | preventDefault | Boolean | A boolean indicating whether the default action should be prevented when the user starts dragging. | true | | stopPropagation | Boolean | A boolean indicating whether the event should stop being propagated when the user starts dragging. | true |

Animation Configuration

The animation property in the configuration object controls the slide animation. It has the following properties:

| Property | Type | Description | Default value | | -------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------- | | timing | Object | An object with duration:number (in milliseconds), easingFactor:number (>=1, 1 is linear), and maxSpeed:number (px/0.0001s) properties. | { duration: 1500, easingFactor: 4, maxSpeed: 10 } | | slide | Boolean | A boolean indicating whether the element should continue to slide after the user stops dragging. | true |

Properties

instances

This is a static property that holds an array of all active DragToScroll instances.

static instances: DragToScroll[] = [];

You can use this property to access all active instances of the DragToScroll class. For example, you can use it to find an instance:

const element = document.getElementById('my-element');
new DragToScroll(element);
// ...
const instance = DragToScroll.instances.find(
	(i) => i.element === element,
);

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License MIT