npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@mooses/drag

v0.0.3

Published

拖拽,支持边界限制、吸附、栅格对齐、速度控制及拖拽回调

Readme

enableDrag 函数使用文档

在线例子

概述

enableDrag 是一个为标签元素添加拖拽功能的函数。 该函数允许开发者为网页中的元素启用可拖拽的交互效果, 同时提供了灵活的自定义选项,例如拖拽方向的限制、边界吸附、栅格对齐等功能。

函数签名

enableDrag(element: HTMLElement, container: HTMLElement, options?: Object): Function

参数说明

| 参数 | 类型 | 必填 | 描述 | | --------- | :---------: | :--: | :------------------------------------------: | | element | HTMLElement | 是 | 启用拖拽功能的 DOM 元素 | | container | HTMLElement | 是 | markdown编译为html | | options | Object | 否 | 可选配置对象,用于自定义拖拽行为(详见下表) |

配置选项

options 对象可以包含以下属性:

| 属性 | 类型 | 默认值 | 描述 | | -------------- | :------: | :------: | :----------------------------------------------------------------------------: | | boundaryLimit | boolean | false | 是否限制元素只能在容器内部拖动。 | | direction | string | 'both' | 限制拖拽方向,可选值:'horizontal'(水平)、'vertical'(垂直)、'both'(两者) | | maxSpeed | number | 40 | 限制拖拽速度(单位:像素),用于避免拖拽时移动过快 | | snapToBoundary | boolean | false | 是否启用边界吸附功能,元素靠近容器边界时自动对齐到边缘 | | snapThreshold | number | 20 | 吸附到容器边缘的距离阈值(单位:像素) | | enableGridSnap | boolean | false | 是否启用栅格对齐,拖拽时元素会对齐到栅格网格 | | gridSize | number | 10 | 栅格对齐的网格尺寸(单位:像素),启用 enableGridSnap 时有效 | | onStart | function | () => {} | 拖拽开始时的回调函数 | | onMove | function | () => {} | 拖拽移动时的回调函数,传递当前拖拽位置 { top, left } 和事件对象 | | onEnd | function | () => {} | 拖拽结束时的回调函数 |

示例

示例1:启用基础拖拽

enableDrag(element, container);

示例2:限制拖拽方向并启用边界限制

enableDrag(element, container, {
    // 限制在容器内拖动
    boundaryLimit: true,
    // 只允许水平拖动
    direction: 'horizontal',
});

示例3:启用边界吸附和栅格对齐

enableDrag(element, container, {
    // 启用边界吸附
    snapToBoundary: true,
    // 吸附距离阈值为 15 像素
    snapThreshold: 15,
    // 启用栅格对齐
    enableGridSnap: true,
    // 栅格大小为 20 像素
    gridSize: 20,
});

示例4:使用回调函数

enableDrag(element, container, {
    onStart: () => {
        console.log('开始拖拽');
    },
    onMove: (position) => {
        console.log('当前位置:', position);
    },
    onEnd: () => {
        console.log('结束拖拽');
    },
});