手写 拖拽 修改参数

复制代码
<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>
相关推荐
MrGaoGang15 分钟前
耗时1年,终于我也拥有属于自己的AI工作流
前端·agent·ai编程
没有鸡汤吃不下饭29 分钟前
前端【数据类型】 No.1 Javascript的数据类型与区别
前端·javascript·面试
跟橙姐学代码32 分钟前
Python时间处理秘籍:别再让日期时间卡住你的代码了!
前端·python·ipython
汤姆Tom36 分钟前
从零到精通:现代原子化 CSS 工具链完全攻略 | PostCSS × UnoCSS × TailwindCSS 深度实战
前端·css·面试
菜市口的跳脚长颌37 分钟前
Web3基础
前端
RoyLin40 分钟前
TypeScript设计模式:代理模式
前端·后端·typescript
IT_陈寒2 小时前
Vue3性能优化实战:这5个技巧让我的应用加载速度提升了70%
前端·人工智能·后端
树上有只程序猿2 小时前
react 实现插槽slot功能
前端
stoneship3 小时前
Web项目减少资源加载失败白屏问题
前端