手写 拖拽 修改参数

复制代码
<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>
相关推荐
万少4 小时前
HarmonyOS 开发必会 5 种 Builder 详解
前端·harmonyos
橙序员小站6 小时前
Agent Skill 是什么?一文讲透 Agent Skill 的设计与实现
前端·后端
炫饭第一名9 小时前
速通Canvas指北🦮——基础入门篇
前端·javascript·程序员
王晓枫9 小时前
flutter接入三方库运行报错:Error running pod install
前端·flutter
符方昊9 小时前
React 19 对比 React 16 新特性解析
前端·react.js
ssshooter9 小时前
又被 Safari 差异坑了:textContent 拿到的值居然没换行?
前端
曲折9 小时前
Cesium-气象要素PNG色斑图叠加
前端·cesium
Forever7_9 小时前
Electron 淘汰!新的桌面端框架 更强大、更轻量化
前端·vue.js
不会敲代码19 小时前
前端组件化样式隔离实战:React CSS Modules、styled-components 与 Vue scoped 对比
css·vue.js·react.js
Angelial9 小时前
Vue3 嵌套路由 KeepAlive:动态缓存与反向配置方案
前端·vue.js