精美的CSS消息通知组件特效

代码如下:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS消息通知组件</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        
        .container {
            width: 100%;
            max-width: 800px;
            margin: 0 auto;
        }
        
        h1 {
            color: #333;
            margin-bottom: 30px;
            text-align: center;
            font-weight: 600;
        }
        
        .notification-container {
            position: fixed;
            top: 20px;
            right: 20px;
            width: 320px;
            z-index: 1000;
        }
        
        .notification {
            position: relative;
            padding: 15px 20px;
            margin-bottom: 15px;
            border-radius: 8px;
            color: white;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            transform: translateX(100%);
            opacity: 0;
            animation: slideIn 0.5s forwards, fadeOut 0.5s forwards 4.5s;
            display: flex;
            align-items: center;
            overflow: hidden;
        }
        
        .notification::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 4px;
            background: rgba(255, 255, 255, 0.5);
            animation: progress 5s linear forwards;
        }
        
        .notification i {
            margin-right: 15px;
            font-size: 24px;
        }
        
        .notification-content {
            flex: 1;
        }
        
        .notification-title {
            font-weight: 600;
            margin-bottom: 5px;
        }
        
        .notification-message {
            font-size: 14px;
            opacity: 0.9;
        }
        
        .success {
            background: linear-gradient(135deg, #4CAF50, #2E7D32);
        }
        
        .error {
            background: linear-gradient(135deg, #F44336, #C62828);
        }
        
        .warning {
            background: linear-gradient(135deg, #FF9800, #EF6C00);
        }
        
        .info {
            background: linear-gradient(135deg, #2196F3, #1565C0);
        }
        
        @keyframes slideIn {
            to {
                transform: translateX(0);
                opacity: 1;
            }
        }
        
        @keyframes fadeOut {
            to {
                transform: translateX(100%);
                opacity: 0;
            }
        }
        
        @keyframes progress {
            from {
                width: 100%;
            }
            to {
                width: 0%;
            }
        }
        
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            color: white;
            font-weight: 600;
            cursor: pointer;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
            margin: 10px 5px;
        }
        
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
        }
        
        .btn:active {
            transform: translateY(0);
        }
        
        .footer {
            margin-top: 50px;
            text-align: center;
            color: #666;
            font-size: 14px;
        }
        
        .footer a {
            color: #6a11cb;
            text-decoration: none;
            font-weight: 600;
        }
        
        .footer a:hover {
            text-decoration: underline;
        }
    </style>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
</head>
<body>
    <div class="container">
        <h1>CSS消息通知组件</h1>
        
        <div class="btn-container">
            <button class="btn" onclick="showNotification('success')">成功通知</button>
            <button class="btn" onclick="showNotification('error')">错误通知</button>
            <button class="btn" onclick="showNotification('warning')">警告通知</button>
            <button class="btn" onclick="showNotification('info')">信息通知</button>
        </div>
    </div>
    
    <div class="notification-container" id="notificationContainer"></div>
    
    <div class="footer">

    </div>

    <script>
        function showNotification(type) {
            const container = document.getElementById('notificationContainer');
            const notification = document.createElement('div');
            notification.className = `notification ${type}`;
            
            let icon, title, message;
            
            switch(type) {
                case 'success':
                    icon = '<i class="fas fa-check-circle"></i>';
                    title = '操作成功';
                    message = '您的操作已成功完成!';
                    break;
                case 'error':
                    icon = '<i class="fas fa-times-circle"></i>';
                    title = '发生错误';
                    message = '操作过程中出现了问题,请重试!';
                    break;
                case 'warning':
                    icon = '<i class="fas fa-exclamation-triangle"></i>';
                    title = '警告提示';
                    message = '此操作可能会影响系统性能!';
                    break;
                case 'info':
                    icon = '<i class="fas fa-info-circle"></i>';
                    title = '系统消息';
                    message = '您有3条未读消息,请及时查看。';
                    break;
            }
            
            notification.innerHTML = `
                ${icon}
                <div class="notification-content">
                    <div class="notification-title">${title}</div>
                    <div class="notification-message">${message}</div>
                </div>
            `;
            
            container.appendChild(notification);
            
            // 自动移除通知
            setTimeout(() => {
                notification.remove();
            }, 5000);
        }
        
        // 页面加载时显示一个欢迎通知
        window.onload = function() {
            setTimeout(() => {
                showNotification('info');
            }, 1000);
        };
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值