一个轻量级的元素拖拽库。
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.org/scripts/587662/1879553/Element%20Draggable.js
用于元素拖拽的 JavaScript 库,可用于油猴脚本或普通网页。
const element = document.querySelector('#element');
const handle = document.querySelector('#handle');
const options = {
handle: handle,
savePosition: true,
id: 'example'
};
const drag = new Draggable(element, options);
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| handle | HTMLElement | element | 指定用于拖拽的手柄。 |
| savePosition | boolean | false | 是否保存元素位置。 |
| id | string | null | 保存位置时使用的唯一标识,仅在 savePosition 为 true 时生效。 |
id。// @grant GM_getValue
// @grant GM_setValue
声明权限后,位置数据会通过油猴管理器保存,可跨页面使用。
如果未声明相关权限,位置数据将默认存储在当前页面的 localStorage 中。
当不再需要拖拽功能或移除元素时,应当手动销毁实例,以释放资源并移除事件监听。
drag.destroy();