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

g-drag

v0.1.1

Published

g-drag is a simple JavaScript drag-and-drop library.

Readme

g-drag

GitHub License PRs Welcome

g-drag 是一个简单的 JavaScript 拖拽库

English

安装

npm install g-drag

快速示例

浏览器兼容性

g-drag 提供了对旧浏览器的兼容版本(legacy build)

如果你的项目需要支持旧版浏览器,请使用以下方式引入:

import {Draggable} from 'g-drag/legacy';

基本用法

import {Draggable} from 'g-drag';

const draggable = document.getElementById('draggable');
const dropzone = document.querySelector('.dropzone');

new Draggable(draggable, {
  data: {id: 'block-1'},
  dropTargets: {
    onDrop: () => {
      console.log('已放置');
      dropzone.classList.remove('active');
    },
    onEnter: () => {
      console.log('进入拖放区域');
      dropzone.classList.add('active');
    },
    onLeave: () => {
      console.log('离开拖放区域');
      dropzone.classList.remove('active');
    },
    selector: '.dropzone',
  },
  onDragEnd: (e) => {
    console.log('拖拽结束', e);
    const targetEl = e.target as HTMLElement;
    const proxyRect = e.proxy.getBoundingClientRect();

    targetEl.style.left = `${proxyRect.left}px`;
    targetEl.style.top = `${proxyRect.top}px`;
  },
  onDragMove: (e) => {
    console.log('正在拖动', e);
  },
  onDragStart: (e) => {
    console.log('开始拖动', e);
  },
  updatePosition: (el, dx, dy) => {
    const rect = el.getBoundingClientRect();
    el.style.left = `${rect.left + dx}px`;
    el.style.top = `${rect.top + dy}px`;
  },
});

API 参考

Draggable(target: HTMLElement, options: DragOptions)

创建可拖拽实例:

  • target: 需要设置为可拖拽的 HTML 元素
  • options: 配置选项

| 选项 | 类型 | 描述 | |----------------|---------------------------------------------------|---------------| | data | Record<string, any> | 随拖拽事件传递的自定义数据 | | dropTargets | DropTargets | 拖放目标配置 | | onDragStart | (event: DragEvent) => boolean | 拖拽开始时触发 | | onDragMove | (event: DragEvent) => void | 拖拽移动时触发 | | onDragEnd | (event: DragEvent) => void | 拖拽结束时触发 | | setupProxy | (proxy: HTMLElement, event: DragEvent) => void | 自定义代理元素设置 | | updatePosition | (el: HTMLElement, dx: number, dy: number) => void | 自定义位置更新逻辑 |

DropTargets

| 选项 | 类型 | 描述 | |----------|--------------------------------------|-------------| | selector | ((el: Element) => boolean) | string | 拖放目标选择器 | | onEnter | (event: DragEvent) => void | 进入拖放目标时触发 | | onLeave | (event: DragEvent) => void | 离开拖放目标时触发 | | onDrop | (event: DragEvent) => void | 在拖放目标上释放时触发 |

DragEvent

| 选项 | 类型 | 描述 | |---------------|------------------------------------------|--------| | data | Record<string, any> | 自定义数据 | | originalEvent | MouseEvent | PointerEvent | TouchEvent | 原始事件对象 | | phase | 'start' | 'move' | 'end' | 拖拽阶段 | | proxy | HTMLElement | 代理元素 | | target | HTMLElement | 原始拖拽元素 |

贡献

欢迎贡献代码!如果您发现任何问题或有改进建议,请随时提交 issue 或 pull request

License

MIT