吉安感知网项目-前端
罗广辉
2026-01-26 beb95fb5fc166804056abafd70fc01ac27de7621
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 失去焦点的时候不触发其他点击事件
 const isolate = {
    mounted(el) {
        let isHandlingBlur = false;
        const overlayClass = 'isolate-blur-overlay';
 
        //
        const inputEl = el.querySelectorAll('input')[0]
 
        const createOverlay = () => {
            const existing = document.querySelector(`.${overlayClass}`);
            if (existing) return;
 
            const overlay = document.createElement('div');
            overlay.className = overlayClass;
            overlay.style.cssText = `
        position: fixed;
        top: 0;
        left: 0;
        width: 100vw;
        height: 100vh;
        z-index: 9999;
        opacity: 0;
      `;
 
            // 阻止所有交互
            overlay.addEventListener('mousedown', stopEvent, true);
            overlay.addEventListener('touchstart', stopEvent, true);
            overlay.addEventListener('click', stopEvent, true);
 
            document.body.appendChild(overlay);
        };
 
        const removeOverlay = () => {
            const overlays = document.querySelectorAll(`.${overlayClass}`);
            overlays.forEach(ol => ol.remove());
        };
 
        function stopEvent(e) {
            e.stopPropagation();
            e.preventDefault();
            e.stopImmediatePropagation();
        }
 
        inputEl.addEventListener('focus', () => {
            isHandlingBlur = true;
        });
 
        inputEl.addEventListener('blur', () => {
            isHandlingBlur = true;
            createOverlay();
 
 
            setTimeout(() => {
                removeOverlay();
                isHandlingBlur = false;
            }, 500);
        });
 
        // 阻止input自身事件冒泡
        inputEl.addEventListener('click', (e) => {
            if (isHandlingBlur) {
                stopEvent(e);
            }
        });
    }
};
 
export default isolate;