draggable.js

原文鏈接: https://www.npmjs.com/package/draggable

High performance, fully cross browser, full featured drag and drop in a tiny (2k gzipped), dependency-free package.

安裝

>npm i draggable --save

Demo

http://bcherny.github.io/draggable/demos/basic/

Usage

HTML

<div id="id"></div>

JavaScript

Using browser globals:

var element = document.getElementById('id');
var options = {
  grid: 10,
  onDrag: function(){ ... }
};
new Draggable (element, options);

Using AMD/CommonJS:

var Draggable = require ('Draggable');
var element = document.getElementById('id');
new Draggable (element);

Dependencies

None!

Options

Option Type Default Description
grid Number 0 grid size for snapping on drag
handle Element null the handle of the draggable; if null, the whole element is the handle
filterTarget Function(target) null prevent drag when target passes this test
limit Element, Function(x, y, x0, y0), or Object { x: null, y: null } limit x/y drag bounds
threshold Number 0 threshold before drag begins (in px)
setCursor Boolean (truthy) false change cursor to move?
setPosition Boolean (truthy) true change draggable position to absolute?
smoothDrag Boolean (truthy) true snap to grid only when dropped, not during drag
useGPU Boolean (truthy) true move graphics calculation/composition to the GPU? (modern browsers only, graceful degradation)

Events

Event Arguments
onDrag element, x, y, event
onDragStart element, x, y, event
onDragEnd element, x, y, event

Instance methods

Method Arguments Returns Description
get --- {Object} {x, y} Get the current coordinates
set {Number} x, {Number} y instance Move to the specified coordinates
setOption {String} property, {Mixed} value instance Set an option in the live instance
destroy --- --- Unbind the instance's DOM event listeners

Notes

Options.limit accepts arguments in several forms:

// no limit
limit: null

// limit x, but leave y unbounded
limit: {
  x: [1,10],
  y: null
}

// limit both axes
limit: {
  x: [1,10],
  y: [1,500]
}

// bound x, set y to a constant
limit: {
  x: [1,10],
  y: 5
}

// bound with an element
limit: document.getElementById('id')

// bound with a custom function
limit: function (
  x,  // current X coordinate
  y,  // current Y coordinate
  x0, // original X coordinate (where drag was started)
  y0  // original Y coordinate (where drag was started)
) {

  var radius = 100,
    dx = x - x0,
    dy = y - y0,
    distance = Math.sqrt(dx*dx + dy*dy),

    // only allow dragging within a circle of radius 100
    outOfRange = distance > radius;


  // if our point is outside of the circle, compute the
  // point on the circle's edge closest to our point
  if (outOfRange) {

    x = x0 + radius * (x - x0) / distance;
    y = y0 + radius * (y - y0) / distance;

  }

  return {
    x: x,
    y: y
  };

}

Tested on

  • Chrome 29 on OSX
  • Chrome 28 on Windows
  • Firefox 23 on OSX
  • Firefox 21 on Windows
  • Opera 16 on OSX
  • Safari 6 on OSX
  • Safari 6 on iPhone4/iOS6
  • Safari 6 on iPhone5/iOS6
  • Safari 6 on iPad2/iOS6
  • Safari 6 on iPad3/iOS6
  • Internet Explorer 8-11 on Windows

To do

  • Improve performance on old iOS
  • Unit tests
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末文狱,一起剝皮案震驚了整個濱河市赢笨,隨后出現(xiàn)的幾起案子焙蚓,更是在濱河造成了極大的恐慌拂檩,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,265評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件牙咏,死亡現(xiàn)場離奇詭異纽谒,居然都是意外死亡,警方通過查閱死者的電腦和手機夺脾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,078評論 2 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來茉继,“玉大人咧叭,你說我怎么就攤上這事÷睿” “怎么了佳簸?”我有些...
    開封第一講書人閱讀 156,852評論 0 347
  • 文/不壞的土叔 我叫張陵乙墙,是天一觀的道長颖变。 經(jīng)常有香客問我,道長听想,這世上最難降的妖魔是什么腥刹? 我笑而不...
    開封第一講書人閱讀 56,408評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮汉买,結果婚禮上衔峰,老公的妹妹穿的比我還像新娘。我一直安慰自己蛙粘,他們只是感情好垫卤,可當我...
    茶點故事閱讀 65,445評論 5 384
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著出牧,像睡著了一般穴肘。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上舔痕,一...
    開封第一講書人閱讀 49,772評論 1 290
  • 那天评抚,我揣著相機與錄音豹缀,去河邊找鬼。 笑死慨代,一個胖子當著我的面吹牛邢笙,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播侍匙,決...
    沈念sama閱讀 38,921評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼氮惯,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了想暗?” 一聲冷哼從身側響起筐骇,我...
    開封第一講書人閱讀 37,688評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎江滨,沒想到半個月后铛纬,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,130評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡唬滑,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,467評論 2 325
  • 正文 我和宋清朗相戀三年告唆,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片晶密。...
    茶點故事閱讀 38,617評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡擒悬,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出稻艰,到底是詐尸還是另有隱情懂牧,我是刑警寧澤,帶...
    沈念sama閱讀 34,276評論 4 329
  • 正文 年R本政府宣布尊勿,位于F島的核電站僧凤,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏元扔。R本人自食惡果不足惜躯保,卻給世界環(huán)境...
    茶點故事閱讀 39,882評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望澎语。 院中可真熱鬧途事,春花似錦、人聲如沸擅羞。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,740評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽减俏。三九已至召烂,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間垄懂,已是汗流浹背骑晶。 一陣腳步聲響...
    開封第一講書人閱讀 31,967評論 1 265
  • 我被黑心中介騙來泰國打工痛垛, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人桶蛔。 一個月前我還...
    沈念sama閱讀 46,315評論 2 360
  • 正文 我出身青樓匙头,卻偏偏與公主長得像,于是被迫代替她去往敵國和親仔雷。 傳聞我的和親對象是個殘疾皇子蹂析,可洞房花燭夜當晚...
    茶點故事閱讀 43,486評論 2 348

推薦閱讀更多精彩內(nèi)容