Git Product home page Git Product logo

draggable-list-html's Introduction

Draggable List Element

Overview

The Draggable List Element is a JavaScript class that allows you to create a list of draggable elements with custom rendering and reordering capabilities. It's designed to make it easy to create and manage lists of items that can be repositioned by dragging and dropping.

Features

  • Create a list of draggable elements.
  • Customize the rendering of each item in the list.
  • Easily reorder items by dragging and dropping.
  • Dispatch events when an item is dragged.

Installation

You can include the DraggableListElem class in your project by adding it to your JavaScript files. Make sure it is loaded after the DOM has been fully loaded.

<script src="draggable-list-elem.js"></script>

Usage

Creating a Draggable List

To create a draggable list, instantiate the DraggableListElem class with the following parameters:

  • listArray: An array of items to be displayed in the list.
  • container: The DOM element where the list will be rendered.
  • renderer: A function that defines how each item should be displayed.
  • className: A class name to be applied to each list item element.
const options = {
  listArray: myArray,
  container: document.getElementById("myListContainer"),
  renderer: (item) => `<div>${item}</div>`,
  className: "list-item",
};

const draggableList = new DraggableListElem(options);

Handling Drag Events

You can listen for drag events by using the onDrag method. This allows you to react to drag-and-drop actions and access the updated list.

draggableList.onDrag((event) => {
  const { listArray, srcElem } = event;
  // Handle the drag event here
});

Customizing Styles

You can customize the styles of your list items by modifying the CSS. You can add or modify CSS rules for the list-item class and any other classes you use in your rendering function.

.list-item {
  /* Add your custom styles here */
}

.active-drag-list {
  /* Add styles for active dragged elements here */
}

Example

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
  <div id="myListContainer"></div>

  <script src="draggable-list-elem.js"></script>
  <script>
    const myArray = ["Item 1", "Item 2", "Item 3"];
    
    const options = {
      listArray: myArray,
      container: document.getElementById("myListContainer"),
      renderer: (item) => `<div>${item}</div>`,
      className: "list-item",
    };
    
    const draggableList = new DraggableListElem(options);
    
    draggableList.onDrag((event) => {
      const { listArray, srcElem } = event;
      // Handle the drag event here
    });
  </script>
</body>
</html>

draggable-list-html's People

Contributors

actuallyroy avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.