好看的404界面并且5秒后跳转指定界面

2025-12-26 744 0

这个404页面具有以下特点

  1. 美观的视觉效果
    • 渐变背景和毛玻璃效果
    • 星空背景动画
    • 平滑的过渡动画
  2. 倒计时功能
    • 5秒倒计时显示
    • 进度条可视化
    • 自动跳转到首页
  3. 交互功能
    • 立即跳转按钮
    • 悬停效果
    • 平滑的动画过渡
  4. 响应式设计
    • 适配不同屏幕尺寸
    • 居中布局

页面会在5秒后自动跳转到根目录(/),您可以通过修改JavaScript中的window.location.href = '/'来更改跳转目标。

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>404 - 页面未找到</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Arial', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
        }
        
        .container {
            text-align: center;
            max-width: 600px;
            padding: 2rem;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 20px;
            backdrop-filter: blur(10px);
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
        }
        
        .error-code {
            font-size: 6rem;
            font-weight: bold;
            margin-bottom: 1rem;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
        }
        
        .error-message {
            font-size: 1.5rem;
            margin-bottom: 1rem;
            opacity: 0.9;
        }
        
        .error-description {
            font-size: 1rem;
            margin-bottom: 2rem;
            opacity: 0.8;
            line-height: 1.6;
        }
        
        .countdown {
            font-size: 1.2rem;
            margin-bottom: 1.5rem;
            opacity: 0.9;
        }
        
        .redirect-btn {
            background: rgba(255, 255, 255, 0.2);
            color: white;
            border: 2px solid rgba(255, 255, 255, 0.3);
            padding: 12px 24px;
            border-radius: 25px;
            cursor: pointer;
            font-size: 1rem;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-block;
        }
        
        .redirect-btn:hover {
            background: rgba(255, 255, 255, 0.3);
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
        }
        
        .stars {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            pointer-events: none;
            z-index: -1;
        }
        
        .star {
            position: absolute;
            background: white;
            border-radius: 50%;
            animation: twinkle var(--duration, 3s) infinite ease-in-out;
        }
        
        @keyframes twinkle {
            0%, 100% { opacity: 0.2; }
            50% { opacity: 1; }
        }
        
        .progress-bar {
            width: 100%;
            height: 4px;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 2px;
            margin: 1rem 0;
            overflow: hidden;
        }
        
        .progress-fill {
            height: 100%;
            background: linear-gradient(90deg, #ff6b6b, #4ecdc4);
            width: 0%;
            transition: width 1s linear;
            border-radius: 2px;
        }
    </style>
</head>
<body>
    <div class="stars" id="stars"></div>
    
    <div class="container">
        <div class="error-code">404</div>
        <h1 class="error-message">页面未找到</h1>
        <p class="error-description">抱歉,您访问的页面不存在或已被移除。请检查网址是否正确,或稍后重试。</p>
        
        <div class="countdown" id="countdown">
            页面将在 <span id="seconds">5</span> 秒后自动跳转到首页
        </div>
        
        <div class="progress-bar">
            <div class="progress-fill" id="progressFill"></div>
        </div>
        
        <a href="/" class="redirect-btn" id="redirectBtn">立即跳转到首页</a>
    </div>

    <script>
        // 创建星星背景
        function createStars() {
            const starsContainer = document.getElementById('stars');
            const starCount = 100;
            
            for (let i = 0; i < starCount; i++) {
                const star = document.createElement('div');
                star.classList.add('star');
                
                // 随机位置
                const x = Math.random() * 100;
                const y = Math.random() * 100;
                const size = Math.random() * 2 + 1;
                const duration = Math.random() * 4 + 2;
                
                star.style.left = `${x}%`;
                star.style.top = `${y}%`;
                star.style.width = `${size}px`;
                star.style.height = `${size}px`;
                star.style.setProperty('--duration', `${duration}s`);
                
                starsContainer.appendChild(star);
            }
        }
        
        // 倒计时和跳转功能
        function initCountdown() {
            let seconds = 5;
            const countdownElement = document.getElementById('seconds');
            const progressFill = document.getElementById('progressFill');
            const redirectBtn = document.getElementById('redirectBtn');
            
            // 每秒更新倒计时
            const timer = setInterval(() => {
                seconds--;
                countdownElement.textContent = seconds;
                
                // 更新进度条
                const progress = ((5 - seconds) / 5) * 100;
                progressFill.style.width = `${progress}%`;
                
                if (seconds <= 0) {
                    clearInterval(timer);
                    window.location.href = '/'; // 跳转到首页
                }
            }, 1000);
            
            // 立即跳转按钮事件
            redirectBtn.addEventListener('click', (e) => {
                e.preventDefault();
                window.location.href = '/';
            });
        }
        
        // 初始化页面
        document.addEventListener('DOMContentLoaded', () => {
            createStars();
            initCountdown();
        });
    </script>
</body>
</html>

相关文章

PNG/JPG在线转换WebP:原理、实现与前端源码详解
实现智能深色/浅色模式(Dark Mode)的终极指南:自动适配系统偏好 + 手动切换 + 本地持久化
一行命令搭建临时文件服务器:5 种语言实现的本地文件共享方案(Python/Node.js/Go/Rust/PHP)
使用 Python 快速搭建一个本地 Markdown 博客生成器
开源挂机页:毫秒级北京时间 + 动态星空 + 情绪字幕
现代 Web 安全中常被忽视但至关重要的主题:前端如何安全处理敏感数据

发布评论