手写 拖拽 修改参数

复制代码
<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>
相关推荐
Chengbei1131 分钟前
云安全漏洞挖掘SKILL、一站式云漏洞挖掘工具,支持S3爆破、IMDS探测、K8s检测与AK/SK权限利用
前端·人工智能·网络安全·云原生·容器·kubernetes·系统安全
不简说1 小时前
JS 代码技巧 vol.9 — 20 个设计模式在真实项目里的应用
前端·javascript·github
CoderLiu1 小时前
Agent 工程的下一层:为什么「把流程写成图」还不够,还需要 Graph Engineering
前端·人工智能·后端
勇往直前plus1 小时前
Vue3(篇三) Element Plus
前端·javascript·typescript·vue
爱勇宝1 小时前
《道德经》第 8 章:真正高级的能力,像水一样成事
前端·后端·程序员
Revolution611 小时前
数组本身没有 map,为什么还能直接调用:原型链怎样查找属性
前端·javascript·面试
bonechips1 小时前
RAG实战(一):EPUB 加载、文本切割与向量入库
前端·数据库
swipe1 小时前
10|(前端转全栈)库存扣减为什么最容易出事故?SKU、并发与原子更新
前端·后端·全栈
cdcdhj1 小时前
vue3中的watchEffect()监听,什么时候监听,什么时候清理,什么时候停止
前端·javascript·vue.js
huabuyu1 小时前
几百 MB 的文件为什么等几分钟才能打开?文件分片下载与渐进预览原理
前端·javascript