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>
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。