解析vue界面中可拖动<div>、<img>

本文详细介绍了如何在网页中实现div和img元素的拖动功能,包括onmousedown、onmousemove和onmouseup事件的使用,以及如何获取和更新元素在页面上的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在界面中拖拽某个元素,用户经历的过程是:点击,拖动,松开,即onmousedown、onmousemove、onmouseup。

div的拖动

若要拖动div,首先应在div上添加绑定事件@mousedown=“mousedown”,这样当点击div时可触发相应函数。

 <div
   width="150px" height="150px"
     @mousedown="mousedown" 
     :style="'position:fixed;left:'+left+'px;top:'+top+'px;'"/>
 </div>

在mousedown函数中首先要继续监听当前标签的onmousemove事件,当用户点击并移动时触发。此时去监控鼠标的点击位置,获取鼠标点击的窗口坐标clientX,clientY与鼠标在标签内的坐标offsetX,offsetY,通过相减即可得到标签在浏览器中的坐标(clientX-offsetX,clientY-offsetY)。并把这个坐标动态赋值给div,即实现了拖动div。最后当鼠标抬起时onmouseup销毁onmousemove事件。

public left:number=0;
public top:number=0;
 public  mousedown(e:any){
      document.onmousemove = (e2) => {
        this.left=e2.clientX-e.offsetX;
        this.top=e2.clientY-e.offsetY;
      };
      document.onmouseup = ()=> {
          document.onmousemove=null;
          document.onmousedown=null;
      };
  }

img的拖动

img标签的拖动与div的拖动思路是一样的,只是作为img标签在拖动时需要禁用浏览器默认事件。在onmousedown中引用下方函数即可

 public pauseEvent(e:any){
    if(e.stopPropagation) e.stopPropagation();//阻止把事件分派到其他节点。
    if(e.preventDefault) e.preventDefault();//阻止默认事件
    e.cancelBubble=true;//阻止冒泡
	e.returnValue=false;//阻止IE的默认事件
	return false;
}
<template> <div class="transfer-container"> <!-- 左侧表格 --> <div class="table-container"> <div class="header-box"> <span>设备列表</span> <span class="checkAll" @click="checkAll">全选</span> </div> <div class="select-box"> <div class="select-item"> <span>{{ $t('主机型号:') }}</span> <a-select v-model="hostModel" @change="changeSelect" :placeholder="$t('请选择主机型号')" allowClear> <a-select-option v-for="item in periodList" :key="item.id" :value="item.itemDesc" :title="item.itemName"> {{ item.itemName }} </a-select-option> </a-select> </div> <div class="select-item"> <span>{{ $t('组织名称:') }}</span> <org-tree-select @treeSelect="orgTreeSelect"></org-tree-select> </div> </div> <!-- 左侧列表 --> <div class="list-container"> <div v-for="item in leftData" :key="item.personId" class="list-item"> <a-checkbox v-model="item.checked" :disabled="item.deviceOnOffLine == 0 || item.configStatus == 1" class="checkbox" ></a-checkbox> <div class="device-box"> <div class="top-info"> <span>{{ item.deviceName }}</span> <span v-show="item.configStatus == 1" @click="openTask(item)"> <hd-icon type="files" /> {{ $t('已配任务') }} </span> </div> <div class="bottom-info"> <span>{{ $t('探测器信息:') }}</span> <span>{{ item.detectorName }} </span> | <span>{{ item.productModel }} </span> | <span>{{ item.detectorUniqueCode }} </span> </div> </div> </div> <a-empty v-show="leftData.length === 0" /> </div> <div v-if="leftData.length" class="pagination"> <a-pagination simple :defaultCurrent="pagination.currentPage" :total="pagination.totalRows" @change="changePage" /> </div> </div> <!-- 中间操作按钮 --> <div class="transfer-actions"> <a-button type="primary" icon="right" :disabled="selectedLeft.length === 0" @click="moveToRight"> </a-button> </div> <!-- 右侧表格 --> <div class="table-container right-content"> <div class="header-box"> <span>{{ $t('已选择列表') + '(' + rightData.length + ')' }}</span> <a-button type="primary" :disabled="rightData.length === 0" @click="clearAll"> 清除 </a-button> </div> <div class="list-container"> <div v-for="item in rightData" :key="item.personId" class="list-item right-list"> <div class="device-box"> <div class="top-info"> <span>{{ item.deviceName }}</span> <div>{{ item.detectorOwnerName }}</div> <span v-show="item.configStatus == 1" @click="openTask(item)"> <hd-icon type="files" /> {{ $t('已配任务') }} </span> </div> <div class="bottom-info"> <span>{{ $t('探测器信息:') }}</span> <span>{{ item.detectorName }} </span> | <span>{{ item.productModel }} </span> | <span>{{ item.detectorUniqueCode }} </span> </div> </div> <img src="@/assets/img/icon-remove.png" @click="moveToLeft(item)" alt="" /> </div> <a-empty v-show="rightData.length === 0" /> </div> </div> <!-- 定时任务已存在 --> <myModal :params="{ title: $t('已配置任务列表'), showFooter: false }" :visible="visible" @cancel="visible = false" destroyOnClose > <template v-slot:header><span></span></template> <template v-slot:body> <div class="table_title"> {{ $t('探测名称') }} </div> <a-table :columns="detailColumns" :scroll="{ x: '100%', y: 400 }" :dataSource="detailData" row-key="id" :pagination="false" > </a-table> </template> </myModal> </div> </template> <script> import factory from '../factory' import myModal from '@/components/scfComponents/modalComponents/modal.vue' import orgTreeSelect from '@/components/orgTreeSelect/orgTreeSelect' export default { components: { myModal, orgTreeSelect }, data() { return { visible: false, // 弹窗 periodList: [], // 主机型号列表 orgData: {}, // 组织树数据 leftData: [], // 左侧表格数据 rightData: [], // 右侧表格数据 pagination: { pageSize: 10, currentPage: 1, totalRows: 0, }, // 分页 // 已配任务列表表头 detailColumns: [ { title: this.$t('日期'), dataIndex: 'date', ellipsis: true, }, { title: this.$t('时间'), dataIndex: 'executeTime', ellipsis: true, }, { title: this.$t('空开操作'), dataIndex: 'operation', ellipsis: true, }, ], // 已配任务列表数据 detailData: [], } }, computed: { // 选中左侧列表 selectedLeft() { return this.leftData.filter(item => item.checked) }, }, mounted() { this.getDic() // 初始化数据 this.leftData = [] this.rightData = [] }, methods: { // 主机型号列表 getDic() { factory.getDic(1080).then(res => { this.periodList = res }) }, // 获取左侧表格数据 getLeftTableData() { let params = { page: this.pagination.currentPage, orgCodes: [this.orgData.orgCode], productModel: this.hostModel, } factory.getBatchDetector(params).then(res => { if (res.success) { this.leftData = res.data.map(item => ({ ...item, checked: false })) || [] // 如果右侧存在数据需要把数据过滤一下 if (this.rightData.length > 0) { let ids = this.rightData.map(item => item.id) this.leftData = this.leftData.filter(item => !ids.includes(item.id)) } } }) }, // 查询已存在空开定时任务 getDetectorJob(record) { factory.getDetectorJob(record.jobId).then(res => { if (res.success) { this.detailData = res.data.map(item => { item.detectorName = record.detectorName || '' item.weekList = this.getActiveWeeks(item.rept) return item }) || [] } }) }, // 主机型号 changeSelect(value) { if (value && Object.keys(this.orgData).length > 0) { this.getLeftTableData() } else { this.leftData = [] } }, getActiveWeeks(rept) { const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'] const pattern = rept.split('') return pattern .map((char, index) => (char == '1' ? weekDays[index] : null)) .filter(Boolean) .join('、') }, // 组织树节点选择 orgTreeSelect(node) { this.orgData = node if (this.hostModel && Object.keys(this.orgData).length > 0) { this.getLeftTableData() } else { this.leftData = [] } }, // 全选 checkAll() { if (this.leftData.length === 0) return this.leftData.forEach(item => { if (item.deviceOnOffLine == 1 && item.configStatus == 0) { item.checked = true } }) }, // 清除 clearAll() { const newLeft = [...this.leftData] this.rightData.forEach(rightItem => { if (!newLeft.some(leftItem => leftItem.id == rightItem.id)) { newLeft.push({ ...rightItem, checked: false }) } }) this.leftData = newLeft this.rightData = [] }, // 向右移动选中项 moveToRight() { this.rightData = [...this.rightData, ...this.selectedLeft] this.leftData = this.leftData.filter(item => !this.selectedLeft.some(i => i.id === item.id)) }, // 向左移动单个项 moveToLeft(item) { this.rightData = this.rightData.filter(i => i.id !== item.id) this.leftData = [...this.leftData, { ...item, checked: false }] }, // 打开已配置任务列表 openTask(record) { this.visible = true this.getDetectorJob(record) }, }, } </script> <style lang="less" scoped> .transfer-container { width: 100%; display: flex; justify-content: space-around; box-sizing: border-box; } .table-container { width: 48%; height: 100%; position: relative; font-family: '微软雅黑'; font-weight: 400; border: 1px solid #ebedef; .header-box { height: 46px; width: 100%; display: flex; align-items: center; padding: 0 16px; border-bottom: 1px solid rgb(229, 231, 234); background: rgb(242, 243, 244); justify-content: space-between; color: rgb(21, 23, 26); font-size: 14px; font-weight: 700; .checkAll { cursor: pointer; color: rgb(41, 141, 255); } } .select-box { display: flex; height: 64px; padding: 0 20px; align-items: center; .select-item { margin-right: 24px; color: rgb(157, 166, 177); > span:first-child { margin-right: 5px; } } } .list-container { padding: 0 20px; height: 392px; overflow-y: scroll; .list-item { height: 74px; display: flex; align-items: center; .device-box { margin-left: 16px; .top-info { display: flex; align-items: center; span { cursor: pointer; color: #298dff; font-size: 14px; } div { height: 24px; border-radius: 2px; font-size: 12px; display: flex; cursor: default; margin-right: 24px; align-items: center; padding: 0 8px; color: rgb(66, 75, 86); background: rgb(236, 238, 240); } span:first-child { color: rgb(21, 23, 26); font-size: 16px; cursor: default; margin-right: 24px; } } .bottom-info { color: rgb(105, 118, 136); font-size: 14px; > span { margin: 0 5px; } } } } } .pagination { display: flex; height: 58px; align-items: center; justify-content: flex-end; } } .right-content { .device-box { margin: 0; } .list-container { height: 514px; .list-item { justify-content: space-between; .device-box { margin: 0; } img { width: 20px; height: 20px; display: inline-block; } } } } .transfer-actions { margin-top: 56px; display: flex; flex-direction: column; justify-content: center; } </style> 代码评审
最新发布
08-02
<template> <div> <el-dialog title="OTA批量升级" :visible.sync="dialogVisible" width="600px" custom-class="ota-dialog" :close-on-click-modal="false" > <div class="dialog-content"> <!-- 操作区域 --> <div class="action-section"> <div class="upload-section"> <el-upload action="/api/upload" :on-success="handleUploadSuccess" :before-upload="beforeUpload" :limit="1" :on-exceed="handleExceed" :file-list="fileList" :class="{ 'has-file': fileList.length }" drag > <i class="el-icon-upload"></i> <div class="el-upload__text"> <div>点击或拖拽文件到此处上传</div> <div class="el-upload__tip">支持.xlsx、.xls格式文件</div> </div> </el-upload> <!-- 文件信息卡片 --> <div v-if="fileList.length" class="file-card"> <div class="file-info"> <i class="el-icon-document"></i> <div class="file-details"> <div class="file-name">{{ fileList[0].name }}</div> <div class="file-size">{{ formatFileSize(fileList[0].size) }}</div> </div> </div> <el-button type="danger" icon="el-icon-delete" circle @click="fileList = []" ></el-button> </div> </div> </div> </div> <!-- 底部按钮 --> <div slot="footer" class="dialog-footer"> <el-button @click="dialogVisible = false">取 消</el-button> <el-button type="primary" @click="otaBatchUpgradeCinfirm()" :disabled="!fileList.length" > 确定 </el-button> </div> </el-dialog> </div> </template> <script> export default { data() { return { dialogVisible: false, fileList: [] }; }, methods: { init() { this.dialogVisible = true; this.fileList = []; }, otaBatchUpgradeCinfirm() { // 升级逻辑 }, handleUploadSuccess(response, file, fileList) { this.$ message.success('文件上传成功'); this.fileList = fileList; }, beforeUpload(file) { const isValidType = [ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel' ].includes(file.type); if (!isValidType) { this.$ message.error('请上传Excel格式的文件 (.xlsx 或 .xls)'); } return isValidType; }, handleExceed() { this.$ message.warning('每次只能上传一个文件'); }, formatFileSize(size) { if (size < 1024) return size + ' B'; if (size < 1024 * 1024) return (size / 1024).toFixed(1) + ' KB'; return (size / (1024 * 1024)).toFixed(1) + ' MB'; } } }; </script> <style scoped> /* 操作区域 */ .action-section { display: flex; flex-direction: column; gap: 20px; } .upload-section { position: relative; display: flex; justify-content: center; } /* 文件卡片 */ .file-card { margin-top: 15px; padding: 15px; border-radius: 8px; background-color: #f5f7fa; display: flex; align-items: center; justify-content: space-between; border: 1px solid #ebeef5; } .file-info { display: flex; align-items: center; gap: 12px; } .file-info i { font-size: 28px; color: #409EFF; } .file-details { line-height: 1.5; } .file-name { font-weight: 500; max-width: 300px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .file-size { font-size: 12px; color: #909399; } /* 底部按钮 */ .dialog-footer { display: flex; justify-content: flex-end; padding-top: 15px; border-top: 1px solid #ebeef5; } /* 上传区域激活状态 */ .has-file >>> .el-upload-dragger { border: 1px dashed #67C23A; background-color: rgba(103, 194, 58, 0.05); } </style> 添加上传文件列表和文件预览功能,要保证美观和交互完美,不加其他新的功能
07-12
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值