Element Draggable

A lightweight draggable library.

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.greasyfork.org/scripts/587662/1879553/Element%20Draggable.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

作者
ryxel
版本
1.0
建立日期
2026-07-19
更新日期
2026-07-19
尺寸
6.3 KB
授權條款
MIT

Draggable

A JavaScript library for dragging elements, usable in Tampermonkey scripts or regular web pages.

Usage

const element = document.querySelector('#element');
const handle = document.querySelector('#handle');

const options = {
    handle: handle,
    savePosition: true,
    id: 'example'
};

const drag = new Draggable(element, options);

Parameters

  • element (HTMLElement): The target DOM element to be dragged.
  • options (Object, optional): Configuration object.

Options Configuration

Parameter Type Default Description
handle HTMLElement element Specifies the drag handle.
savePosition boolean false Whether to save the element position.
id string null Unique identifier used for saving the position. Only works when savePosition is true.

Notes

  • A unique id must be provided when position saving is enabled.
  • When using in a Tampermonkey script, if position saving is enabled, it is recommended to declare the following permissions in the script header:
// @grant GM_getValue
// @grant GM_setValue

With these permissions, position data will be stored through the userscript manager and can persist across pages.

If the permissions are not declared, position data will be stored in the current page's localStorage.

Destroy Instance

When the dragging functionality is no longer needed or the element is removed, the instance should be manually destroyed to release resources and remove event listeners.

drag.destroy();