Skip to content
This repository was archived by the owner on Dec 2, 2025. It is now read-only.

Commit 7a3e219

Browse files
committed
当处在resizing/moving状态的时候,吞掉click事件
1 parent 5312baf commit 7a3e219

File tree

6 files changed

+57
-10
lines changed

6 files changed

+57
-10
lines changed

src/config/constant.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export default {
2-
BORDER_SCOPE: 10,
2+
BORDER_SCOPE: 10, // resize区域的宽度
3+
AVAILABLE_CLICK_MAX_MOVE_DISTANCE: 4, // 在move中,超出这个距离的话将把click事件吞掉
4+
AVAILABLE_CLICK_MAX_RESIZE_DISTANCE: 4, // 在resize中,超出这个距离的话将把click事件吞掉
35
};

src/libs/common.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,9 @@ export function recoverIframe(window) {
237237
iframe.style['pointer-events'] = 'auto';
238238
});
239239
}
240+
/* 计算两点间距离 */
241+
export function calDistance({ x1, y1, x2, y2 }) {
242+
const result = Math.pow(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2), 0.5);
243+
// console.log({ x1, y1, x2, y2 }, result);
244+
return result;
245+
}

src/libs/event-binding.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,20 @@ export function eventBinding(el, customParams) {
8989
if (finalParams.resizable) {
9090
el.addEventListener(startEvent, handleStartEventForResize.bind(instance));
9191
el.addEventListener(moveEvent, cursorChange.bind(instance));
92-
93-
/* 当处在resizing状态的时候,吞掉click事件 */
94-
el.addEventListener('click', event => {
95-
if (el.className.indexOf('resizing') > -1) {
96-
event.stopImmediatePropagation();
97-
}
98-
});
9992
}
10093

10194
/* 最大化相关 */
10295
if (maximizeHandler) {
10396
addMaximizeEvent.call(instance, maximizeHandler);
10497
}
98+
99+
/* 当处在resizing/moving状态的时候,吞掉click事件 */
100+
el.addEventListener('click', event => {
101+
if (
102+
el.className.indexOf('moving') > -1 ||
103+
el.className.indexOf('resizing') > -1
104+
) {
105+
event.stopImmediatePropagation();
106+
}
107+
});
105108
}

src/libs/move.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
judgeResizeType,
88
getSize,
99
setSize,
10+
calDistance,
1011
} from './common';
12+
import constant from '../config/constant';
1113

1214
export function handleStartEventForMove(event) {
1315
function _handleEndEventForMove(event) {
@@ -43,7 +45,15 @@ export function handleStartEventForMove(event) {
4345
window.style.right = 'auto'; // 必须设置为auto,否则就会把宽度撑起来
4446

4547
/* 设置moving状态,主要用于吞掉click事件 */
46-
if (window.className.indexOf('moving') === -1) {
48+
if (
49+
calDistance({
50+
x1: position.x,
51+
y1: position.y,
52+
x2: startPoint.x,
53+
y2: startPoint.y,
54+
}) > constant.AVAILABLE_CLICK_MAX_MOVE_DISTANCE &&
55+
window.className.indexOf('moving') === -1
56+
) {
4757
window.className += ' moving';
4858
}
4959
}

src/libs/resize.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import {
1111
isInMaximizeHandler,
1212
judgeResizeType,
1313
getStyle,
14+
calDistance,
1415
} from './common';
16+
import constant from '../config/constant';
1517

1618
function _isOnOtherHandler(el, { moveHandler, maximizeHandler }) {
1719
return el === moveHandler || el === maximizeHandler;
@@ -181,13 +183,31 @@ export function handleStartEventForResize(startEvent) {
181183
setSize(target, calWidth, calHeight);
182184
setPositionOffset(target, calLeft, calTop);
183185
moveEvent.stopPropagation();
186+
187+
/* 设置resizing状态,主要用于吞掉click事件 */
188+
if (
189+
calDistance({
190+
x1: nowPosition.x,
191+
y1: nowPosition.y,
192+
x2: startPoint.x,
193+
y2: startPoint.y,
194+
}) > constant.AVAILABLE_CLICK_MAX_RESIZE_DISTANCE &&
195+
target.className.indexOf('resizing') === -1
196+
) {
197+
target.className += ' resizing';
198+
}
184199
}
185200

186201
function _handleEndEventForResize(endEvent) {
187202
document.removeEventListener(moveEvent, _handleMoveEventForResize, false); // 拖拽结束,清除移动的事件回调
188203

189204
endEvent.preventDefault();
190205
endEvent.stopPropagation();
206+
207+
/* 撤销moving状态,但由于此状态值主要用于吞掉click事件,因此使用setTimeout延长moving状态至click事件结束 */
208+
setTimeout(() => {
209+
target.className = target.className.replace(/ ?resizing/, '');
210+
}, 0);
191211
}
192212

193213
const eventEl = startEvent.target;

test.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
</style>
4040
</head>
4141
<body>
42-
<div class="window window2 window--by-function" v-show="ifShowWindow">
42+
<div class="window window2 window--by-function">
4343
<div class="window__header">
4444
函数方式生成的窗口
4545
</div>
@@ -65,6 +65,12 @@
6565
}
6666

6767
enhanceWindow();
68+
69+
document
70+
.querySelector('.window--by-function')
71+
.addEventListener('click', () => {
72+
console.log('触发了click事件');
73+
});
6874
</script>
6975
</body>
7076
</html>

0 commit comments

Comments
 (0)