之前有发过python版的满屏爱心,python是以小窗口的形式弹出,需要手动关闭;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>
.popup {
position: fixed;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
font-family: "楷体", "KaiTi", serif;
font-size: 18px;
text-align: center;
z-index: 1000;
animation: fadeIn 0.5s ease-out, fadeOut 0.5s ease-in 4.5s forwards;
max-width: 220px;
word-wrap: break-word;
opacity: 0;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
@keyframes fadeOut {
from { opacity: 1; transform: translateY(0); }
to { opacity: 0; transform: translateY(-10px); }
}
body {
margin: 0;
padding: 0;
min-height: 100vh;
background-color: #ffe6f2;
}
</style>
</head>
<body>
<script>
const bgColors = ['pink', 'lightblue', 'lightgreen', 'lightyellow', 'lightpink', 'cyan', 'lavender'];
const messages = [
'岁岁无忧!',
'生辰喜乐!',
'顺遂长安!',
'岁岁平安!',
'永远被爱包围!',
'永远的幸福!',
'愿你平安健康!',
'愿你事事顺心!',
'愿你永远保持心里的光!'
];
function getRandomPosition() {
const width = window.innerWidth;
const height = window.innerHeight;
const x = Math.floor(Math.random() * (width - 240));
const y = Math.floor(Math.random() * (height - 120));
return { x, y };
}
function createPopup(msg) {
const popup = document.createElement('div');
popup.className = 'popup';
popup.textContent = msg;
popup.style.backgroundColor = bgColors[Math.floor(Math.random() * bgColors.length)];
const { x, y } = getRandomPosition();
popup.style.left = `${x}px`;
popup.style.top = `${y}px`;
document.body.appendChild(popup);
setTimeout(() => {
popup.remove();
}, 5000);
}
function createPopupsBatch(count = 500, delay = 50) {
let index = 0;
const interval = setInterval(() => {
if (index >= count) {
clearInterval(interval);
return;
}
const randomMsg = messages[Math.floor(Math.random() * messages.length)];
createPopup(randomMsg);
index++;
}, delay);
}
window.addEventListener('load', () => {
createPopupsBatch(500, 50);
});
</script>
</body>
</html>