共计 3455 个字符,预计需要花费 9 分钟才能阅读完成。
1. 拖拽效果 1.1 拖拽前 1.2 拖拽后
2. 拖拽组件。把代码单独拷贝在一个 js 里即可,这里写在公共文件夹 common/js/directives.js 里, 具体看步骤 3 示例。参考 API https://cn.vuejs.org/v2/api/ 的 Vue.directive。
import Vue from ‘vue’;
// v-dialogDrag: 弹窗拖拽属性(重点!!!给模态框添加这个属性模态框就能拖拽了)
Vue.directive(‘dialogDrag’, { // 属性名称 dialogDrag,前面加 v - 使用
bind(el, binding, vnode, oldVnode) {
const dialogHeaderEl = el.querySelector(‘.el-dialog__header’);
const dragDom = el.querySelector(‘.el-dialog’);
//dialogHeaderEl.style.cursor = ‘move’;
dialogHeaderEl.style.cssText += ‘;cursor:move;’
dragDom.style.cssText += ‘;top:0px;’
// 获取原有属性 ie dom 元素.currentStyle 火狐谷歌 window.getComputedStyle(dom 元素, null);
const sty = (function() {
if (window.document.currentStyle) {
return (dom, attr) => dom.currentStyle[attr];
} else{
return (dom, attr) => getComputedStyle(dom, false)[attr];
}
})()
dialogHeaderEl.onmousedown = (e) => {
// 鼠标按下,计算当前元素距离可视区的距离
const disX = e.clientX – dialogHeaderEl.offsetLeft;
const disY = e.clientY – dialogHeaderEl.offsetTop;
const screenWidth = document.body.clientWidth; // body 当前宽度
const screenHeight = document.documentElement.clientHeight; // 可见区域高度 (应为 body 高度,可某些环境下无法获取)
const dragDomWidth = dragDom.offsetWidth; // 对话框宽度
const dragDomheight = dragDom.offsetHeight; // 对话框高度
const minDragDomLeft = dragDom.offsetLeft;
const maxDragDomLeft = screenWidth – dragDom.offsetLeft – dragDomWidth;
const minDragDomTop = dragDom.offsetTop;
const maxDragDomTop = screenHeight – dragDom.offsetTop – dragDomheight;
// 获取到的值带 px 正则匹配替换
let styL = sty(dragDom, ‘left’);
let styT = sty(dragDom, ‘top’);
// 注意在 ie 中 第一次获取到的值为组件自带 50% 移动之后赋值为 px
if(styL.includes(‘%’)) {
styL = +document.body.clientWidth * (+styL.replace(/\%/g, ”) / 100);
styT = +document.body.clientHeight * (+styT.replace(/\%/g, ”) / 100);
}else {
styL = +styL.replace(/\px/g, ”);
styT = +styT.replace(/\px/g, ”);
};
document.onmousemove = function (e) {
// 通过事件委托,计算移动的距离
let left = e.clientX – disX;
let top = e.clientY – disY;
// 边界处理
if (-(left) > minDragDomLeft) {
left = -(minDragDomLeft);
} else if (left > maxDragDomLeft) {
left = maxDragDomLeft;
}
if (-(top) > minDragDomTop) {
top = -(minDragDomTop);
} else if (top > maxDragDomTop) {
top = maxDragDomTop;
}
// 移动当前元素
dragDom.style.cssText += `;left:${left + styL}px;top:${top + styT}px;`;
};
document.onmouseup = function (e) {
document.onmousemove = null;
document.onmouseup = null;
};
}
}
})
3. 拖拽组件的引入,只要引入了 directives.js 即可
3.1 引入
import ‘../common/js/directives.js’
3.2 示例
4. 使用。给 el-dialog 添加属性 v -dialogDrag 即可代码:
<el-dialog
title=” 外币维护 ”
v-dialogDrag
size=’mini’
:close-on-click-modal=”false”
:visible.sync=”dialogVisible”
width=”400px”
@close=”close”>
<div class=”mini-font mb-5 balance”>
<el-form :model=”form”>
<el-form-item label=” 外币金额 ” :label-width=”formLabelWidth”>
<!– <el-input v-model=”form.fmMoney” :value=”form.fmMoney | formatMoney1″ auto-complete=”off” size=’mini’></el-input> –>
<el-input-number
size=’small’
:precision=”2″
:controls=”false”
v-model=”form.fmMoney”
auto-complete=”off”>
</el-input-number>
</el-form-item>
<el-form-item label=” 汇率 ” :label-width=”formLabelWidth”>
<!– <el-input v-model=”form.percent” auto-complete=”off” size=’mini’></el-input> –>
<el-input-number
size=’small’
:precision=”4″
:controls=”false”
v-model=”form.percent”
auto-complete=”off”>
</el-input-number>
</el-form-item>
<el-form-item label=” 本位币金额 ” :label-width=”formLabelWidth”>
<!– <el-input v-model=”form.balance” auto-complete=”off” size=’mini’></el-input> –>
<el-input-number
size=’small’
:precision=”2″
:controls=”false”
v-model=”form.balance”
auto-complete=”off”>
</el-input-number>
</el-form-item>
</el-form>
</div>
<span slot=”footer” class=”dialog-footer”>
<el-button type=”primary” size=’mini’ @click=”sureBtn”> 确定 </el-button>
<el-button type=”primary” size=’mini’ @click=”close”> 取消 </el-button>
</span>
</el-dialog>
参考:https://www.jianshu.com/p/c3c…