Skip to content

Commit afd255d

Browse files
committed
fix[el-dargDialog]: fixed drag bug in IE
1 parent b7c8079 commit afd255d

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/directive/el-dragDialog/drag.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,32 @@ export default{
22
bind(el, binding) {
33
const dialogHeaderEl = el.querySelector('.el-dialog__header')
44
const dragDom = el.querySelector('.el-dialog')
5-
dialogHeaderEl.style = 'cursor:move;'
6-
5+
dialogHeaderEl.style.cssText += ';cursor:move;'
6+
dragDom.style.cssText += ';top:0px;'
77
// 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
8-
const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null)
8+
function getStyle(dom, attr) {
9+
if (dom.currentStyle) {
10+
return dom.currentStyle[attr]
11+
} else {
12+
return getComputedStyle(dom, false)[attr]
13+
}
14+
}
915

1016
dialogHeaderEl.onmousedown = (e) => {
1117
// 鼠标按下,计算当前元素距离可视区的距离
1218
const disX = e.clientX - dialogHeaderEl.offsetLeft
1319
const disY = e.clientY - dialogHeaderEl.offsetTop
1420

1521
// 获取到的值带px 正则匹配替换
16-
let styL, styT
22+
let styL = getStyle(dragDom, 'left')
23+
let styT = getStyle(dragDom, 'top')
1724

18-
// 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
19-
if (sty.left.includes('%')) {
20-
styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100)
21-
styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100)
25+
if (styL.includes('%')) {
26+
styL = +document.body.clientWidth * (+styL.replace(/\%/g, '') / 100)
27+
styT = +document.body.clientHeight * (+styT.replace(/\%/g, '') / 100)
2228
} else {
23-
styL = +sty.left.replace(/\px/g, '')
24-
styT = +sty.top.replace(/\px/g, '')
29+
styL = +styL.replace(/\px/g, '')
30+
styT = +styT.replace(/\px/g, '')
2531
}
2632

2733
document.onmousemove = function(e) {
@@ -30,8 +36,7 @@ export default{
3036
const t = e.clientY - disY
3137

3238
// 移动当前元素
33-
dragDom.style.left = `${l + styL}px`
34-
dragDom.style.top = `${t + styT}px`
39+
dragDom.style.cssText += `;left:${l + styL}px;top:${t + styT}px;`
3540

3641
// 将此时的位置传出去
3742
// binding.value({x:e.pageX,y:e.pageY})

0 commit comments

Comments
 (0)