@@ -2,26 +2,32 @@ export default{
2
2
bind ( el , binding ) {
3
3
const dialogHeaderEl = el . querySelector ( '.el-dialog__header' )
4
4
const dragDom = el . querySelector ( '.el-dialog' )
5
- dialogHeaderEl . style = 'cursor:move;'
6
-
5
+ dialogHeaderEl . style . cssText + = '; cursor:move;'
6
+ dragDom . style . cssText += ';top:0px;'
7
7
// 获取原有属性 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
+ }
9
15
10
16
dialogHeaderEl . onmousedown = ( e ) => {
11
17
// 鼠标按下,计算当前元素距离可视区的距离
12
18
const disX = e . clientX - dialogHeaderEl . offsetLeft
13
19
const disY = e . clientY - dialogHeaderEl . offsetTop
14
20
15
21
// 获取到的值带px 正则匹配替换
16
- let styL , styT
22
+ let styL = getStyle ( dragDom , 'left' )
23
+ let styT = getStyle ( dragDom , 'top' )
17
24
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 )
22
28
} 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, '' )
25
31
}
26
32
27
33
document . onmousemove = function ( e ) {
@@ -30,8 +36,7 @@ export default{
30
36
const t = e . clientY - disY
31
37
32
38
// 移动当前元素
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;`
35
40
36
41
// 将此时的位置传出去
37
42
// binding.value({x:e.pageX,y:e.pageY})
0 commit comments