GithubHelp home page GithubHelp logo

easy-drag's Introduction

easy-drag

中文文档

Easy to realize drag effect

Principle

Listen to mousedown, mousemove, and mouseup events, and set the element's transform property to drag and drop according to the mouse position

Feature

  1. Use JavaScript implementation, support any framework
  2. Use transform property implementation that supports elements in any positioning mode
  3. Lightweight, only 2KB
  4. Performance, hardware acceleration for a silky drag experience

Install

# Yarn
yarn add easy-drag

# NPM
npm install --save easy-drag

Usage

import enableDrag from "easy-drag";
const disableDrag = enableDrag(document.querySelector(".draggable"));
if ("what to disable drag") {
  disableDrag();
}

Or

import enableDrag from "easy-drag";
const disableDrag = enableDrag(document.querySelector(".draggable"), {
  outerElement: document.body,
  innerElement: document.querySelector(".drag-icon"),
  onDragStart: () => {},
  onDrag: () => {},
  onDragEnd: () => {},
});
if ("what to disable drag") {
  disableDrag();
}

Description:

  • outerElement: drag range element, default document.body
  • innerElement: drag icon element, application scenario: Pop-ups need to be draggable, but only the title area can be dragged
  • onDragStart: callback on drag start, parameter v is element drag translate vector (relative to initial position)
  • onDrag: callback in dragging, parameter v is element drag translate vector (relative to initial position)
  • onDragEnd: callback on drag end, parameter v is element drag translate vector (relative to initial position)

Interface

import { TVector } from "./utils";
interface IOptions {
  /** element that control drag range */
  outerElement?: HTMLElement;
  /** element that to drag */
  innerElement?: HTMLElement;
  /** callback on drag start */
  onDragStart?: (v: TVector) => void;
  /** callback in dragging */
  onDrag?: (v: TVector) => void;
  /** callback on drag end */
  onDragEnd?: (v: TVector) => void;
}
/**
 * use transform easy to realize drag effect
 */
declare const enableDrag: (
  element: HTMLElement,
  options?: IOptions | undefined
) => () => void;
export default enableDrag;

License

MIT

easy-drag's People

Contributors

junqiuzhang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.