代码如下:
<!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>
502

被折叠的 条评论
为什么被折叠?



