手写 拖拽 修改参数

复制代码
<template>
  <div style="display: flex; justify-content: space-between; height: 100vh;">
    <div>
      模型
      <div
        class="moxing"
        draggable="true"
        id="moxingElement"
        @dragstart="onDragStart($event)"
      >
        西周
      </div>
    </div>

    <div class="huabu" @dragover.prevent @drop="onDrop" ref="canvas">
      画布
    </div>

    <div class="canshu">
      参数
      <div>
        宽度:<input type="text" v-model.number="width" />
        高度:<input type="text" v-model.number="height" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      width: 200,  // 初始宽度
      height: 200, // 初始高度
    };
  },
  methods: {
    onDragStart(event) {
      const draggableElement = event.target;
      event.dataTransfer.setData('text/plain', draggableElement.id);
    },

    onDrop(event) {
      const elementId = event.dataTransfer.getData('text/plain');
      const draggedElement = document.getElementById(elementId);
      const canvas = this.$refs.canvas;

      if (draggedElement) {
        canvas.innerHTML = ''; // 清空画布内容
        canvas.appendChild(draggedElement); // 将拖动的元素添加到画布

        // 更新样式
        draggedElement.style.width = `${this.width}px`;
        draggedElement.style.height = `${this.height}px`;
      } else {
        console.error('Dragged element not found with ID:', elementId);
      }
    },
  },
};
</script>

<style>
.moxing {
  width: 200px;
  height: 200px;
  background-color: red;
  cursor: move;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white; /* 添加文本颜色以便在红色背景上可见 */
}

.huabu {
  width: 500px;
  height: 500px;
  border: 1px solid black;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f0f0f0; /* 浅灰色背景以便更好地查看 */
}

.canshu {
  width: 300px; /* 调整宽度以适应参数输入 */
  height: 500px;
  border: 1px solid black;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
</style>
相关推荐
万少2 小时前
HarmonyOS官方模板集成创新活动-流蓝卡片
前端·harmonyos
-To be number.wan5 小时前
C++ 赋值运算符重载:深拷贝 vs 浅拷贝的生死线!
前端·c++
噢,我明白了5 小时前
JavaScript 中处理时间格式的核心方式
前端·javascript
纸上的彩虹6 小时前
半年一百个页面,重构系统也重构了我对前端工作的理解
前端·程序员·架构
be or not to be7 小时前
深入理解 CSS 浮动布局(float)
前端·css
LYFlied7 小时前
【每日算法】LeetCode 1143. 最长公共子序列
前端·算法·leetcode·职场和发展·动态规划
老华带你飞7 小时前
农产品销售管理|基于java + vue农产品销售管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
小徐_23337 小时前
2025 前端开源三年,npm 发包卡我半天
前端·npm·github
GIS之路8 小时前
GIS 数据转换:使用 GDAL 将 Shp 转换为 GeoJSON 数据
前端
JIngJaneIL8 小时前
基于springboot + vue房屋租赁管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端