0.1.2 • Published 5 years ago

fastdrag v0.1.2

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

Fastdrag

Build Status

A fast and lovely drag & drop library.

demo

Features

  • 👌 Easy to use
  • 🚀 Tiny size, <2Kb gziped
  • 🤣 Modular, easy to extend
  • ⚡️ Only pure JavaScript

Getting Started

Installation

You can get it on npm:

npm install fastdrag --save

As a dependency:

import Fastdrag from 'Fastdrag'

Or via CDN:

<script src="https://unpkg.com/fastdrag@latest/dist/fastdrag.min.js"></script>

Examples

let instance = new Fastdrag({
  el: document.getElementById('demo'),
  friction: 2
})

Or in a more simple way:

let instanceList = Fastdrag.to(document.getElementsByClassName('dragdrag'))

API

MethodsDescription
FastdragCreate the instance
configModify the setting
destroyDestroy the instance
onAdd eventListener to instance
toGenerate instances quickly

class Fastdrag

new Fastdrag(options): Fastdrag Instance

Create the instance.

  • options:
ParameterDefault ValueDescriptionType
eldocument.bodyThe containerHTMLElement
friction3The friction of targetnumber
rotateRange60The rotate range limitnumber
scale1The scale factor of Containernumber

config

config(options): void

Everything can be changed except 'el'.

examples:

let instance = new Fastdrag({
  el: document.getElementById('demo'),
  friction: 2
})

instance.config({
  friction: 5
})

destroy

destroy(): void

Destroy the instance.

examples:

let instance = new Fastdrag({
  el: document.getElementById('demo'),
  friction: 2
})

instance.destroy()

to

to(target, options): Fastdrag Instances

ParameterDefault ValueDescriptionType
targetdocument.bodyThe containerHTMLElement | HTMLElement[]
options{}As same as Fastdrag optionsobject

on

on(event, callback): void

Normally, you can listen the browser's native event on the instance of Fastdrag:

let demo = document.getElementById('demo')
let instance = new Fastdrag({
  el: demo,
  friction: 4
})

// Actually, animation is still moving.
demo.addEventListener('dragend', e => {
	console.log('done!')
})

But sometimes you need to get more exactly what happened:

let demo = document.getElementById('demo')
let instance = new Fastdrag({
  el: demo,
  friction: 4
})

instance.on('end', e => {
  console.log('done!')
})
  • params:
ParameterDefault ValueDescriptionType
eventdocument.bodyThe event namestring
callbackn => nThe callback functionfunction
  • event list:
EventDescription
startAs same as dragstart
moveEvery AnimationFrame of motion would trigger it
endTrigger if Animation is stopped

Inspiration

Inspired by: https://github.com/ClassicOldSong/Drag.js

License

MIT