GithubHelp home page GithubHelp logo

doc22940 / draggabilly Goto Github PK

View Code? Open in Web Editor NEW

This project forked from desandro/draggabilly

1.0 1.0 0.0 367 KB

:point_down: Make that shiz draggable

Home Page: https://draggabilly.desandro.com

HTML 32.42% CSS 1.83% JavaScript 65.75%

draggabilly's Introduction

Draggabilly

Make that shiz draggable

draggabilly.desandro.com

Rad because it supports IE10+ and touch devices.

Install

Download

Package managers

Install with npm: npm install draggabilly

Install with Bower: bower install draggabilly

CDN

Link directly to draggabilly.pkgd.min.js on unpkg.com.

<script src="https://unpkg.com/draggabilly@2/dist/draggabilly.pkgd.min.js"></script>

Usage

Initialize Draggabilly as a jQuery plugin

var $draggable = $('.draggable').draggabilly({
  // options...
})

Initialize Draggabilly with vanilla JS

var elem = document.querySelector('.draggable');
var draggie = new Draggabilly( elem, {
  // options...
});

// or pass in selector string as first argument
var draggie = new Draggabilly( '.draggable', {
  // options...
});

// if you have multiple .draggable elements
// get all draggie elements
var draggableElems = document.querySelectorAll('.draggable');
// array of Draggabillies
var draggies = []
// init Draggabillies
for ( var i=0; i < draggableElems.length; i++ ) {
  var draggableElem = draggableElems[i];
  var draggie = new Draggabilly( draggableElem, {
    // options...
  });
  draggies.push( draggie );
}

Classes

  • .is-pointer-down added when the user's pointer (mouse, touch, pointer) first presses down.
  • .is-dragging added when elements starts to drag.

Options

axis

Type: String

Values: 'x' or 'y'

axis: 'x'

Constrains movement to horizontal or vertical axis.

containment

Type: Element, Selector String, or Boolean

containment: '.container'

Contains movement to the bounds of the element. If true, the container will be the parent element.

grid

Type: Array

Values: [ x, y ]

grid: [ 20, 20 ]

Snaps the element to a grid, every x and y pixels.

handle

Type: Selector String

handle: '.handle'

Specifies on what element the drag interaction starts.

handle is useful for when you do not want all inner elements to be used for dragging, like inputs and forms. See back handle example on CodePen.

Events

Bind events with jQuery with standard jQuery event methods .on(), .off(), and .one(). Inside jQuery event listeners this refers to the Draggabilly element.

// jQuery
function listener(/* parameters */) {
  // get Draggabilly instance
  var draggie = $(this).data('draggabilly');
  console.log( 'eventName happened', draggie.position.x, draggie.position.y );
}
// bind event listener
$draggable.on( 'eventName', listener );
// unbind event listener
$draggable.off( 'eventName', listener );
// bind event listener to trigger once. note ONE not ON
$draggable.one( 'eventName', function() {
  console.log('eventName happened just once');
});

Bind events with vanilla JS with .on(), .off(), and .once() methods. Inside vanilla JS event listeners this refers to the Draggabilly instance.

// vanilla JS
function listener(/* parameters */) {
  console.log( 'eventName happened', this.position.x, this.position.y );
}
// bind event listener
draggie.on( 'eventName', listener );
// unbind event listener
draggie.off( 'eventName', listener );
// bind event listener to trigger once. note ONCE not ONE or ON
draggie.once( 'eventName', function() {
  console.log('eventName happened just once');
});

dragStart

Triggered when dragging starts and the element starts moving. Dragging starts after the user's pointer has moved a couple pixels to allow for clicks.

// jQuery
$draggable.on( 'dragStart', function( event, pointer ) {...})
// vanilla JS
draggie.on( 'dragStart', function( event, pointer ) {...})
  • event - Type: Event - the original mousedown or touchstart event
  • pointer - Type: MouseEvent or Touch - the event object that has .pageX and .pageY

dragMove

Triggered when dragging moves.

// jQuery
$draggable.on( 'dragMove', function( event, pointer, moveVector ) {...})
// vanilla JS
draggie.on( 'dragMove', function( event, pointer, moveVector ) {...})
  • event - Type: Event - the original mousemove or touchmove event
  • pointer - Type: MouseEvent or Touch - the event object that has .pageX and .pageY
  • moveVector Type: Object - How far the pointer has moved from its start position { x: 20, y: -30 }

dragEnd

Triggered when dragging ends.

// jQuery
$draggable.on( 'dragEnd', function( event, pointer ) {...})
// vanilla JS
draggie.on( 'dragEnd', function( event, pointer ) {...})
  • event - Type: Event - the original mouseup or touchend event
  • pointer - Type: MouseEvent or Touch - the event object that has .pageX and .pageY

pointerDown

Triggered when the user's pointer (mouse, touch, pointer) presses down.

// jQuery
$draggable.on( 'pointerDown', function( event, pointer ) {...})
// vanilla JS
draggie.on( 'pointerDown', function( event, pointer ) {...})
  • event - Type: Event - the original mousedown or touchstart event
  • pointer - Type: MouseEvent or Touch - the event object that has .pageX and .pageY

pointerMove

Triggered when the user's pointer moves.

// jQuery
$draggable.on( 'pointerMove', function( event, pointer, moveVector ) {...})
// vanilla JS
draggie.on( 'pointerMove', function( event, pointer, moveVector ) {...})
  • event - Type: Event - the original mousemove or touchmove event
  • pointer - Type: MouseEvent or Touch - the event object that has .pageX and .pageY
  • moveVector Type: Object - How far the pointer has moved from its start position { x: 20, y: -30 }

pointerUp

Triggered when the user's pointer unpresses.

// jQuery
$draggable.on( 'pointerUp', function( event, pointer ) {...})
// vanilla JS
draggie.on( 'pointerUp', function( event, pointer ) {...})
  • event - Type: Event - the original mouseup or touchend event
  • pointer - Type: MouseEvent or Touch - the event object that has .pageX and .pageY

staticClick

Triggered when the user's pointer is pressed and unpressed and has not moved enough to start dragging.

click events are hard to detect with draggable UI, as they are triggered whenever a user drags. Draggabilly's staticClick event resolves this, as it is triggered when the user has not dragged.

// jQuery
$draggable.on( 'staticClick', function( event, pointer ) {...})
// vanilla JS
draggie.on( 'staticClick', function( event, pointer ) {...})
  • event - Type: Event - the original mouseup or touchend event
  • pointer - Type: MouseEvent or Touch - the event object that has .pageX and .pageY

Methods

disable

// jQuery
$draggable.draggabilly('disable')
// vanilla JS
draggie.disable()

enable

// jQuery
$draggable.draggabilly('enable')
// vanilla JS
draggie.enable()

setPosition

// jQuery
$draggable.draggabilly( 'setPosition', x, y )
// vanilla JS
draggie.setPosition( x, y )
  • x - Type: Number - horizontal position
  • y - Type: Number - vertical position

destroy

// jQuery
$draggable.draggabilly('destroy')
// vanilla JS
draggie.destroy()

jQuery.fn.data('draggabilly')

Get the Draggabilly instance from a jQuery object. Draggabilly instances are useful to access Draggabilly properties.

var draggie = $('.draggable').data('draggabilly')
// access Draggabilly properties
console.log( 'draggie at ' + draggie.position.x + ', ' + draggie.position.y )

Properties

position

draggie.position
// => { x: 20, y: -30 }
  • position - Type: Object
  • x - Type: Number
  • y - Type: Number

Module loaders

Webpack & Browserify

Install Draggabilly with npm.

npm install draggabilly
var Draggabilly = require('draggabilly');

var draggie = new Draggabilly( '.draggable', {
  // options
});

To use Draggabilly as a jQuery plugin, you need to install and call jQuery Bridget.

npm install jquery-bridget
var $ = require('jquery');
var jQueryBridget = require('jquery-bridget');
var Draggabilly = require('draggabilly');

// make Draggabilly a jQuery plugin
jQueryBridget( 'draggabilly', Draggabilly, $ );
// now you can use $().draggabilly()
$('.draggable').draggabilly({...})

RequireJS

Draggabilly works with RequireJS.

You can require draggabilly.pkgd.js..

requirejs( [
  'path/to/draggabilly.pkgd.js',
], function( Draggabilly ) {
  new Draggabilly( '.draggable', {...});
});

To use Draggabilly as a jQuery plugin with RequireJS and draggabilly.pkgd.js, you need to call jQuery Bridget.

// require the require function
requirejs( [ 'require', 'jquery', 'path/to/draggabilly.pkgd.js' ],
  function( require, $, Draggabilly ) {
    // require jquery-bridget, it's included in draggabilly.pkgd.js
    require( [ 'jquery-bridget/jquery-bridget' ],
    function( jQueryBridget ) {
      // make Draggabilly a jQuery plugin
      jQueryBridget( 'draggabilly', Draggabilly, $ );
      // now you can use $().draggabilly()
      $('.draggable').draggabilly({...})
    }
  );
});

Or, you can manage dependencies with a package manager like npm or Bower. Set baseUrl to the package directory and set a path config for all your application code.

requirejs.config({
  baseUrl: 'node_modules/',
  paths: {
    // path your your app
    app: '../'
  }
});

requirejs( [
  'draggabilly/draggabilly',
  'app/my-component.js'
], function( Draggabilly, myComp ) {
  new Draggabilly( '.draggable', {...});
});

To use Draggabilly as a jQuery plugin with a package manager, you need install and to call jQuery Bridget.

requirejs.config({
  baseUrl: 'node_modules/',
  paths: {
    jquery: 'jquery/dist/jquery'
  }
});

requirejs( [
    'jquery',
    'draggabilly/draggabilly',
    'jquery-bridget/jquery-bridget'
  ],
  function( $, Draggabilly, jQueryBridget ) {
    // make Draggabilly a jQuery plugin
    jQueryBridget( 'draggabilly', Draggabilly, $ );
    // now you can use $().draggabilly()
    $('.draggable').draggabilly({...})
});

Browser support

Draggabilly v2.2 supports Chrome 36+, Firefox 23+, Safari 9+ (mobile & desktop), IE10+, and Edge 12+.

Use Draggabilly v2.1 for Android 4+ and Safari 6+ support.

Use Draggabilly v1 for IE8 & 9, and Android 2.3+ support.

License

Draggabilly is released under the MIT License. Have at it.


Made by David DeSandro ๐Ÿ˜ป

draggabilly's People

Contributors

desandro avatar lluchs avatar andrelion avatar baldwicc avatar joscha avatar npmcdn-to-unpkg-bot avatar

Stargazers

Acampbell avatar

Watchers

 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.