@mystroken/drag

0.2.0 • Public • Published

🏄 drag

contributions welcome Tweet

A lightweight JavaScript "hold and drag" utility. Vanilla JS - No dependencies.
Its size is ~619 B (minified and compressed).

See the online demo (Advanced example)

Usage

npm install @mystroken/drag and start using the hold and drag system.

import Drag from '@mystroken/drag';

// Elements to listen/move.
const slidable = document.querySelector('#slidable');
const container = document.querySelector('#container');

// Where to store the drag position.
let currentX = 0;
let currentY = 0;

// Initialize the library.
const options = {
  listener: container,
  multiplier: 2
};
const drag = new Drag(options);

// Store the cursor position on move.
drag.on(event => {
  currentX = event.X;
  currentY = event.Y;
});

// Use the cursor position to slide the slidable element.
const move = () => {
  slidable.style.transform = `translate3d(${currentX}px, ${currentY}px, 0px)`;
  requestAnimationFrame(move);
};
requestAnimationFrame(move);

Package Sidebar

Install

npm i @mystroken/drag

Weekly Downloads

4

Version

0.2.0

License

MIT

Unpacked Size

7.65 kB

Total Files

4

Last publish

Collaborators

  • mystroken