Skip to content
This repository was archived by the owner on Aug 21, 2025. It is now read-only.

renerpdev/css-dnd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CSS-DnD

license GitHub (pre-)release npm

css-dnd example

jQuery plugin for making droppable/draggable items just adding CSS classes

Table of topics

  1. Description
  2. How to use it?
  3. Customization
  4. Examples
  5. New versions of this tool
  6. Further help

Description

This plugin uses the native Drag & Drop function of the browser. Just add some CSS classes to the element and this plugin does the rest.

Demo page

Use steps

First need to install the package from NPM

npm i -S css-dnd

Then import css-dnd and jQuery within the BODY tag

<body>
    ...
    <script src="/path/to/jquery.min.js"></script>
    <script src="/path/to/css-dnd/dist/css-dnd.min.js"></script>
</body>

Once you have done all the importation steps, you need to add the class to the elements

<body>
    <div class="dnd-droppable">
        <div class="dnd-draggable"></div>
    </div> 
    <div class="dnd-droppable"></div>    
    ...
</body>

If you want the default styles just add the css file within the HEAD tag

<head>
    ...
    <link rel="stylesheet" href="./node_modules/css-dnd/dist/css-dnd.min.css">
</head>

You can also import js and styles from a CDN

<link href="https://cdn.jsdelivr.net/npm/css-dnd@latest/dist/css-dnd.min.css" rel="stylesheet" />
<script src="/path/to/cdn/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/css-dnd@latest/dist/css-dnd.min.js"></script>

Customization

You can also change the default css classes just calling the plugin directly in your javascript code:
    $('.dnd-droppable').droppable({draggingClass:'dnd--dragging'});// for droppable elements
    $('.dnd-draggable').draggable({idPrefix:'dnd-draggable-item_'});// for draggable elements

draggingClass: changes the droppable element's default on-dragging class (default value: dnd--dragging)

idPrefix: changes the draggable element's default id (default value: dnd-draggable-item)

Also you can listening to each DnD event like:

Draggable options:

    $('.dnd-draggable').draggable({
        onDragStart: function(event,callback){
            console.log('Oh you drag me')
            // do something here before...
            callback() // call the default ondragstart behavior
        }
    });

onDragStart(ev,callback): Listening to ondragstart event

Droppable options:

    $('.dnd-droppable').droppable({
        onDrop: function(event,callback){
            console.log(`Oh you drop me into ${event.target}!`)
            // do something here before...
            callback() // call the default ondrop behavior
        }
    });

onDrop(ev,callback): Listening to ondrop event

onDragEnter(ev,callback): Listening to ondragenter event

onDragEnd(ev,callback): Listening to ondragend event

onDragOver(ev,callback): Listening to ondragover event

onDragLeave(ev,callback): Listening to ondragleave event

note: event is the event data and callback is the default event behavior function

Some basic examples

You can style it at your own way ussing the provided classes.

css-dnd example css-dnd example css-dnd example css-dnd example

Note: When the event listener dragover is triggered the element will get the class dnd-dragging or the one that user customized.

Changelog

Get update with all new versions here

Licence

Copyright (c) 2019 Rene Ricardo. Licensed under the MIT license.

Help

Send me an email if you have some doubt or just add an issue

About

jquery plugin for making droppable/draggable items just adding css classes

Resources

License

Stars

Watchers

Forks

Packages

No packages published