grok-3-preview-02-24· xAI LMArena Human Preference 140K
使用html做一个单文件博客网页,动画效果越多越好,功能越多越好
- User
使用html做一个单文件博客网页,动画效果越多越好,功能越多越好
- Assistant
好的!下面是一个功能丰富、动画效果多样的单文件博客网页示例。它包含以下功能和特性:
功能:
- 文章列表:展示博客文章卡片,支持分页。
- 文章详情:点击文章卡片弹出模态框查看全文。
- 搜索功能:支持按标题搜索文章。
- 分类筛选:按文章分类筛选。
- 评论系统:每篇文章支持添加评论(仅前端模拟,数据不持久化)。
- 夜间模式:切换明暗主题。
- 返回顶部按钮:页面滚动时显示。
- 响应式设计:适配手机、平板、桌面设备。
动画效果:
- 页面加载时的渐入动画。
- 文章卡片的悬停放大、阴影增强动画。
- 模态框的缩放弹出动画。
- 搜索框的展开动画。
- 分类筛选的滑动切换动画。
- 夜间模式切换的平滑过渡。
- 返回顶部按钮的淡入淡出动画。
以下是完整的单文件 HTML 代码,直接保存为
index.html即可运行:<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>炫酷博客 - 动画与功能齐飞</title> <style> * { box-sizing: border-box; } body { margin: 0; font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; background: #f0f2f5; color: #333; transition: background 0.5s, color 0.5s; } body.dark { background: #1a1a1a; color: #e0e0e0; } header { background: #007bff; color: white; padding: 20px; text-align: center; position: sticky; top: 0; z-index: 1000; animation: slideInDown 0.5s ease-out; } @keyframes slideInDown { from { transform: translateY(-100%); } to { transform: translateY(0); } } nav { display: flex; justify-content: center; gap: 20px; padding: 10px; background: #0056b3; animation: fadeIn 0.8s ease-out; } @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } nav a { color: white; text-decoration: none; font-weight: bold; padding: 8px 16px; border-radius: 4px; transition: background 0.3s, transform 0.3s; } nav a:hover { background: #003d80; transform: scale(1.1); } main { max-width: 1200px; margin: 20px auto; padding: 0 10px; animation: fadeInUp 0.8s ease-out; } @keyframes fadeInUp { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } } /* 搜索框 */ .search-bar { margin-bottom: 20px; text-align: center; } #searchInput { width: 0; padding: 8px; border: none; border-radius: 20px; transition: width 0.5s ease, padding 0.5s ease; background: white; box-shadow: 0 2px 6px rgba(0,0,0,0.1); } #searchInput.active { width: 300px; padding: 8px 16px; } body.dark #searchInput { background: #333; color: #e0e0e0; } /* 分类筛选 */ .categories { display: flex; gap: 10px; margin-bottom: 20px; overflow-x: auto; padding-bottom: 10px; } .category-btn { padding: 8px 16px; border: none; border-radius: 20px; background: #007bff; color: white; cursor: pointer; transition: transform 0.3s, background 0.3s; } .category-btn.active, .category-btn:hover { background: #0056b3; transform: scale(1.1); } body.dark .category-btn { background: #444; } body.dark .category-btn.active, body.dark .category-btn:hover { background: #666; } /* 文章网格 */ .article-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 20px; } .article-card { background: white; border-radius: 8px; overflow: hidden; box-shadow: 0 4px 8px rgba(0,0,0,0.1); transition: transform 0.3s, box-shadow 0.3s; animation: cardFadeIn 0.5s ease-out; } @keyframes cardFadeIn { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } } .article-card:hover { transform: translateY(-10px); box-shadow: 0 8px 16px rgba(0,0,0,0.2); } body.dark .article-card { background: #2a2a2a; } .article-thumb { width: 100%; height: 160px; background-size: cover; background-position: center; transition: transform 0.3s; } .article-card:hover .article-thumb { transform: scale(1.05); } .article-info { padding: 16px; } .article-title { font-size: 18px; font-weight: bold; margin: 0 0 8px; height: 2.4em; overflow: hidden; text-overflow: ellipsis; } .article-meta { font-size: 14px; color: #666; } body.dark .article-meta { color: #aaa; } /* 分页 */ .pagination { display: flex; justify-content: center; gap: 10px; margin-top: 20px; } .pagination button { padding: 8px 16px; border: none; border-radius: 4px; background: #007bff; color: white; cursor: pointer; transition: background 0.3s, transform 0.3s; } .pagination button:hover, .pagination button.active { background: #0056b3; transform: scale(1.1); } body.dark .pagination button { background: #444; } body.dark .pagination button:hover, body.dark .pagination button.active { background: #666; } /* 文章详情模态框 */ .modal { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.7); display: none; align-items: center; justify-content: center; z-index: 2000; } .modal.active { display: flex; } .modal-content { background: white; max-width: 800px; width: 90%; max-height: 80vh; overflow-y: auto; border-radius: 8px; padding: 20px; animation: modalPop 0.3s ease-out; } @keyframes modalPop { from { transform: scale(0.8); opacity: 0; } to { transform: scale(1); opacity: 1; } } body.dark .modal-content { background: #2a2a2a; } .modal-close { float: right; font-size: 24px; cursor: pointer; transition: transform 0.3s; } .modal-close:hover { transform: rotate(90deg); } .modal-title { font-size: 24px; font-weight: bold; margin-bottom: 16px; } .modal-body { line-height: 1.6; } /* 评论区 */ .comments { margin-top: 20px; } .comment-input { width: 100%; padding: 8px; margin-bottom: 10px; } .comment-btn { padding: 8px 16px; border: none; border-radius: 4px; background: #007bff; color: white; cursor: pointer; transition: background 0.3s, transform 0.3s; } .comment-btn:hover { background: #0056b3; transform: scale(1.1); } .comment-list { margin-top: 10px; } .comment-item { padding: 10px; border-bottom: 1px solid #eee; animation: fadeIn 0.5s ease-out; } body.dark .comment-item { border-color: #444; } /* 夜间模式切换 */ .theme-toggle { position: fixed; top: 20px; right: 20px; z-index: 1000; padding: 8px 16px; border: none; border-radius: 20px; background: #007bff; color: white; cursor: pointer; transition: background 0.3s, transform 0.3s; } .theme-toggle:hover { background: #0056b3; transform: scale(1.1); } /* 返回顶部按钮 */ .back-to-top { position: fixed; bottom: 20px; right: 20px; z-index: 1000; padding: 10px 16px; border: none; border-radius: 50%; background: #007bff; color: white; cursor: pointer; opacity: 0; transition: opacity 0.3s, transform 0.3s; } .back-to-top.visible { opacity: 1; } .back-to-top:hover { transform: scale(1.1); } @media (max-width: 600px) { nav { flex-wrap: wrap; gap: 10px; } .modal-content { width: 95%; } #searchInput.active { width: 90%; } } </style> </head> <body> <header>炫酷博客</header> <nav> <a href="#">首页</a> <a href="#">技术</a> <a href="#">生活</a> <a href="#">旅行</a> <a href="#">关于</a> </nav> <main> <div class="search-bar"> <input type="text" id="searchInput" placeholder="搜索文章..."> </div> <div class="categories" id="categoryFilter"> <button class="category-btn active" data-category="all">全部</button> <button class="category-btn" data-category="tech">技术</button> <button class="category-btn" data-category="life">生活</button> <button class="category-btn" data-category="travel">旅行</button> </div> <div class="article-grid" id="articleGrid"></div> <div class="pagination" id="pagination"></div> </main> <!-- 文章详情模态框 --> <div class="modal" id="articleModal"> <div class="modal-content"> <span class="modal-close" id="modalClose">×</span> <h2 class="modal-title" id="modalTitle"></h2> <div class="modal-body" id="modalBody"></div> <div class="comments"> <h3>评论</h3> <textarea class="comment-input" placeholder="写下你的评论..."></textarea> <button class="comment-btn">提交评论</button> <div class="comment-list"></div> </div> </div> </div> <!-- 夜间模式切换 --> <button class="theme-toggle" id="themeToggle">夜间模式</button> <!-- 返回顶部 --> <button class="back-to-top" id="backToTop">↑</button> <script> // 模拟文章数据 const articles = [ { id: 1, title: "JavaScript 动画技巧", category: "tech", thumb: "https://via.placeholder.com/300x160", content: "这是关于 JavaScript 动画的详细教程,涵盖了 CSS 动画、requestAnimationFrame 等技术..." }, { id: 2, title: "我的欧洲旅行日记", category: "travel", thumb: "https://via.placeholder.com/300x160", content: "这次旅行我去了巴黎、罗马和柏林,记录了沿途的风景和美食..." }, { id: 3, title: "如何高效学习编程", category: "tech", thumb: "https://via.placeholder.com/300x160", content: "学习编程需要方法和坚持,这篇文章分享了我的经验..." }, { id: 4, title: "健康生活的10个小习惯", category: "life", thumb: "https://via.placeholder.com/300x160", content: "每天一个小改变,让你的生活更健康..." }, { id: 5, title: "日本文化探秘", category: "travel", thumb: "https://via.placeholder.com/300x160", content: "从东京到京都,感受日本的传统与现代..." }, { id: 6, title: "CSS Grid 布局详解", category: "tech", thumb: "https://via.placeholder.com/300x160", content: "CSS Grid 是现代网页布局的利器,这篇文章带你深入了解..." }, ]; const ITEMS_PER_PAGE = 4; let currentPage = 1; let filteredArticles = articles; const articleGrid = document.getElementById("articleGrid"); const pagination = document.getElementById("pagination"); const searchInput = document.getElementById("searchInput"); const categoryFilter = document.getElementById("categoryFilter"); const articleModal = document.getElementById("articleModal"); const modalTitle = document.getElementById("modalTitle"); const modalBody = document.getElementById("modalBody"); const modalClose = document.getElementById("modalClose"); const themeToggle = document.getElementById("themeToggle"); const backToTop = document.getElementById("backToTop"); // 渲染文章 function renderArticles(page = 1) { articleGrid.innerHTML = ""; const start = (page - 1) * ITEMS_PER_PAGE; const end = start + ITEMS_PER_PAGE; const paginatedArticles = filteredArticles.slice(start, end); paginatedArticles.forEach(article => { const card = document.createElement("div"); card.className = "article-card"; card.innerHTML = ` <div class="article-thumb" style="background-image: url(${article.thumb})"></div> <div class="article-info"> <h3 class="article-title">${article.title}</h3> <p class="article-meta">分类: ${article.category}</p> </div> `; card.addEventListener("click", () => openArticle(article)); articleGrid.appendChild(card); }); renderPagination(); } // 渲染分页 function renderPagination() { pagination.innerHTML = ""; const totalPages = Math.ceil(filteredArticles.length / ITEMS_PER_PAGE); for (let i = 1; i <= totalPages; i++) { const btn = document.createElement("button"); btn.textContent = i; if (i === currentPage) btn.classList.add("active"); btn.addEventListener("click", () => { currentPage = i; renderArticles(currentPage); }); pagination.appendChild(btn); } } // 打开文章详情 function openArticle(article) { modalTitle.textContent = article.title; modalBody.textContent = article.content; articleModal.classList.add("active"); // 初始化评论 const commentList = articleModal.querySelector(".comment-list"); commentList.innerHTML = ""; const commentInput = articleModal.querySelector(".comment-input"); const commentBtn = articleModal.querySelector(".comment-btn"); commentBtn.onclick = () => { if (commentInput.value.trim()) { const comment = document.createElement("div"); comment.className = "comment-item"; comment.textContent = commentInput.value; commentList.appendChild(comment); commentInput.value = ""; } }; } // 关闭模态框 modalClose.addEventListener("click", () => articleModal.classList.remove("active")); articleModal.addEventListener("click", e => { if (e.target === articleModal) articleModal.classList.remove("active"); }); // 搜索功能 searchInput.addEventListener("focus", () => searchInput.classList.add("active")); searchInput.addEventListener("blur", () => { if (!searchInput.value) searchInput.classList.remove("active"); }); searchInput.addEventListener("input", () => { const query = searchInput.value.toLowerCase(); filteredArticles = articles.filter(article => article.title.toLowerCase().includes(query)); currentPage = 1; renderArticles(currentPage); }); // 分类筛选 categoryFilter.addEventListener("click", e => { const btn = e.target.closest(".category-btn"); if (!btn) return; document.querySelectorAll(".category-btn").forEach(b => b.classList.remove("active")); btn.classList.add("active"); const category = btn.dataset.category; filteredArticles = category === "all" ? articles : articles.filter(article => article.category === category); currentPage = 1; renderArticles(currentPage); }); // 夜间模式 themeToggle.addEventListener("click", () => { document.body.classList.toggle("dark"); themeToggle.textContent = document.body.classList.contains("dark") ? "白天模式" : "夜间模式"; }); // 返回顶部 window.addEventListener("scroll", () => { backToTop.classList.toggle("visible", window.scrollY > 300); }); backToTop.addEventListener("click", () => window.scrollTo({ top: 0, behavior: "smooth" })); // 初始化 renderArticles(); </script> </body> </html>
功能与动画效果说明:
功能:
- 文章列表与分页:文章以卡片形式展示,支持分页,每页显示 4 篇文章。
- 文章详情:点击卡片弹出模态框,显示文章全文,并支持评论(评论仅前端模拟)。
- 搜索功能:输入关键词实时过滤文章。
- 分类筛选:点击分类按钮过滤文章,支持“全部”显示。
- 夜间模式:点击右上角按钮切换明暗主题,所有元素颜色平滑过渡。
- 返回顶部:页面滚动超过 300px 时显示返回顶部按钮,点击平滑滚动到顶部。
- 响应式设计:适配手机、平板、桌面设备,搜索框、导航栏等在小屏幕上自动调整。
动画效果:
- 页面加载动画:头部、导航、主内容区域渐入并上滑。
- 文章卡片动画:卡片加载时渐入,悬停时放大并增强阴影,封面图缩放。
- 搜索框动画:聚焦时平滑展开,失焦时若无内容则收起。
- 分类按钮动画:点击或悬停时放大,背景色平滑过渡。
- 模态框动画:弹出时缩放并渐入,关闭按钮悬停时旋转。
- 分页按钮动画:点击或悬停时放大,背景色平滑过渡。
- 夜间模式动画:背景色、文字色、按钮色等平滑过渡。
- 返回顶部按钮动画:淡入淡出,悬停时放大。
使用说明:
- 直接将代码保存为
index.html,用浏览器打开即可体验。 - 文章数据是模拟的,封面图使用占位图(
https://via.placeholder.com),你可以替换为真实的图片链接。 - 评论功能仅前端模拟,刷新页面后会丢失。如果你需要持久化评论,可以结合后端(如 Node.js + MongoDB)或本地存储(如
localStorage)。
扩展建议:
如果你想进一步增强这个博客,可以考虑以下功能(需要后端支持或更复杂的 JavaScript):
- 文章发布:添加文章编辑器(如 TinyMCE 或 Markdown 编辑器)。
- 用户系统:支持用户注册、登录,评论绑定用户。
- 数据持久化:使用后端 API 或本地存储保存文章和评论。
- 更多动画:添加粒子效果、视差滚动、3D 翻转等高级动画(可使用 Three.js 或 GSAP)。
- SEO 优化:添加 meta 标签、结构化数据等。
如果你对某个扩展功能感兴趣,可以告诉我,我会帮你实现!😊
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.