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

在现代网页设计中,创建一个功能齐全且用户友好的登录页面是至关重要的。本文将介绍如何使用 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 代码来处理页面上的交互。例如,显示和隐藏注册表单的功能:

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 处理交互。这种页面设计有助于提升用户体验,并且可以很容易地扩展和定制以满足具体需求。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 免费的html css js实现登录注册界面.rar是一套基于HTMLCSSJavaScript开发的前端代码集合,可以方便地实现登录注册功能,为开发者提供了一个免费的选择。该RAR压缩文件包含了许多前端代码文件,包括HTML文件、CSS文件和JavaScript文件,直接使用这些文件可以很方便地创建出一个登录注册界面。 在使用这些文件前,我们需要先了解HTMLCSSJavaScript的基础知识。HTML是一种标记语言,用于创建网页,CSS是一种用于显示HTML文件的样式表语言,而JavaScript是一种脚本语言,用于在网页中实现动态效果和用户交互。 当我们对这些技术有了一定的掌握后,就可以开始使用RAR压缩文件中的文件进行前端开发。首先,我们可以通过HTML文件创建出登录注册页面的框架,然后使用CSS文件来美化页面的样式,让页面看起来更加符合用户的审美需求。最后,我们可以使用JavaScript文件来实现页面的交互功能,例如实现用户输入信息的验证和自动补全等。 总之,免费的html css js实现登录注册界面.rar提供了一个快速开发前端界面的解决方案,非常适用于初学者和中级开发者。它不仅可以提高开发效率,还能为开发者节省不少时间和资源成本。 ### 回答2: 对于免费的html css js实现登录注册界面.rar,我并不推荐直接使用该文件进行网站的登录注册功能开发,原因如下: 首先,免费的资源未必是最适合我们的资源。向网络上公开的免费资源普遍缺乏鲁棒性,可能会存在代码bug和安全漏洞等隐患,从而影响我们网站的稳定性和安全性。 其次,在进行网站功能开发时,我们应该始终保持代码规范和可读性,方便以后的维护和升级。而免费的资源无法保证这些方面,可能在使用过程中出现代码冗余、变量名混乱等问题,给我们后续的开发和维护带来不便。 最后,为了使网站有更好的用户体验和不断进化的功能,我们应该注重网站的个性化和差异化,使用免费的开源资源可能会让我们在这些方面受到束缚,难以实现个性化需求。 因此,为了更好地开发网站的登录注册功能,我们应该采用更为专业、可靠的开发工具和技术,同时注重开发人员的技能提升和经验积累,从而创造出更为优秀的网站体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值