HTML+CSS+Javascript实现弹窗登录

网站登录功能,弹窗体验更好。点击 登录 弹窗登录界面:

实现:HTML和CSS来完成基本布局和样式,JavaScript实现弹窗的显示和隐藏功能。

HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Popup Login</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="overlay" id="overlay">
        <div class="popup" id="popup">
            <h2>Login</h2>
            <form id="loginForm">
                <label for="username">Username:</label>
                <input type="text" id="username" name="username" required>
                <label for="password">Password:</label>
                <input type="password" id="password" name="password" required>
                <button type="submit">Login</button>
                <button type="button" onclick="closePopup()">Cancel</button>
            </form>
        </div>
    </div>
    <button onclick="openPopup()">Login</button>

    <script src="script.js"></script>
</body>
</html>

CSS代码,保存在styles.css文件中:

body {
    font-family: Arial, sans-serif;
}

.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: none;
    justify-content: center;
    align-items: center;
}

.popup {
    background: white;
    padding: 20px;
    border-radius: 5px;
    width: 300px;
    text-align: center;
    position: relative;
    z-index: 1;
}

.popup h2 {
    margin-top: 0;
}

.popup label {
    display: block;
    margin: 10px 0 5px;
}

.popup input {
    width: calc(100% - 22px);
    padding: 10px;
    margin-bottom: 10px;
    border: 1px solid #ccc;
    border-radius: 3px;
}

.popup button {
    padding: 10px 20px;
    border: none;
    border-radius: 3px;
    cursor: pointer;
    background-color: #007BFF;
    color: white;
    margin: 5px;
}

.popup button:hover {
    background-color: #0056b3;
}

Javascript代码,保存在script.js文件:

function openPopup() {
    document.getElementById('overlay').style.display = 'flex';
}

function closePopup() {
    document.getElementById('overlay').style.display = 'none';
}

document.getElementById('loginForm').addEventListener('submit', function(event) {
    event.preventDefault();
    
    alert('Login successful!');
    closePopup();
});

喜欢记得关注,有问题可留言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值