日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

vue實現div可拖動位置也可改變盒子大小的原理

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

以下是效果圖:實現了div盒子在固定區域的拖動,也可改變盒子的高度和寬度,當超出邊距后無法繼續改變大小

vue實現div可拖動位置也可改變盒子大小的原理

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

盒子拖動

這里用到了js的三個鼠標事件,分別是onmousedown(鼠標按下)、onmousemove(鼠標移動)以及onmouseup(鼠標松開),大致流程就是鼠標按下拖動圖標進行拖動時,動態獲取當前div的left和top再重新賦值給當前div的top、left值,當鼠標松開再清除事件,至于固定在某個區域內拖動,在賦值的時候判斷當前top及left值是否超過限制區域的值,如果超過給最大值最小值

盒子改變大小

這里用到的也是盒子拖動的三個事件,當鼠標移入盒子左邊框觸發mousemove事件,動態計算盒子寬度重新賦值,鼠標松開注銷mousrmove事件,我將寬度和高度改變分別封裝了組件,用的時候直接調用就好

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

// index.vue<template> <!-- demo --> <div id='maxBoxId'> <div : : > <div class='drag-content'> <div class='content-text'> <!-- 拖拽圖標 --> <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: ’我是拖動的小盒子’, width: 400, height: 100, // 上邊距和左邊距 coordinate: { x: 180, y: 10 } } } }, methods: { // 區塊拖動 dragDiv(el, index) { // dragId: 可拖動區域唯一標識 // moveId: 改變寬度組件唯一標識 const { dragId, coordinate } = this.moveInfo let obig = document.getElementById(’maxBoxId’) let osmall = document.getElementById(dragId) // 用于保存小的div拖拽前的坐標 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 // 調用接口保存數據 }, // 改變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拖動寬度時超出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 } // 設置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拖動寬度時超出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 } // 設置div的最小寬度和最大寬度 if (this.moveInfo.height < 100) { this.moveInfo.height = 100 } if (this.moveInfo.height > 200) { this.moveInfo.height = 200 } }, // 清除鼠標事件 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> <!-- 拖動右邊距改變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>

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

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

總結

到此這篇關于vue實現div可拖動位置也可改變盒子大小的文章就介紹到這了,更多相關vue 實現div拖動位置內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Vue
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
1024精品一区二区三区| 亚洲欧美在线专区| 视频一区中文| 在线看片日韩| 国产a亚洲精品| 欧美日韩一区二区综合| 黄色亚洲大片免费在线观看| 国产毛片久久久| 国产精品99免费看| 久久大逼视频| 久久毛片亚洲| 日韩中文字幕一区二区高清99| 日韩视频一二区| 黄色成人在线网址| 欧美日一区二区在线观看| 好看的亚洲午夜视频在线| 蜜桃av一区| 国产精品色在线网站| 在线一区电影| 国产精品白丝久久av网站| 亚洲电影在线| 午夜亚洲福利| 日韩精品午夜| 精品视频高潮| 欧美私人啪啪vps| 色在线中文字幕| 国产精品videosex极品| 久久久夜精品| 国产欧美日韩免费观看| 亚洲免费影视| 肉色欧美久久久久久久免费看| 日韩av一区二| 免费国产自线拍一欧美视频| 国产精品免费精品自在线观看| 亚洲成av人片一区二区密柚| 国产精品一区二区中文字幕| 视频在线观看91| 日韩视频中文| 精品日韩毛片| 久久精品一区| 国产黄色精品| 美美哒免费高清在线观看视频一区二区| 卡一卡二国产精品| 91亚洲精品视频在线观看| 99日韩精品| 久久久精品久久久久久96| 欧美激情三区| 亚洲18在线| 久久国产99| 男人天堂欧美日韩| 黄色亚洲大片免费在线观看| 香蕉视频亚洲一级| 久久国产免费| 国产精久久久| 国产精品115| 欧美日韩伊人| 国产精品一卡| 国产亚洲精品精品国产亚洲综合| 玖玖玖国产精品| 视频一区国产视频| 美女久久一区| 亚洲精品日本| 亚洲小说春色综合另类电影| 亚洲一区二区小说| 亚洲精品欧美| 日韩专区视频网站| 日韩不卡一区二区| 国产麻豆一区二区三区精品视频| 日韩高清成人在线| 国产精品久久777777毛茸茸| 久久精品97| 国产精品尤物| 精品国产亚洲日本| 在线看片福利| 99久久亚洲精品| 亚洲欧洲一区二区天堂久久| 国产一区二区高清| 亚洲一区欧美激情| 人人爽香蕉精品| 日韩三级一区| 欧美黄色一区二区| 欧美aa一级| 国产农村妇女精品一二区 | 日韩一区三区| 亚洲午夜av| 亚洲午夜免费| 麻豆国产欧美一区二区三区 | 国产精品美女| 日本成人手机在线| 美女av一区| 久久高清免费| 深夜福利一区| 精品国产精品久久一区免费式| 日韩1区在线| 少妇精品导航| 在线视频观看日韩| 亚洲精品动态| 国产一区一一区高清不卡| 久久精品官网| 日韩三级视频| 高清不卡亚洲| 亚洲最新av| 国产成人精品福利| 99香蕉国产精品偷在线观看| 国产色99精品9i| 久久精品不卡| 国产精品网在线观看| 在线日韩视频| 久久精品72免费观看| 韩日一区二区三区| 国产精品视频一区二区三区| 欧美日一区二区| 国产日韩中文在线中文字幕 | 亚洲丝袜美腿一区| 久久久久久亚洲精品美女| av高清一区| 日韩精彩视频在线观看| 日韩一区二区在线免费| 日本久久二区| 99热国内精品| 美女视频黄久久| 亚洲欧美日韩视频二区| 国产一区二区亚洲| 日本一区福利在线| 国产尤物精品| 狂野欧美性猛交xxxx| 蜜桃视频第一区免费观看| 亚洲涩涩在线| 欧美精品1区| 免费看日韩精品| 免费看av不卡| 国产欧美啪啪| 中文字幕一区二区三区日韩精品| 国产一区二区三区黄网站 | 日韩电影免费在线观看| 日韩不卡在线观看日韩不卡视频| 色婷婷精品视频| 黄色精品视频| 欧美中文高清| 亚州av日韩av| 久久亚洲电影| 亚洲福利精品| 秋霞影院一区二区三区| 美女高潮久久久| 日韩中文av| 在线视频亚洲| 91久久午夜| 亚洲激情欧美| 久久男女视频| 97精品97| 亚洲一区资源| av资源新版天堂在线| 精品三区视频| 精品国产黄a∨片高清在线| 久久av综合| 美女性感视频久久| 精品成av人一区二区三区| 国产精品宾馆| 麻豆视频一区| 国产精品.xx视频.xxtv| 国产精品视频一区二区三区| 欧美精品观看| 国产精品theporn| 精品五月天堂| 精品视频在线你懂得| 精品黄色一级片| 日韩1区2区| 久久国产欧美| 欧美日韩国产免费观看| 国产免费成人| 亚洲精品黄色| 国产精品一区亚洲| 精品一区91| 美女尤物久久精品| 国产精品久久久久久模特 | 偷拍欧美精品| 亚洲一区观看| 少妇精品久久久一区二区| 91久久精品无嫩草影院| 国产欧美一区二区精品久久久 | 正在播放日韩精品| 成人免费网站www网站高清| 国产专区一区| 中文无码日韩欧| 国产三级一区| 在线精品亚洲欧美日韩国产| 亚洲二区精品| 亚洲精品精选| 久久久精品国产**网站| av综合电影网站| 亚洲综合精品四区| 久久久久国产| 欧美日韩伊人| 婷婷综合电影| 一区二区三区国产在线| 自由日本语亚洲人高潮| 亚洲久久视频| 久久九九精品| 日本不卡在线视频| 日韩av在线播放网址|