当前位置:首页 > JavaScript

js实现阿甘图可拖拽

2026-03-01 22:43:27JavaScript

实现阿甘图(Gantt Chart)可拖拽功能

阿甘图是一种项目管理工具,用于展示任务的时间安排和进度。实现可拖拽功能可以增强用户体验,允许用户调整任务的时间范围或顺序。以下是使用JavaScript实现可拖拽阿甘图的步骤。

使用HTML和CSS构建基础结构

创建一个基础的HTML结构,包含任务列表和时间轴。CSS用于样式化阿甘图的布局。

<div class="gantt-container">
  <div class="gantt-header">
    <div class="gantt-time-scale"></div>
  </div>
  <div class="gantt-body">
    <div class="gantt-task" draggable="true">
      <div class="gantt-task-bar"></div>
    </div>
  </div>
</div>
.gantt-container {
  width: 100%;
  overflow-x: auto;
}
.gantt-header {
  display: flex;
  border-bottom: 1px solid #ccc;
}
.gantt-time-scale {
  display: flex;
  min-width: 1000px;
}
.gantt-body {
  position: relative;
  height: 500px;
}
.gantt-task {
  position: absolute;
  height: 30px;
  cursor: move;
}
.gantt-task-bar {
  height: 100%;
  background-color: #4CAF50;
  border-radius: 3px;
}

添加拖拽事件监听

使用JavaScript为任务条添加拖拽事件监听,实现拖拽功能。

document.addEventListener('DOMContentLoaded', function() {
  const tasks = document.querySelectorAll('.gantt-task');

  tasks.forEach(task => {
    task.addEventListener('dragstart', handleDragStart);
    task.addEventListener('dragend', handleDragEnd);
  });

  const ganttBody = document.querySelector('.gantt-body');
  ganttBody.addEventListener('dragover', handleDragOver);
  ganttBody.addEventListener('drop', handleDrop);
});

function handleDragStart(e) {
  e.dataTransfer.setData('text/plain', e.target.id);
  e.target.classList.add('dragging');
}

function handleDragEnd(e) {
  e.target.classList.remove('dragging');
}

function handleDragOver(e) {
  e.preventDefault();
}

function handleDrop(e) {
  e.preventDefault();
  const taskId = e.dataTransfer.getData('text/plain');
  const task = document.getElementById(taskId);
  const ganttBody = e.currentTarget;

  const rect = ganttBody.getBoundingClientRect();
  const x = e.clientX - rect.left;
  const y = e.clientY - rect.top;

  task.style.left = `${x}px`;
  task.style.top = `${y}px`;
}

实现时间范围调整

拖拽任务条的边缘可以调整任务的时间范围。为任务条的左右边缘添加拖拽手柄。

<div class="gantt-task" draggable="true">
  <div class="gantt-task-bar">
    <div class="resize-handle left"></div>
    <div class="resize-handle right"></div>
  </div>
</div>
.resize-handle {
  width: 5px;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.2);
  cursor: ew-resize;
}
.resize-handle.left {
  float: left;
}
.resize-handle.right {
  float: right;
}
document.querySelectorAll('.resize-handle').forEach(handle => {
  handle.addEventListener('mousedown', initResize);
});

function initResize(e) {
  const taskBar = e.target.parentElement;
  const startX = e.clientX;
  const startWidth = taskBar.offsetWidth;

  function doResize(e) {
    const newWidth = startWidth + (e.clientX - startX);
    taskBar.style.width = `${newWidth}px`;
  }

  function stopResize() {
    window.removeEventListener('mousemove', doResize);
    window.removeEventListener('mouseup', stopResize);
  }

  window.addEventListener('mousemove', doResize);
  window.addEventListener('mouseup', stopResize);
}

集成第三方库简化实现

使用现成的库如dhtmlxGantt可以快速实现可拖拽阿甘图。

<link rel="stylesheet" href="https://cdn.dhtmlx.com/gantt/edge/dhtmlxgantt.css">
<script src="https://cdn.dhtmlx.com/gantt/edge/dhtmlxgantt.js"></script>
<div id="gantt_container"></div>
gantt.init('gantt_container');
gantt.config.drag_move = true;
gantt.config.drag_resize = true;

gantt.parse({
  data: [
    { id: 1, text: 'Task 1', start_date: '2023-01-01', duration: 3 },
    { id: 2, text: 'Task 2', start_date: '2023-01-05', duration: 4 }
  ]
});

注意事项

  • 拖拽功能需要兼容移动设备时,需添加触摸事件支持。
  • 时间轴的刻度需要与任务的位置和宽度对应,确保拖拽后的时间计算准确。
  • 对于复杂的项目需求,建议使用专业库如dhtmlxGanttfrappe-gantt

js实现阿甘图可拖拽

标签: 阿甘拖拽
分享给朋友:

相关文章

前端实现拖拽功能vue

前端实现拖拽功能vue

实现拖拽功能的基本方法 在Vue中实现拖拽功能可以通过HTML5的拖放API或第三方库如vuedraggable来完成。HTML5的拖放API提供了原生支持,而vuedraggable则简化了列表拖拽…

vue实现carousel拖拽

vue实现carousel拖拽

Vue实现Carousel拖拽功能 实现一个支持拖拽的Carousel组件可以通过结合Vue的响应式特性和原生DOM事件来实现。以下是具体实现方法: 基础Carousel结构 创建一个基础的Car…

vue 实现拖拽甘特图

vue 实现拖拽甘特图

实现拖拽甘特图的核心思路 Vue 实现拖拽甘特图需要结合第三方库或自定义逻辑,核心功能包括任务条渲染、时间轴刻度、拖拽交互、数据绑定等。以下是具体实现方法: 使用第三方库(推荐) vue-gantt…

vue实现carousel拖拽

vue实现carousel拖拽

实现拖拽轮播的基本思路 在Vue中实现可拖拽的Carousel组件需要结合触摸事件(touchstart、touchmove、touchend)和鼠标事件(mousedown、mousemove、mo…

jquery 拖拽

jquery 拖拽

jQuery 拖拽实现方法 使用 jQuery 实现拖拽功能可以通过多种方式完成,以下是常见的几种方法: 使用 jQuery UI 的拖拽功能 jQuery UI 提供了 draggable 方法,…

vue实现盒子拖拽

vue实现盒子拖拽

实现盒子拖拽的基本思路 在Vue中实现盒子拖拽功能,可以通过监听鼠标事件(mousedown、mousemove、mouseup)来实现。核心逻辑是记录拖拽起始位置,计算位移差值,并更新盒子的位置。…