Giter Club home page Giter Club logo

dragmove.js's Introduction

dragmove.js

A super tiny Javascript library to make DOM elements draggable and movable. Has touch screen support. Zero dependencies and 500 bytes Gzipped. Demo here.

Usage

Node

npm install @knadh/dragmove
import { dragmove } from @knadh/dragmove;

// (target, handler, onStart(target, x, y), onEnd(target, x, y)).
// onStart and onEnd are optional callbacks that receive target element, and x, y coordinates.

dragmove(document.querySelector("#box"), document.querySelector("#box .drag-handle"));

ES6 module

Check this example to include dragmove.js as a <script> directly on an HTML page.

Licensed under the MIT License.

dragmove.js's People

Contributors

0xflotus avatar knadh avatar stoyandimitrov avatar suxianbaozi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dragmove.js's Issues

Unregister element

If an element no longer needs to be dragged, could it be "unregistered" and removed from the callbacks array? This would help save memory especially in single page apps where several views could potentially be shown or rerendered.

const instance = dragmove(...)
instance.unregister()

// or

const id = dragmove(...)
enddragmove(id)

Uncaught error when build with webpack

Hi,
Using webpack in development (with watch without build and finalization) everything works perfectly. But when I go to build I get this error from the browser: TypeError: can't access property "addEventListener", (void 0) is undefined .
I use Laravel Mix for build the assets. I paste here the lines of code I use, maybe I wrote something wrong.


(function () {
  'use strict';

  document.addEventListener('DOMContentLoaded', handleDOMContentLoaded);

  function handleDOMContentLoaded() {
    document.querySelectorAll('.collage__item').forEach(function (el) {
      dragmove(el, el);
    })
  }
})();

onEnd functions all fire for every `mouseup` event

On line 49,

  document.addEventListener(_isTouch ? "touchend" : "mouseup", function() {
   /* ... */
  }

I think this should be target instead of document, no? This way, only the functions specific to an element get fired (not for every onEnd ever registered).

onEnd won't run if the handler has child nodes covering it

On line 53,

if (onEnd && e.target === handler) {
  onEnd(target, parseInt(target.style.left), parseInt(target.style.top));
}

If the handler has child nodes covering it, e.target will end up being the child node you clicked on, not the handler itself, so onEnd won't run. One way to replicate this bug is through the example in docs/index.html by changing it like so:

Add a span element to one of the handlers:

<div id="test" class="parent" data-drag-bound="true">
  <div class="handle" style=""><span>Drag here</span></div>
</div>

and make sure .handle has a minimum height, like say 24px for this example:

.handle {
  background: #888;
  cursor: move;
  width: 130px;
  min-height: 24px;
  color: #fff;
  padding: 10px;
}

Then try dragging the element outside the view by clicking on its text first, and you'll see that the snapping won't happen.

I was working on adding buttons and menus in the handler nodes in my project, which is how I found the bug. The dragging works perfectly fine, just to be clear. It's just onEnd that's the issue.

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.