目录

前端蒙层中间挖空效果

Admin11/12/2025456 阅读

前端蒙层中间挖空效果实现

我将创建一个带有中间挖空效果的蒙层,这种效果常用于引导用户注意力或突出显示特定区域。

设计思路

  • 创建一个覆盖全屏的半透明蒙层
  • 使用CSS的clip-pathmask属性在中间创建透明区域
  • 添加交互功能,如点击蒙层关闭

实现代码

html 复制代码
<!DOCTYPE html>
<html lang=\"zh-CN\">
<head>
    <meta charset=\"UTF-8\">
    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
    <title>蒙层中间挖空效果</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            color: #333;
            line-height: 1.6;
            padding: 20px;
            min-height: 100vh;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        
        header {
            text-align: center;
            margin-bottom: 40px;
            color: white;
        }
        
        h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
        }
        
        .subtitle {
            font-size: 1.2rem;
            opacity: 0.9;
        }
        
        .content {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
            margin-bottom: 40px;
        }
        
        .card {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease;
        }
        
        .card:hover {
            transform: translateY(-5px);
        }
        
        .card h3 {
            margin-bottom: 15px;
            color: #2575fc;
        }
        
        .demo-area {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            justify-content: center;
            margin-top: 30px;
        }
        
        .btn {
            background: #2575fc;
            color: white;
            border: none;
            padding: 12px 25px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 1rem;
            transition: background 0.3s ease;
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.2);
        }
        
        .btn:hover {
            background: #1a68e8;
        }
        
        .btn-secondary {
            background: #6a11cb;
        }
        
        .btn-secondary:hover {
            background: #5a0db3;
        }
        
        /* 蒙层样式 */
        .overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.7);
            display: none;
            z-index: 1000;
        }
        
        /* 方法1: 使用clip-path */
        .overlay-clip {
            clip-path: polygon(
                0% 0%, 
                0% 100%, 
                25% 100%, 
                25% 25%, 
                75% 25%, 
                75% 75%, 
                25% 75%, 
                25% 100%, 
                100% 100%, 
                100% 0%
            );
        }
        
        /* 方法2: 使用mask */
        .overlay-mask {
            -webkit-mask: radial-gradient(circle at center, transparent 100px, black 100px);
            mask: radial-gradient(circle at center, transparent 100px, black 100px);
        }
        
        /* 方法3: 使用多个div组合 */
        .overlay-complex {
            background: none;
        }
        
        .overlay-complex::before,
        .overlay-complex::after {
            content: '';
            position: absolute;
            background: rgba(0, 0, 0, 0.7);
        }
        
        .overlay-complex::before {
            top: 0;
            left: 0;
            width: 100%;
            height: 40%;
        }
        
        .overlay-complex::after {
            bottom: 0;
            left: 0;
            width: 100%;
            height: 40%;
        }
        
        .overlay-left, .overlay-right {
            position: absolute;
            background: rgba(0, 0, 0, 0.7);
            top: 40%;
            height: 20%;
        }
        
        .overlay-left {
            left: 0;
            width: 40%;
        }
        
        .overlay-right {
            right: 0;
            width: 40%;
        }
        
        .highlight-box {
            position: absolute;
            top: 40%;
            left: 40%;
            width: 20%;
            height: 20%;
            border: 2px dashed #ffcc00;
            border-radius: 5px;
            box-shadow: 0 0 0 9999px rgba(255, 204, 0, 0.2);
        }
        
        .overlay-content {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: white;
            padding: 20px;
            border-radius: 10px;
            text-align: center;
            max-width: 400px;
            width: 90%;
            z-index: 1001;
        }
        
        .close-btn {
            position: absolute;
            top: 10px;
            right: 15px;
            background: none;
            border: none;
            font-size: 1.5rem;
            cursor: pointer;
            color: #666;
        }
        
        footer {
            text-align: center;
            color: white;
            margin-top: 40px;
            padding-top: 20px;
            border-top: 1px solid rgba(255, 255, 255, 0.2);
        }
    </style>
</head>
<body>
    <div class=\"container\">
        <header>
            <h1>前端蒙层中间挖空效果</h1>
            <p class=\"subtitle\">多种方法实现蒙层中间挖空,突出显示特定内容区域</p>
        </header>
        
        <div class=\"content\">
            <div class=\"card\">
                <h3>方法一:使用 clip-path</h3>
                <p>通过 CSS clip-path 属性裁剪出中间透明区域,实现挖空效果。</p>
                <p>优点:代码简洁,性能较好。</p>
                <p>缺点:形状固定,不够灵活。</p>
                <div class=\"demo-area\">
                    <button class=\"btn\" id=\"showClipOverlay\">演示效果</button>
                </div>
            </div>
            
            <div class=\"card\">
                <h3>方法二:使用 mask</h3>
                <p>使用 CSS mask 属性创建径向渐变,中间透明,四周不透明。</p>
                <p>优点:可以实现圆形挖空,代码简洁。</p>
                <p>缺点:兼容性稍差。</p>
                <div class=\"demo-area\">
                    <button class=\"btn\" id=\"showMaskOverlay\">演示效果</button>
                </div>
            </div>
            
            <div class=\"card\">
                <h3>方法三:使用多个元素组合</h3>
                <p>通过多个 div 元素组合,模拟中间挖空效果。</p>
                <p>优点:兼容性最好,形状灵活。</p>
                <p>缺点:代码较多,结构复杂。</p>
                <div class=\"demo-area\">
                    <button class=\"btn btn-secondary\" id=\"showComplexOverlay\">演示效果</button>
                </div>
            </div>
        </div>
        
    </div>
    
    <!-- 方法1: clip-path 蒙层 -->
    <div class=\"overlay overlay-clip\" id=\"clipOverlay\">
        <div class=\"overlay-content\">
            <button class=\"close-btn\">&times;</button>
            <h3>clip-path 挖空效果</h3>
            <p>这种方法使用 CSS clip-path 属性裁剪出中间的矩形区域。</p>
            <p>点击蒙层或关闭按钮可以关闭此提示。</p>
        </div>
    </div>
    
    <!-- 方法2: mask 蒙层 -->
    <div class=\"overlay overlay-mask\" id=\"maskOverlay\">
        <div class=\"overlay-content\">
            <button class=\"close-btn\">&times;</button>
            <h3>mask 挖空效果</h3>
            <p>这种方法使用 CSS mask 属性创建径向渐变,实现圆形挖空。</p>
            <p>点击蒙层或关闭按钮可以关闭此提示。</p>
        </div>
    </div>
    
    <!-- 方法3: 多个元素组合蒙层 -->
    <div class=\"overlay overlay-complex\" id=\"complexOverlay\">
        <div class=\"overlay-left\"></div>
        <div class=\"overlay-right\"></div>
        <div class=\"highlight-box\"></div>
        <div class=\"overlay-content\">
            <button class=\"close-btn\">&times;</button>
            <h3>多个元素组合挖空效果</h3>
            <p>这种方法使用多个 div 元素组合,模拟中间挖空效果。</p>
            <p>点击蒙层或关闭按钮可以关闭此提示。</p>
        </div>
    </div>
    
    <script>
        // 获取元素
        const clipOverlay = document.getElementById('clipOverlay');
        const maskOverlay = document.getElementById('maskOverlay');
        const complexOverlay = document.getElementById('complexOverlay');
        
        const showClipBtn = document.getElementById('showClipOverlay');
        const showMaskBtn = document.getElementById('showMaskOverlay');
        const showComplexBtn = document.getElementById('showComplexOverlay');
        
        const closeButtons = document.querySelectorAll('.close-btn');
        
        // 显示蒙层函数
        function showOverlay(overlay) {
            overlay.style.display = 'block';
        }
        
        // 隐藏蒙层函数
        function hideOverlay(overlay) {
            overlay.style.display = 'none';
        }
        
        // 绑定事件
        showClipBtn.addEventListener('click', () => showOverlay(clipOverlay));
        showMaskBtn.addEventListener('click', () => showOverlay(maskOverlay));
        showComplexBtn.addEventListener('click', () => showOverlay(complexOverlay));
        
        // 点击蒙层关闭
        clipOverlay.addEventListener('click', (e) => {
            if (e.target === clipOverlay) hideOverlay(clipOverlay);
        });
        
        maskOverlay.addEventListener('click', (e) => {
            if (e.target === maskOverlay) hideOverlay(maskOverlay);
        });
        
        complexOverlay.addEventListener('click', (e) => {
            if (e.target === complexOverlay) hideOverlay(complexOverlay);
        });
        
        // 点击关闭按钮关闭
        closeButtons.forEach(btn => {
            btn.addEventListener('click', () => {
                hideOverlay(clipOverlay);
                hideOverlay(maskOverlay);
                hideOverlay(complexOverlay);
            });
        });
    </script>
</body>
</html>

功能说明

这个页面展示了三种实现蒙层中间挖空的方法:

  1. 使用 clip-path:通过CSS的clip-path属性裁剪出中间透明区域
  2. 使用 mask:通过CSS的mask属性创建径向渐变实现圆形挖空
  3. 使用多个元素组合:通过多个div元素组合模拟中间挖空效果

每种方法都有其优缺点:

  • clip-path:代码简洁但形状固定
  • mask:可以实现圆形挖空但兼容性稍差
  • 多个元素组合:兼容性最好但代码较多

用户可以通过点击页面上的按钮来查看每种方法的效果,点击蒙层或关闭按钮可以关闭提示。

这个设计采用了现代化UI,包含卡片布局、渐变背景和交互动画,提供了良好的用户体验。

评论

共 0 条
后参与评论
共 104 篇文章 感谢支持

RSS ·关于

© 2026 我的博客