HTML+CSS+JS案例展示(模态框显示移动与隐藏)

  参考来源:

JavaScript基础语法-dom-bom-js-es6新语法-jQuery-数据可视化echarts黑马pink老师前端入门基础视频教程(500多集)持续_哔哩哔哩_bilibili

效果展示:

网页GitHub地址如下:(若加载较慢建议刷新后耐心等待一会~)

https://zhiyuanda.github.io/js_login/

主要功能:

  1. 点击弹出模态框以及全屏灰色背景

  2. 点击关闭按钮,隐藏模态框以及背景

  3. 在模态框的头部按下鼠标开始移动,松开鼠标停止移动模态框

网页代码如下:

HTMLHTML+CSS+JS:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>js_login</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        a {
            text-decoration: none;
            color: #000;
        }
        .login {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            width: 500px;
            height: 218px;
            border: 1px solid #ccc;
            background-color: #fff;
            transform: translate(-50%,-50%);
            box-shadow: 0 0 20px #ddd;
            z-index: 9999;
        }
        .login-header {
            float: left;
            width: 100%;
            height: 100%;
            line-height: 40px;
            margin: 0 auto;
            text-align: center;
            font-weight: 700;
            font-size: 16px;

            color: #000;
        }
        .login-header a {
            display: block;
            margin: 0 auto;
        }
        .login-title {
            cursor: move;
            position: relative;
            text-align: center;
            font-size: 18px;
            height: 40px;
            line-height: 40px;
        }
        .login-title span{
            position: absolute;
            width: 40px;
            height: 40px;
            top: -20px;
            right: -30px;
            font-size: 12px;
            border: 1px solid #ebebeb;
            background-color: #fff;
            border-radius: 50%;
        }
        .login-bd {
            height: 108px;
        }
        .login-input {
            float: right;
            margin-right: 50px;
            height: 50px;
            line-height: 67px;
            font-size: 12px;
        }
        input {
            right: 50px;
            border: 1px solid #ccc;
            width: 370px;
            height: 30px;
        }
        .login-btn {
            margin: 15px 125px;
            font-size: 12px;
            width: 250px;
            height: 30px;
            line-height: 30px;
            border: 1px solid #ccc;
            text-align: center;
        }
        .login-bg {
            display: none;
            width: 100%;
            height: 1000px;
            background-color: gray;
            z-index: 1;
        }
    </style>
</head>
<body>
    <div id="login-header" class="login-header">
        <a href="javascript:;">点击弹出登录框</a>
    </div>
    <div id="login" class="login">
        <div class="login-title">登陆会员<span><a href="javascript:;" class="closebtn">关闭</a></span></div>
        <div class="login-bd">
            <div class="login-input">
                <label>用户名:</label>
                <input type="text" placeholder="请输入用户名" name="info[username]" id="username">
            </div>
            <div class="login-input">
                <label>登陆密码:</label>
                <input type="password" placeholder="请输入密码" name="info[password]" id="password">
            </div>
        </div>
        <div id="login-btn" class="login-btn">
            <a href="javascript:;">登录会员</a>
        </div>
    </div>
    <div id="bg" class="login-bg"></div>
    <script>
        //1. 获取元素
        var login = document.querySelector('.login');
        var bg = document.querySelector('.login-bg');
        var header = document.querySelector('.login-header');
        var closebtn = document.querySelector('.closebtn');
        var title = document.querySelector('.login-title');
        //2. 注册事件
        //2.1 点击弹出模态框以及全屏灰色背景
        header.addEventListener('click',open);
        function open() {
            login.style.display = 'block';
            bg.style.display = 'block';
        };
        //2.2 点击关闭按钮,隐藏模态框以及背景
        closebtn.addEventListener('click',close);
        function close() {
            login.style.display = 'none';
            bg.style.display = 'none';
        }
        //2.3 在模态框的头部按下鼠标开始移动,松开鼠标停止移动模态框
        title.addEventListener('mousedown',function(e){
            var x = e.pageX - login.offsetLeft;
            var y = e.pageY - login.offsetTop;
            document.addEventListener('mousemove',move);
            function move(e) {
                if((e.pageX - x) > 250 && (e.pageY - y) > 105){
                    login.style.left = e.pageX - x + 'px';
                    login.style.top = e.pageY - y + 'px';
                }
            }
            document.addEventListener('mouseup',function() {
                this.removeEventListener('mousemove',move);
            })
        });

    </script>
</body>
</html>

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的前端HTML + CSS + JS注册和登录案例HTML代码: ``` <!DOCTYPE html> <html> <head> <title>注册和登录</title> <link rel="stylesheet" href="style.css"> </head> <body> <h1>欢迎注册和登录</h1> <div class="container"> <div class="form-container"> <h2>注册</h2> <form id="register-form" onsubmit="return validateRegisterForm()"> <label for="username">用户名:</label> <input type="text" name="username" id="username"> <label for="password">密码:</label> <input type="password" name="password" id="password"> <label for="email">电子邮件:</label> <input type="email" name="email" id="email"> <button type="submit">注册</button> </form> </div> <div class="form-container"> <h2>登录</h2> <form id="login-form" onsubmit="return validateLoginForm()"> <label for="username">用户名:</label> <input type="text" name="username" id="login-username"> <label for="password">密码:</label> <input type="password" name="password" id="login-password"> <button type="submit">登录</button> </form> </div> </div> <script src="script.js"></script> </body> </html> ``` CSS代码: ``` body { font-family: Arial, Helvetica, sans-serif; background-color: #f4f4f4; } h1 { text-align: center; margin-top: 50px; } .container { display: flex; flex-direction: row; justify-content: center; align-items: center; margin-top: 50px; } .form-container { background-color: #fff; padding: 20px; margin: 20px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.3); } label { display: block; margin-bottom: 5px; } input[type="text"], input[type="password"], input[type="email"] { width: 100%; padding: 10px; margin-bottom: 10px; border: none; border-radius: 5px; background-color: #f4f4f4; } button[type="submit"] { background-color: #4CAF50; color: #fff; padding: 10px; border: none; border-radius: 5px; cursor: pointer; } button[type="submit"]:hover { background-color: #3e8e41; } ``` JavaScript代码: ``` function validateRegisterForm() { var username = document.getElementById("username").value; var password = document.getElementById("password").value; var email = document.getElementById("email").value; if (username == "") { alert("请输入用户名"); return false; } if (password == "") { alert("请输入密码"); return false; } if (email == "") { alert("请输入电子邮件"); return false; } alert("注册成功!"); return true; } function validateLoginForm() { var username = document.getElementById("login-username").value; var password = document.getElementById("login-password").value; if (username == "") { alert("请输入用户名"); return false; } if (password == "") { alert("请输入密码"); return false; } alert("登录成功!"); return true; } ``` 这个案例包含一个注册表单和一个登录表单。当用户提交表单时,JavaScript代码会验证输入是否有效,并根据结果显示成功或失败消息。您可以将此代码作为起点,进行更多的开发和改进,以实现更完整的注册和登录功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值