您好,登錄后才能下訂單哦!
怎么在vue中利用自定義指令實(shí)現(xiàn)一個(gè)拖拽功能?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
1.vue自定義指令
Vue.directive('dragx', (el, binding, vnode) => { // 默認(rèn)參數(shù) let defaultOpts = { dragDirection: 'n, e, s, w, ne, se, sw, nw, all', dragContainerId: '', // dragBarClass: '', // 類選擇器 canDrag: true, canResize: true, multiSelect: false } let isMove = false let isResize = false let constraintDom let constraintRect let constraintRectHeight let constraintRectWidth binding.value = binding.value || {} let cfg = Object.assign({}, defaultOpts, binding.value) if (cfg.dragContainerId) { constraintDom = document.querySelector('#' + cfg.dragContainerId) constraintRect = constraintDom.getBoundingClientRect() constraintRectHeight = constraintRect.height constraintRectWidth = constraintRect.width } let getStyleNumValue = (style, key) => parseInt(style.getPropertyValue(key), 10) // 設(shè)置約束范圍 function setConstraint (data) { if (cfg.dragContainerId) { if (data.left <= 0) data.left = 0 if (data.top <= 0) data.top = 0 if (data.top + data.height + data.borderTop + data.borderBottom >= constraintRectHeight) data.top = constraintRectHeight - data.height - data.borderTop - data.borderBottom if (data.left + data.width + data.borderLeft + data.borderRight > constraintRectWidth) data.left = constraintRectWidth - data.width - data.borderLeft - data.borderRight } } el.onmousemove = e => { if (cfg.dragBarClass.length > 0 && e.target.classList.contains(cfg.dragBarClass) && cfg.canDrag) { el.style.cursor = 'move' return } el.style.cursor = '' } el.onmouseleave = e => { el.style.cursor = '' } el.onmousedown = e => { let clickId = (e.target || e.srcElement).id let posData = { x: e.pageX, y: e.pageY } el.onmouseup = e => { // dom元素選中時(shí)發(fā)送active事件 if (!cfg.active) el.dispatchEvent(new CustomEvent('bindActive', { 'detail': { ...posData } })) } isMove = false if (cfg.dragBarClass.length > 0 && !e.target.classList.contains('icon-icon-resize')) { isMove = true isResize = false document.body.style.cursor = 'move' } else if (e.target.classList.contains('icon-icon-resize')) { isMove = false isResize = true } let style let rect let data let tableStyle style = window.getComputedStyle(el) rect = el.getBoundingClientRect() data = { width: getStyleNumValue(style, 'width'), height: getStyleNumValue(style, 'height'), left: getStyleNumValue(style, 'left'), top: getStyleNumValue(style, 'top'), borderLeft: getStyleNumValue(style, 'border-left-width'), borderTop: getStyleNumValue(style, 'border-top-width'), borderRight: getStyleNumValue(style, 'border-right-width'), borderBottom: getStyleNumValue(style, 'border-bottom-width'), deltX: e.pageX - rect.left, deltY: e.pageY - rect.top, startX: rect.left, startY: rect.top } if (el.id.indexOf(THSIGN) > -1 || el.id.indexOf(TDSIGN) > -1) { let table = document.getElementById(el.id.split(SEPARATOR)[0]) tableStyle = window.getComputedStyle(table) data.left = getStyleNumValue(tableStyle, 'left') data.top = getStyleNumValue(tableStyle, 'top') } document.onmousemove = edom => { if (edom.ctrlKey) return if (isResize) { data.width = Math.round(edom.pageX - data.startX + data.borderLeft + data.borderRight) data.height = Math.round(edom.pageY - data.startY + data.borderBottom + data.borderTop) } // 處理組件 移動(dòng) if (isMove && cfg.canDrag && (cfg.active || cfg.multiSelect)) { let targetPageX = edom.pageX let targetPageY = edom.pageY let deltX = targetPageX - data.startX - data.deltX let deltY = targetPageY - data.startY - data.deltY let newLeft = Math.round(getStyleNumValue(style, 'left') || '0', 10) + deltX let newTop = Math.round(getStyleNumValue(style, 'top') || '0', 10) + deltY data.left = Math.round(newLeft) data.top = Math.round(newTop) data.startX = data.startX + deltX data.startY = data.startY + deltY setConstraint(data) if (el.id.indexOf(THSIGN) > -1 || el.id.indexOf(TDSIGN) > -1) { let newLeft = Math.round(getStyleNumValue(tableStyle, 'left') || '0', 10) + deltX let newTop = Math.round(getStyleNumValue(tableStyle, 'top') || '0', 10) + deltY data.left = Math.round(newLeft) data.top = Math.round(newTop) } } if (cfg.multiSelect) { let domData = { el: clickId, x: edom.pageX, y: edom.pageY } // 移動(dòng)多個(gè)元素時(shí)發(fā)送事件 bindUpdateDoms el.dispatchEvent(new CustomEvent('bindUpdateDoms', { detail: domData })) } else { // 移動(dòng)單個(gè)元素移動(dòng)時(shí)發(fā)送事件 bindUpdate el.dispatchEvent(new CustomEvent('bindUpdate', { detail: data })) } document.onmouseup = edom => { if (cfg.multiSelect && posData.x != edom.pageX && posData.y != edom.pageY) { let domData = { x: edom.pageX, y: edom.pageY } // 發(fā)送下面2個(gè)事件主要是為了實(shí)現(xiàn)操作的撤銷和恢復(fù)功能,瀏覽器保存一個(gè)執(zhí)行棧,一個(gè)移動(dòng)表示一個(gè)事件 // 多個(gè)元素同時(shí)移動(dòng)時(shí)發(fā)送事件 bindFinishMoveDoms el.dispatchEvent(new CustomEvent('bindFinishMoveDoms', { detail: domData })) } else if (posData.x != edom.pageX && posData.y != edom.pageY) { // 單個(gè)元素同時(shí)移動(dòng)時(shí)發(fā)送事件 bindFinishMove el.dispatchEvent(new CustomEvent('bindFinishMove', { detail: data })) } document.body.style.cursor = '' document.onmousemove = null document.onmouseup = null isMove = false } } document.onmouseup = edom => { document.body.style.cursor = '' document.onmousemove = null document.onmouseup = null isMove = false } } })
2.自定義事件
在vue組件使用自定義指令并添加事件監(jiān)聽(tīng)器
如果指令需要多個(gè)值,可以傳入一個(gè) JavaScript 對(duì)象字面量。記住,指令函數(shù)能夠接受所有合法的 JavaScript 表達(dá)式。
<div v-dragx="{ dragBarClass: 'drag', dragContainerId: 'page', multiSelect, active }" @bindUpdate="bindUpdate" @bindActive="activeDom" @bindUpdateDoms="bindUpdateDoms" @bindFinishMove="pushStackByDom" @bindFinishMoveDoms="pushStackByDoms" : :id="uId" class="drag-wrap"> </div>
// 添加一個(gè)適當(dāng)?shù)氖录O(jiān)聽(tīng)器 <div @bindUpdate="bindUpdate" /> // 創(chuàng)建并分發(fā)事件 el.dispatchEvent(new CustomEvent('bindUpdate', { detail: data }))
字段說(shuō)明
<template> <div v-dragx="{ dragBarClass: 'drag', dragContainerId: 'page', multiSelect, active }" @bindUpdate="bindUpdate" @bindActive="activeDom" @bindUpdateDoms="bindUpdateDoms" @bindFinishMove="pushStackByDom" @bindFinishMoveDoms="pushStackByDoms" : :id="uId" class="drag-wrap"> <slot></slot> <span class="iconfont icon-icon-resize"></span> </div> </template> <script> export default { data () { return { prevSize: {} } }, props: { item: { type: Object, default: () => ({}) }, isVariable: { type: Boolean, default: false }, active: { type: Boolean, default: false }, size: { type: Object, default: () => ({ x: 200, y: 200, w: 200, h: 24 }) }, multiSelect: { type: Boolean, default: false }, uId: { type: String, required: true }, dragContainerId: { type: String } }, computed: { style: function () { return { top: this.size.y + 'px', left: this.size.x + 'px', width: this.size.w + 'px', height: this.size.h + 'px' } } }, methods: { activeDom (e) { this.$emit('activated', this.uId, e.detail.x, e.detail.y) this.prevSize = { ...this.size } }, bindUpdate (event) { let data = event.detail this.size.x = data.left this.size.y = data.top this.$store.dispatch('RESIZE_ID', { id: this.uId, ...data }) this.$emit('resizing', { ...data }) }, bindUpdateDoms (e) { this.$emit('movingDoms', e.detail) }, pushStackByDom (e) { this.$store.dispatch('RESIZE_ID', { prevSize: this.prevSize, id: this.uId, ...event.detail }) }, pushStackByDoms (e) { this.$emit('finishMoveDoms', e.detail) } } } </script> <style scoped> .drag { background-color: #ccc; border-top: solid 1px rgba(33, 61, 223, 0.541); border-bottom: solid 1px rgba(33, 61, 223, 0.541); } .drag-wrap { position: absolute; } .icon-icon-resize { position: absolute; right: 0; bottom: 0; line-height: 12px; cursor: se-resize; font-size: 12px; color: #666; } </style>
3.vuex實(shí)現(xiàn)狀態(tài)管理
因?yàn)樯婕暗蕉鄠€(gè)組件的通信,因此使用vuex,把模板信息和執(zhí)行棧全部保存在vuex中
最終實(shí)現(xiàn)的效果如下圖所示
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。