萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

JavaScript 實現遮罩層與 Loading 加載提示組件

核心功能

  • 半透明遮罩層鎖定頁面交互
  • 居中顯示加載動畫與提示文字
  • 支援多次調用、巢狀調用計數
  • IE6 select 穿透問題修復

現代模組化實現


class LoadingMask {
    constructor() {
        this.count = 0;
        this.mask = null;
        this.tip = null;
        this.hiddenSelects = [];
    }
    
    show(message = '加載中...') {
        this.count++;
        if (this.count > 1) return; // 已顯示
        
        this._createMask();
        this._createTip(message);
        this._showMask();
        this._showTip();
        this._hideSelectsIE6();
    }
    
    hide() {
        this.count = Math.max(0, this.count - 1);
        if (this.count > 0) return; // 還有其他調用未關閉
        
        this._hideTip();
        this._hideMask();
        this._showSelectsIE6();
    }
    
    _createMask() {
        if (this.mask) return;
        this.mask = document.createElement('div');
        this.mask.id = 'global-loading-mask';
        Object.assign(this.mask.style, {
            position: 'fixed', top: 0, left: 0, width: '100%', height: '100%',
            background: 'rgba(0,0,0,0.5)', zIndex: 9998,
            display: 'none'
        });
        document.body.appendChild(this.mask);
    }
    
    _createTip(message) {
        if (this.tip) {
            this.tip.querySelector('.msg').textContent = message;
            return;
        }
        this.tip = document.createElement('div');
        this.tip.id = 'global-loading-tip';
        this.tip.innerHTML = `
            
${message} `; Object.assign(this.tip.style, { position: 'fixed', top: '50%', left: '50%', transform: 'translate(-50%, -50%)', padding: '20px 30px', background: 'rgba(0,0,0,0.8)', color: '#fff', borderRadius: '8px', zIndex: 9999, display: 'none', fontSize: '14px', textAlign: 'center', minWidth: '120px' }); // 簡單 CSS 動畫 const style = document.createElement('style'); style.textContent = ` #global-loading-tip .spinner { width: 24px; height: 24px; border: 3px solid #fff; border-top-color: transparent; border-radius: 50%; animation: spin 1s linear infinite; margin: 0 auto 10px; } @keyframes spin { to { transform: rotate(360deg); } } `; document.head.appendChild(style); document.body.appendChild(this.tip); } _showMask() { this.mask.style.display = 'block'; } _hideMask() { this.mask.style.display = 'none'; } _showTip() { this.tip.style.display = 'block'; } _hideTip() { this.tip.style.display = 'none'; } // IE6 select 穿透修復 _hideSelectsIE6() { if (!/MSIE 6/.test(navigator.userAgent)) return; this.hiddenSelects = Array.from(document.querySelectorAll('select')); this.hiddenSelects.forEach(s => s.style.visibility = 'hidden'); } _showSelectsIE6() { this.hiddenSelects.forEach(s => s.style.visibility = 'visible'); this.hiddenSelects = []; } } // 全局單例 window.Loading = new LoadingMask(); // 使用 Loading.show('數據加載中...'); // 異步操作完成後 Loading.hide();

jQuery 版本(舊項目)


var Loading = {
    _count: 0,
    show: function(msg) {
        this._count++;
        if (this._count > 1) return;
        $('body').append('');
        $('body').append('');
        $('#mask, #tip').show();
    },
    hide: function() {
        this._count = Math.max(0, this._count - 1);
        if (this._count > 0) return;
        $('#tip, #mask').hide();
    }
};

React/Vue 組件化思路


// Vue 3 組件示例
// LoadingMask.vue



最佳實踐清單

  1. 使用 fixed 定位確保全屏覆蓋
  2. 遮罩 z-index 低於提示框,高於業務內容
  3. 計數器機制支援巢狀調用
  4. 動畫使用 CSS transition/animation 而非 JS
  5. 提供 Promise 介面配合 async/await
  6. 無障礙:role="alertdialog"、aria-live="polite"
copyright © 萬盛學電腦網 all rights reserved