html+css+js实现登录界面设计

embedded/2024/12/23 3:16:08/

在现代网页设计中,创建一个功能齐全且用户友好的登录页面是至关重要的。本文将介绍如何使用 HTML 和 CSS 创建一个简单而有效的登录页面,包括验证码、记住密码选项及忘记密码链接。

1. HTML 结构

我们将从 HTML 代码开始,构建一个包含登录表单的页面。以下是代码示例:

<canvas id="myCanvas"></canvas><div class="box"><div style=" width: 100%;height: 100%;"><div style="display: block;" id="myLogin" style="width: 100%;height: 100%;"><div class="left"><button class="register" type="submit" onclick="toggleDivRegister()">去注册</button></div><div class="right" style="margin-left: 300px;"><h2>登 录</h2><form method="post" novalidate>{% csrf_token %}<div style="width: 400px;"><div class="form-group"><label>用户名</label>{{ form.username }}<span style="color: red;position:absolute">{{ form.username.errors.0 }}</span></div><div class="form-group"><label>密码</label>{{ form.password }}<span style="color: red;position:absolute">{{ form.password.errors.0 }}</span></div><div class="form-group"><label for="id_code">图片验证码</label><div class="row"><div class="col-xs-8">{{ form.code }}<span style="color: red;position:absolute">{{ form.code.errors.0 }}</span></div><div class="col-xs-4"><img id="image_code" src="/image/code/" style="width: 100%;height: 33px"></div></div></div><div style="padding: 10px;"><span style="float: left;display: flex;justify-content: center;align-content: center;"><input type="checkbox" style="vertical-align:top;" /><span style="font-size: 14px;vertical-align:top;padding-left: 5px;margin-top:4px">记住密码</span></span><span style="float: right;font-size: 15px;color: rgb(74, 159, 244);margin-top:4px">忘记密码?</span></div></div><button class="submit" type="submit">登录</button></form></div></div><div style="display: none;" id="myRegister" style="width: 100%;height: 100%;"><div class="left"><button class="register" type="submit" onclick="toggleDivLogin()">去登录</button></div><div class="right" style="margin-left: 300px;"><h2 style="color: rgb(1, 59, 100);font-size: 50px;margin-top: 40px;">注 册</h2><form action=""><div style="width: 400px;"><div><div style="display: flex;"><i class="iconPhone distant2"></i><input class="content" type="phone" placeholder="请输入手机号"style="height: 30px; padding-bottom: 10px;" required></div><div style="display: flex;"><i class="iconUser distant2"></i><input class="content" type="username" placeholder="请输入用户名"style="height: 30px; padding-bottom: 10px;" required></div><div style="display: flex;"><i class="iconPassword distant2"></i><input class="content" type="password" placeholder="请输入密码"style="height: 30px; padding-bottom: 10px;" required></div><div style="display: flex;"><i class="iconPassword distant2"></i><input class="content" type="password_reset" placeholder="请再次输入密码"style="height: 30px; padding-bottom: 10px;" required></div></div></div><button class="submit" type="submit" onclick="submitRegister()">立即注册</button></form></div></div></div></div>
2. CSS 样式

为了使页面看起来更美观,我们可以添加一些 CSS 样式。将以下样式添加到 styles.css 文件中:

* {margin: 0;padding: 0;box-sizing: border-box;}body {overflow: hidden;}canvas {display: block;position: absolute;z-index: 10;background-image: url("../../static/img/2.jpg");background-size: cover;}.box {width: 900px;height: 500px;background-color: rgba(255, 255, 255, 0.7);border-radius: 10px;box-shadow: 0 0 10px 6px rgba(0, 0, 0, 0.1);position: absolute;left: 23%;top: 25%;z-index: 200;}.box .left {width: 35%;height: 100%;position: absolute;z-index: 999;background-image: url(../../static/img/2.jpg);background-size: cover;}.register {position: absolute;width: 60%;height: 60px;color: #080808;background-color: transparent;font-size: 20px;border-radius: 5ch;border: 2px dotted #0984e3;margin: 70% 0 0 50%;transform: translateX(-50%);}.register:hover {color: #0984e3;}.box .right {display: flex;width: 65%;flex-direction: column;align-items: center;}.box .right h2 {color: rgb(1, 59, 100);font-size: 50px;margin-top: 50px;}.box .right form {display: flex;flex-wrap: wrap;justify-content: center;}.box .right form .content {width: 100%;height: 50px;font-size: 20px;margin-top: 30px;padding: 10px 0 0 10px;border: none;border-bottom: 1px dotted rgb(1, 59, 100);color: #74b9ff;}.right form .content:focus {outline: none;color: #74b9ff;padding: 10px 0 0 20px;}.right .submit {width: 60%;height: 60px;color: #f6f6f6;background-image: linear-gradient(120deg, rgb(1, 59, 100), #b1d2f6 100%);font-size: 20px;border-radius: 5ch;border: none;margin: 30px 0 0 50%;transform: translateX(-50%);}.right .submit:hover {box-shadow: 0 0 5px 5px rgba(20, 20, 20, 0.15);}.iconPhone {display: inline-block;height: 20px;width: 20px;background-image: url(../../static/img/phone.svg);background-size: cover;/*这里放置图标的绝对路径*/background-repeat: no-repeat;}.iconPassword {display: inline-block;height: 20px;width: 20px;background-image: url(../../static/img/password.svg);background-size: cover;/*这里放置图标的绝对路径*/background-repeat: no-repeat;}.iconUser {display: inline-block;height: 20px;width: 20px;background-image: url(../../static/img/user.svg);background-size: cover;/*这里放置图标的绝对路径*/background-repeat: no-repeat;}.distant2 {margin-top: 35px;}.distant1 {margin-top: 50px;}
3. JavaScript 动态效果

为了增强用户体验,可以添加一些 JavaScript 代码来处理页面上的交互。例如,显示和隐藏注册表单的功能:

javascript">const canvas = document.getElementById("myCanvas");const ctx = canvas.getContext("2d");const width = window.innerWidth;const height = window.innerHeight;canvas.width = width;canvas.height = height;const particles = [];const connections = [];const particleCount = 300;   // 粒子数量const particleSpeed = 1;     // 粒子移动速度const particleSize = 2;      // 粒子大小const maxDistance = 100;     // 粒子连线的最大距离const lightningColor = "#fff"; // 粒子连线的颜色// 创建粒子类class Particle {constructor() {this.x = Math.random() * width;this.y = Math.random() * height;this.color = "#fff";this.angle = Math.random() * 360;this.speed = Math.random() * particleSpeed;this.opacity = Math.random() * 0.5 + 0.5;}update() {this.x += Math.cos(this.angle) * this.speed;this.y += Math.sin(this.angle) * this.speed;// 如果粒子超出画布范围,则重新随机设置位置if (this.x < 0 || this.x > width || this.y < 0 || this.y > height) {this.x = Math.random() * width;this.y = Math.random() * height;}}draw() {ctx.beginPath();ctx.arc(this.x, this.y, particleSize, 0, Math.PI * 2);ctx.fillStyle = `rgba(255, 255, 255, ${this.opacity})`;ctx.fill();}}// 创建粒子数组function createParticles() {for (let i = 0; i < particleCount; i++) {particles.push(new Particle());}}// 绘制粒子之间的连线function drawConnections() {for (let i = 0; i < particles.length; i++) {for (let j = i + 1; j < particles.length; j++) {const dx = particles[i].x - particles[j].x;const dy = particles[i].y - particles[j].y;const distance = Math.sqrt(dx * dx + dy * dy);if (distance < maxDistance) {ctx.beginPath();ctx.moveTo(particles[i].x, particles[i].y);ctx.lineTo(particles[j].x, particles[j].y);ctx.strokeStyle = lightningColor;ctx.lineWidth = 0.2 * (1 - distance / maxDistance);ctx.stroke();ctx.closePath();}}}}// 动画循环function animate() {ctx.clearRect(0, 0, width, height);for (const particle of particles) {particle.update();particle.draw();}drawConnections();requestAnimationFrame(animate);}// 监听鼠标移动事件,根据鼠标位置更新粒子运动状态document.addEventListener("mousemove", (e) => {const mouseX = e.clientX;const mouseY = e.clientY;for (const particle of particles) {const dx = mouseX - particle.x;const dy = mouseY - particle.y;const distance = Math.sqrt(dx * dx + dy * dy);if (distance < maxDistance) {particle.angle = Math.atan2(dy, dx);particle.speed = 5;} else {particle.speed = Math.random() * particleSpeed;}}});// 初始化粒子数组并启动动画createParticles();animate();

完整代码

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><link rel="stylesheet" href="/static/plugins/bootstrap-3.3.5/css/bootstrap.css"><style>* {margin: 0;padding: 0;box-sizing: border-box;}body {overflow: hidden;}canvas {display: block;position: absolute;z-index: 10;background-image: url("../../static/img/2.jpg");background-size: cover;}.box {width: 900px;height: 500px;background-color: rgba(255, 255, 255, 0.7);border-radius: 10px;box-shadow: 0 0 10px 6px rgba(0, 0, 0, 0.1);position: absolute;left: 23%;top: 25%;z-index: 200;}.box .left {width: 35%;height: 100%;position: absolute;z-index: 999;background-image: url(../../static/img/2.jpg);background-size: cover;}.register {position: absolute;width: 60%;height: 60px;color: #080808;background-color: transparent;font-size: 20px;border-radius: 5ch;border: 2px dotted #0984e3;margin: 70% 0 0 50%;transform: translateX(-50%);}.register:hover {color: #0984e3;}.box .right {display: flex;width: 65%;flex-direction: column;align-items: center;}.box .right h2 {color: rgb(1, 59, 100);font-size: 50px;margin-top: 50px;}.box .right form {display: flex;flex-wrap: wrap;justify-content: center;}.box .right form .content {width: 100%;height: 50px;font-size: 20px;margin-top: 30px;padding: 10px 0 0 10px;border: none;border-bottom: 1px dotted rgb(1, 59, 100);color: #74b9ff;}.right form .content:focus {outline: none;color: #74b9ff;padding: 10px 0 0 20px;}.right .submit {width: 60%;height: 60px;color: #f6f6f6;background-image: linear-gradient(120deg, rgb(1, 59, 100), #b1d2f6 100%);font-size: 20px;border-radius: 5ch;border: none;margin: 30px 0 0 50%;transform: translateX(-50%);}.right .submit:hover {box-shadow: 0 0 5px 5px rgba(20, 20, 20, 0.15);}.iconPhone {display: inline-block;height: 20px;width: 20px;background-image: url(../../static/img/phone.svg);background-size: cover;/*这里放置图标的绝对路径*/background-repeat: no-repeat;}.iconPassword {display: inline-block;height: 20px;width: 20px;background-image: url(../../static/img/password.svg);background-size: cover;/*这里放置图标的绝对路径*/background-repeat: no-repeat;}.iconUser {display: inline-block;height: 20px;width: 20px;background-image: url(../../static/img/user.svg);background-size: cover;/*这里放置图标的绝对路径*/background-repeat: no-repeat;}.distant2 {margin-top: 35px;}.distant1 {margin-top: 50px;}</style><script>function toggleDivRegister() {var x = document.getElementById("myRegister");var y = document.getElementById("myLogin");x.style.display = "block";y.style.display = "none";}function toggleDivLogin() {var x = document.getElementById("myRegister");var y = document.getElementById("myLogin");x.style.display = "none";y.style.display = "block";}function submitRegister() {// alert("注册成功!")}</script>
</head>
<body><canvas id="myCanvas"></canvas><div class="box"><div style=" width: 100%;height: 100%;"><div style="display: block;" id="myLogin" style="width: 100%;height: 100%;"><div class="left"><button class="register" type="submit" onclick="toggleDivRegister()">去注册</button></div><div class="right" style="margin-left: 300px;"><h2>登 录</h2><form method="post" novalidate>{% csrf_token %}<div style="width: 400px;"><div class="form-group"><label>用户名</label>{{ form.username }}<span style="color: red;position:absolute">{{ form.username.errors.0 }}</span></div><div class="form-group"><label>密码</label>{{ form.password }}<span style="color: red;position:absolute">{{ form.password.errors.0 }}</span></div><div class="form-group"><label for="id_code">图片验证码</label><div class="row"><div class="col-xs-8">{{ form.code }}<span style="color: red;position:absolute">{{ form.code.errors.0 }}</span></div><div class="col-xs-4"><img id="image_code" src="/image/code/" style="width: 100%;height: 33px"></div></div></div><div style="padding: 10px;"><span style="float: left;display: flex;justify-content: center;align-content: center;"><input type="checkbox" style="vertical-align:top;" /><span style="font-size: 14px;vertical-align:top;padding-left: 5px;margin-top:4px">记住密码</span></span><span style="float: right;font-size: 15px;color: rgb(74, 159, 244);margin-top:4px">忘记密码?</span></div></div><button class="submit" type="submit">登录</button></form></div></div><div style="display: none;" id="myRegister" style="width: 100%;height: 100%;"><div class="left"><button class="register" type="submit" onclick="toggleDivLogin()">去登录</button></div><div class="right" style="margin-left: 300px;"><h2 style="color: rgb(1, 59, 100);font-size: 50px;margin-top: 40px;">注 册</h2><form action=""><div style="width: 400px;"><div><div style="display: flex;"><i class="iconPhone distant2"></i><input class="content" type="phone" placeholder="请输入手机号"style="height: 30px; padding-bottom: 10px;" required></div><div style="display: flex;"><i class="iconUser distant2"></i><input class="content" type="username" placeholder="请输入用户名"style="height: 30px; padding-bottom: 10px;" required></div><div style="display: flex;"><i class="iconPassword distant2"></i><input class="content" type="password" placeholder="请输入密码"style="height: 30px; padding-bottom: 10px;" required></div><div style="display: flex;"><i class="iconPassword distant2"></i><input class="content" type="password_reset" placeholder="请再次输入密码"style="height: 30px; padding-bottom: 10px;" required></div></div></div><button class="submit" type="submit" onclick="submitRegister()">立即注册</button></form></div></div></div></div><script>const canvas = document.getElementById("myCanvas");const ctx = canvas.getContext("2d");const width = window.innerWidth;const height = window.innerHeight;canvas.width = width;canvas.height = height;const particles = [];const connections = [];const particleCount = 300;   // 粒子数量const particleSpeed = 1;     // 粒子移动速度const particleSize = 2;      // 粒子大小const maxDistance = 100;     // 粒子连线的最大距离const lightningColor = "#fff"; // 粒子连线的颜色// 创建粒子类class Particle {constructor() {this.x = Math.random() * width;this.y = Math.random() * height;this.color = "#fff";this.angle = Math.random() * 360;this.speed = Math.random() * particleSpeed;this.opacity = Math.random() * 0.5 + 0.5;}update() {this.x += Math.cos(this.angle) * this.speed;this.y += Math.sin(this.angle) * this.speed;// 如果粒子超出画布范围,则重新随机设置位置if (this.x < 0 || this.x > width || this.y < 0 || this.y > height) {this.x = Math.random() * width;this.y = Math.random() * height;}}draw() {ctx.beginPath();ctx.arc(this.x, this.y, particleSize, 0, Math.PI * 2);ctx.fillStyle = `rgba(255, 255, 255, ${this.opacity})`;ctx.fill();}}// 创建粒子数组function createParticles() {for (let i = 0; i < particleCount; i++) {particles.push(new Particle());}}// 绘制粒子之间的连线function drawConnections() {for (let i = 0; i < particles.length; i++) {for (let j = i + 1; j < particles.length; j++) {const dx = particles[i].x - particles[j].x;const dy = particles[i].y - particles[j].y;const distance = Math.sqrt(dx * dx + dy * dy);if (distance < maxDistance) {ctx.beginPath();ctx.moveTo(particles[i].x, particles[i].y);ctx.lineTo(particles[j].x, particles[j].y);ctx.strokeStyle = lightningColor;ctx.lineWidth = 0.2 * (1 - distance / maxDistance);ctx.stroke();ctx.closePath();}}}}// 动画循环function animate() {ctx.clearRect(0, 0, width, height);for (const particle of particles) {particle.update();particle.draw();}drawConnections();requestAnimationFrame(animate);}// 监听鼠标移动事件,根据鼠标位置更新粒子运动状态document.addEventListener("mousemove", (e) => {const mouseX = e.clientX;const mouseY = e.clientY;for (const particle of particles) {const dx = mouseX - particle.x;const dy = mouseY - particle.y;const distance = Math.sqrt(dx * dx + dy * dy);if (distance < maxDistance) {particle.angle = Math.atan2(dy, dx);particle.speed = 5;} else {particle.speed = Math.random() * particleSpeed;}}});// 初始化粒子数组并启动动画createParticles();animate();</script>
</body>
</html>
实现效果

总结

通过上述步骤,你可以创建一个功能齐全且美观的登录页面。HTML 提供了结构,CSS 美化了样式,JavaScript 处理交互。这种页面设计有助于提升用户体验,并且可以很容易地扩展和定制以满足具体需求。


http://www.ppmy.cn/embedded/99543.html

相关文章

LINUX考试知识点总结

一.填空题 1. 文件权限 — rwx rwx rwx 文件 类型 user group other d rwx rwx rwx 文件夹 2.文件编辑器的基本操作 w保存 q退出 q!强制退出 3.文件链接命令 软链接:ln -s 目标 链接名 硬链接:ln 4.创建文件/文件夹 touch mkdir 5.文件不同对比…

把握国产替代机遇,水浸传感器厂家哪家强?

一旦发生漏水&#xff0c;不仅会影响到人们的正常生活&#xff0c;更会导致一些工业生产场景出现重大财产损失。因此&#xff0c;水浸传感器成为了保障安全生产必不可缺的一环。水浸传感器厂家哪家强&#xff1f; 水浸传感器的历史可以追溯到上世纪60年代&#xff0c;等到80年代…

GitFlow的四个分支

GitFlow中有四个分支&#xff1a; 常规分支&#xff1a;master & develop master主分支储存正式发布的历史&#xff0c;develop开发分支作为功能开发集成分支功能分支&#xff1a;feature 每个feature分支都对应一个要实现的具体功能或者改进点&#xff0c;这样可以保持每…

django学习入门系列之第九点《MySQL命令介绍三》

文章目录 数据行操作1 新增数据2 删除数据3 修改数据4 查询数据 小节往期回顾 数据行操作 1 新增数据 insert into 表名&#xff08;列名&#xff0c;列名&#xff09; values(值,值) insert into 表名&#xff08;列名&#xff0c;列名&#xff09; values(值,值),(值&#…

【全网行为管理解决方案】上网行为系统有哪些?

全网行为管理系统是一种用于监控、管理和优化企业内部网络中所有用户活动及网络流量的技术解决方案。 这类系统可以帮助企业提高网络安全、优化网络性能&#xff0c;并确保网络使用符合公司政策及法规要求。以下是几种常用的上网行为管理系统&#xff1a; 一、安企神 特点&am…

鸿蒙Harmony实战:Accessibility Kit应用开发

前言&#xff1a; “Accessibility”译作“信息无障碍”&#xff0c;是指任何人在任何情况下都能平等、方便地获取信息并利用信息。其目的是缩小全社会不同阶层、不同地区、不同年龄、不同健康状况的人群在信息理解、信息交互、信息利用方面的数字鸿沟&#xff0c;使其更加方便…

[数据集][目标检测]考场行为作弊检测数据集VOC+YOLO格式4413张4类别

数据集格式&#xff1a;Pascal VOC格式YOLO格式(不包含分割路径的txt文件&#xff0c;仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数)&#xff1a;4413 标注数量(xml文件个数)&#xff1a;4413 标注数量(txt文件个数)&#xff1a;4413 标注…

【数据结构】关于快速排序,归并排序,计数排序,基数排序,你到底了解多少???(超详解)

前言&#xff1a; &#x1f31f;&#x1f31f;Hello家人们&#xff0c;这期继续讲解排序算法的原理&#xff0c;希望你能帮到屏幕前的你。 &#x1f308;上期博客在这里&#xff1a;http://t.csdnimg.cn/g7PyB &#x1f308;感兴趣的小伙伴看一看小编主页&#xff1a;GGBondlct…