在本教程中,我们将逐步使用 HTML、CSS 和 JavaScript 创建具有流畅动画和客户端验证的高级交互式多步骤表单。此表单提供了增强的用户体验,看起来就像直接来自 2025 年的东西!
现场演示:https://codepen.io/HanGPIIIErr/pen/ZYzbrqW
目录
1.简介
多步骤表单是将冗长的表单分解为可管理的部分,是增强用户体验的绝佳方法。在本教程中,我们将创建一个五步表单,其中包括:
我们将在步骤之间添加流畅的动画并验证用户输入以确保数据完整性。
先决条件
对 HTML、CSS 和 JavaScript 的基本了解
熟悉 JavaScript 中的表单元素和事件处理
项目结构
我们将有三个主要文件:
index.html — HTML 结构
style.css — 我们表单的样式
script.js — 处理表单交互的 JavaScript
让我们从设置 HTML 文件开始。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Premium Multi-Step Form</title> <link href="https://fonts.googleapis.com/css?family=Poppins:300,400,600&display=swap" rel="stylesheet"> <link rel="stylesheet" href="style.css"> </head> <body> <form> <p>Explanation</p> <ul> <li>Form Steps: Each step is wrapped in a div with the class form-step.</li> <li>Active Class: The first step has the class active to display it initially.</li> <li>Navigation Buttons: Each step (except the first and last) has "Previous" and "Next" buttons.</li> <li>Summary Section: The last step displays a summary of the entered information.</li> </ul> <p>Styling with CSS</p> <p>Now, let's style our form to give it that premium feel.<br> </p> <pre class="brush:php;toolbar:false">/* style.css */ body { font-family: 'Poppins', sans-serif; background: linear-gradient(135deg, #1abc9c, #16a085); display: flex; justify-content: center; align-items: center; height: 100vh; overflow: hidden; } form { width: 90%; max-width: 600px; background: rgba(255, 255, 255, 0.95); padding: 3em; border-radius: 20px; box-shadow: 0 15px 25px rgba(0, 0, 0, 0.2); backdrop-filter: blur(10px); position: relative; overflow: hidden; } .form-step { position: absolute; width: 100%; opacity: 0; transform: scale(0.8) translateY(50px); transition: all 0.5s ease; } .form-step.active { opacity: 1; transform: scale(1) translateY(0); position: relative; } .step-header { position: absolute; top: -30px; right: 30px; background: #16a085; color: #fff; padding: 0.5em 1em; border-radius: 30px; font-weight: 600; animation: slideIn 0.5s forwards; } h2 { margin-bottom: 1em; color: #333; font-weight: 600; text-align: center; animation: fadeInDown 0.5s ease-in-out; } label { display: block; margin-top: 1em; color: #555; font-weight: 500; animation: fadeInUp 0.5s ease-in-out; } input[type="text"], input[type="email"], input[type="file"], textarea { width: 100%; padding: 0.75em 1em; margin-top: 0.5em; border: 2px solid #ddd; border-radius: 10px; font-size: 1em; outline: none; transition: border-color 0.3s; animation: fadeInUp 0.5s ease-in-out; } input:focus, textarea:focus { border-color: #1abc9c; } input[type="checkbox"] { margin-right: 0.5em; } .buttons { display: flex; justify-content: space-between; margin-top: 2em; animation: fadeInUp 0.5s ease-in-out; } button { padding: 0.75em 2em; border: none; border-radius: 30px; cursor: pointer; font-size: 1em; font-weight: 600; transition: background 0.3s, transform 0.3s, box-shadow 0.3s; } .next-step, .prev-step { background: #1abc9c; color: #fff; } .next-step:hover, .prev-step:hover { background: #16a085; transform: translateY(-3px); box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1); } button[type="submit"] { background: #e74c3c; color: #fff; margin-left: auto; } button[type="submit"]:hover { background: #c0392b; transform: translateY(-3px); box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1); } #summary p { margin: 1em 0; color: #333; font-weight: 500; animation: fadeInUp 0.5s ease-in-out; } /* Animations */ @keyframes fadeInUp { from { opacity: 0; transform: translateY(30px); } to { opacity: 1; transform: translateY(0); } } @keyframes fadeInDown { from { opacity: 0; transform: translateY(-30px); } to { opacity: 1; transform: translateY(0); } } @keyframes slideIn { from { opacity: 0; transform: translateX(30px); } to { opacity: 1; transform: translateX(0); } }
说明
让我们的表单发挥作用。
document.addEventListener('DOMContentLoaded', function() { const form = document.getElementById('multi-step-form'); const steps = document.querySelectorAll('.form-step'); const nextBtns = document.querySelectorAll('.next-step'); const prevBtns = document.querySelectorAll('.prev-step'); const summary = document.getElementById('summary'); let currentStep = 0; nextBtns.forEach(btn => { btn.addEventListener('click', () => { if (validateStep()) { steps[currentStep].classList.remove('active'); currentStep++; if (currentStep < steps.length) { steps[currentStep].classList.add('active'); } if (currentStep === steps.length - 1) { displaySummary(); } } }); }); prevBtns.forEach(btn => { btn.addEventListener('click', () => { steps[currentStep].classList.remove('active'); currentStep--; steps[currentStep].classList.add('active'); }); }); form.addEventListener('submit', (e) => { e.preventDefault(); alert('Form successfully submitted!'); form.reset(); steps[currentStep].classList.remove('active'); currentStep = 0; steps[currentStep].classList.add('active'); }); function validateStep() { let stepIsValid = true; const currentInputs = steps[currentStep].querySelectorAll('input, textarea'); currentInputs.forEach(input => { if (!input.checkValidity()) { input.reportValidity(); stepIsValid = false; } }); return stepIsValid; } function displaySummary() { const name = document.getElementById('name').value || 'N/A'; const email = document.getElementById('email').value || 'N/A'; const prefs = Array.from(document.querySelectorAll('input[name="pref"]:checked')).map(el => el.value).join(', ') || 'None'; const comments = document.getElementById('comments').value || 'None'; summary.innerHTML = ` <p><strong>Name:</strong> ${name}</p> <p><strong>Email:</strong> ${email}</p> <p><strong>Preferences:</strong> ${prefs}</p> <p><strong>Comments:</strong> ${comments}</p> `; } // Initialize steps steps.forEach((step, index) => { if (index !== currentStep) { step.classList.remove('active'); } else { step.classList.add('active'); } }); });
说明
?结论:打造角斗士之战的未来
角斗士之战的最新增强功能标志着向所有玩家提供无缝且迷人的体验的重大飞跃。凭借丰富的教程系统、模块化组件、蓬勃发展的公会生态系统和优化的小游戏,该游戏正在进化为终极角斗RPG。
无论您是第一次探索竞技场的新人,还是主宰战场的经验丰富的战士,这些更新都确保每个人都可以打造自己的史诗遗产。
?加入旅程吧!
我们正在积极寻求玩家和开发者的反馈。深入了解《角斗士之战》并与我们分享您的想法。
?网站:https://gladiatorsbattle.com
?️ 在 Kickstarter 上支持我们:https://www.kickstarter.com/projects/gladiatorsbattle/gladiators-battle-forge-your-legend-in-the-ultimate-arena
?在 X(以前称为 Twitter)上关注我们:https://x.com/GladiatorsBT
?在 LinkedIn 上联系:https://www.linkedin.com/in/pierre-romain-lopez
?加入 Discord 社区:https://discord.gg/YBNF7KjGwx
感谢您对我们继续开发《角斗士之战》的坚定支持。您的反馈、想法和热情是我们进步的动力。
让冒险继续下去吧,角斗士们! ?✨
如果您有任何疑问或建议,请在下方留言!
以上是使用动画构建高级多步骤表单的详细内容。更多信息请关注PHP中文网其他相关文章!