国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

vue實(shí)現(xiàn)div可拖動(dòng)位置也可改變盒子大小的原理

瀏覽:61日期:2022-11-22 09:14:59

以下是效果圖:實(shí)現(xiàn)了div盒子在固定區(qū)域的拖動(dòng),也可改變盒子的高度和寬度,當(dāng)超出邊距后無法繼續(xù)改變大小

vue實(shí)現(xiàn)div可拖動(dòng)位置也可改變盒子大小的原理

這里說一下大致原理:拖動(dòng)和改變大小是分開來操作的,接下來分別說一下

盒子拖動(dòng)

這里用到了js的三個(gè)鼠標(biāo)事件,分別是onmousedown(鼠標(biāo)按下)、onmousemove(鼠標(biāo)移動(dòng))以及onmouseup(鼠標(biāo)松開),大致流程就是鼠標(biāo)按下拖動(dòng)圖標(biāo)進(jìn)行拖動(dòng)時(shí),動(dòng)態(tài)獲取當(dāng)前div的left和top再重新賦值給當(dāng)前div的top、left值,當(dāng)鼠標(biāo)松開再清除事件,至于固定在某個(gè)區(qū)域內(nèi)拖動(dòng),在賦值的時(shí)候判斷當(dāng)前top及l(fā)eft值是否超過限制區(qū)域的值,如果超過給最大值最小值

盒子改變大小

這里用到的也是盒子拖動(dòng)的三個(gè)事件,當(dāng)鼠標(biāo)移入盒子左邊框觸發(fā)mousemove事件,動(dòng)態(tài)計(jì)算盒子寬度重新賦值,鼠標(biāo)松開注銷mousrmove事件,我將寬度和高度改變分別封裝了組件,用的時(shí)候直接調(diào)用就好

博主用的vue寫的,這里展示的也是銅鼓vue書寫的,其他都是大同小異,知道原理就好

// index.vue<template> <!-- demo --> <div id='maxBoxId'> <div : : > <div class='drag-content'> <div class='content-text'> <!-- 拖拽圖標(biāo) --> <div class='drag-icon'> <i @mousedown.stop='dragDiv($event)' @mouseup.stop='dragUp($event)' ></i> </div> {{ moveInfo.text }} </div> <!-- 寬度改變組件 --> <ChangeWidth :moveId='moveInfo.moveId' index='0' @widthChange='changeWidth' @clearEvent='clearEvent' /> <!-- 高度改變組件 --> <ChangeHeight :moveId='moveInfo.moveId' index='1' @heightChange='heightChange' @clearEvent='clearEvent' /> </div> </div> </div></template> <script>import ChangeWidth from ’../component/ChangeWidth’import ChangeHeight from ’../component/ChangeHeight’export default { components: { ChangeWidth, ChangeHeight }, name: ’demo’, data() { return { moveInfo: { dragId: ’smallDragBoxId’, moveId: ’smallMoveBoxId’, text: ’我是拖動(dòng)的小盒子’, width: 400, height: 100, // 上邊距和左邊距 coordinate: { x: 180, y: 10 } } } }, methods: { // 區(qū)塊拖動(dòng) dragDiv(el, index) { // dragId: 可拖動(dòng)區(qū)域唯一標(biāo)識(shí) // moveId: 改變寬度組件唯一標(biāo)識(shí) const { dragId, coordinate } = this.moveInfo let obig = document.getElementById(’maxBoxId’) let osmall = document.getElementById(dragId) // 用于保存小的div拖拽前的坐標(biāo) osmall.startX = el.clientX - osmall.offsetLeft osmall.startY = el.clientY - osmall.offsetTop document.onmousemove = e => { let left, top left = e.clientX - osmall.startX top = e.clientY - osmall.startY osmall.style.left = left + ’px’ osmall.style.top = top + ’px’ coordinate.x = left coordinate.y = top if (left <= 0) { osmall.style.left = 0 + ’px’ coordinate.x = 0 } if (top <= 0) { osmall.style.top = 0 + ’px’ coordinate.y = 0 } if (left >= obig.offsetWidth - osmall.offsetWidth) { osmall.style.left = obig.offsetWidth - osmall.offsetWidth + ’px’ coordinate.x = obig.offsetWidth - osmall.offsetWidth } if (top >= obig.offsetHeight - osmall.offsetHeight) { osmall.style.top = obig.offsetHeight - osmall.offsetHeight + ’px’ coordinate.y = obig.offsetHeight - osmall.offsetHeight } } }, dragUp(el) { document.onmousemove = null document.onmouseup = null // 調(diào)用接口保存數(shù)據(jù) }, // 改變drag寬度尺寸 changeWidth(params) { const { index, width } = params let left const { dragId } = this.moveInfo // let obig = document.getElementById(’maxBoxId’) let osmall = document.getElementById(dragId) let boxWidth = document.getElementById(’maxBoxId’).offsetWidth left = osmall.style.left const newWidth = this.moveInfo.width + width // outWidth拖動(dòng)寬度時(shí)超出box的寬度 const outWidth = Number(left.slice(0, left.length - 2)) + Number(newWidth) - Number(boxWidth) // 如果超出box將截取留下的 if (outWidth >= 0) { this.moveInfo.width = Number(boxWidth) - Number(left.slice(0, left.length - 2)) } else { this.moveInfo.width = newWidth } // 設(shè)置div的最小寬度和最大寬度 if (this.moveInfo.width < 200) { this.moveInfo.width = 200 } if (this.moveInfo.width > 900) { this.moveInfo.width = 900 } }, // 改變drag高度 heightChange(params) { const { index, height } = params let top let osmall = document.getElementById(this.moveInfo.dragId) let boxHeight = document.getElementById(’maxBoxId’).offsetHeight top = osmall.style.top const newHeight = this.moveInfo.height + height // outHeight拖動(dòng)寬度時(shí)超出box的高度 const outHeight = Number(top.slice(0, top.length - 2)) + Number(newHeight) - Number(boxHeight) // 如果超出box將截取留下的 if (outHeight >= 0) { this.moveInfo.height = Number(boxHeight) - Number(top.slice(0, top.length - 2)) } else { this.moveInfo.height = newHeight } // 設(shè)置div的最小寬度和最大寬度 if (this.moveInfo.height < 100) { this.moveInfo.height = 100 } if (this.moveInfo.height > 200) { this.moveInfo.height = 200 } }, // 清除鼠標(biāo)事件 clearEvent() { document.onmousemove = null document.onmouseup = null } }}</script><style lang='scss' scoped>.demo { position: relative; width: 100%; z-index: 10; width: 1200px; background: red; height: 300px; margin-bottom: 1000px; margin-left: 100px; .drag-class { background: rgba(255, 255, 255, 0); position: absolute; .drag-content { position: relative; height: 100%; .content-text { border: 1px dashed #ffffff; font-size: 34px; color: #ffffff; margin-top: 5px; position: relative; height: 100%; .drag-icon { position: absolute; right: 10px; top: 5px; float: left; // margin-right: 10px; .down-dragger { cursor: move; font-size: 30px; color: #dbdce0; color: #ffffff; } } } } }}</style>

以下是改變大小的組件

<template> <!-- 拖動(dòng)右邊距改變div寬度 --> <div : @mousedown='mouseDown'></div></template> <script>export default { name: ’ChangeWidth’, props: [’index’, ’moveId’], data() { return { lastX: ’’ } }, created() { document.addEventListener(’mouseup’, this.mouseUp) }, destroyed() { document.removeEventListener(’mouseup’, this.mouseUp) }, methods: { mouseDown(event) { document.addEventListener(’mousemove’, this.mouseMove) this.lastX = event.screenX }, mouseMove(e) { this.$emit(’widthChange’, { width: e.screenX - this.lastX, index: this.index }) this.lastX = e.screenX }, mouseUp() { this.lastX = ’’ document.removeEventListener(’mousemove’, this.mouseMove) this.$emit(’clearEvent’) } }}</script><style lang='less' scoped>.x-handle { width: 5px; cursor: e-resize; background: #2866f0; height: 30px; position: absolute; right: 0; top: 40%;}</style>

改變高度的組件原理和寬度一樣,避免代碼重復(fù)就不上傳了

上面就是大致流程和源碼。

總結(jié)

到此這篇關(guān)于vue實(shí)現(xiàn)div可拖動(dòng)位置也可改變盒子大小的文章就介紹到這了,更多相關(guān)vue 實(shí)現(xiàn)div拖動(dòng)位置內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 美女综合网 | 德国女人一级毛片免费 | 男人天堂中文字幕 | 中文字幕1区 | 中国精品自拍 | a级毛片免费高清毛片视频 a级毛片免费高清视频 | 一级看片免费视频囗交 | 九色国产在线 | www国产 | 国产3级在线观看 | 欧美成人专区 | 男人的天堂久久香蕉国产 | av狼论坛 | 福利社在线视频 | 在线视频精品一区 | 免费播放欧美毛片欧美a | 欧美一区二区三区高清视频 | 亚洲成年www | 国产在线精品一区免费香蕉 | 一区二区不卡在线 | 亚洲成a人片在线观看中文 亚洲成a人片在线观看中文!!! | 欧美成人a视频 | 欧美亚洲一级片 | 亚洲精品国产第一区二区三区 | 亚洲视频在线一区二区 | 综合中文字幕 | 色婷婷色综合激情国产日韩 | 成年人一级片 | 特黄的欧美毛片 | 国产福利在线91 | 国产美女作爱 | 国产亚洲精品一区二区久久 | 特级aaa片毛片免费观看 | 久久精品国产99国产精品免费看 | 一级做a爰片久久毛片看看 一级做a爰片久久毛片鸭王 | 久久久久欧美情爱精品 | 亚洲专区一 | 最新日韩欧美不卡一二三区 | 亚洲欧美激情精品一区二区 | 欧美午夜在线观看理论片 | 午夜三级毛片 |