layui弹出层在登录中的应用
首先写好登录界面,然后在主界面登录按钮上添加layui弹出层。
登录界面、注册界面二合一(含表单验证)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
<link rel="stylesheet" href="layui/css/layui.css">
<style>
.login{
margin-top: 100px;
min-height: 400px;
}
</style>
</head>
<body>
<div class="layui-fluid">
<div class="layui-row">
<div class="layui-col-md4 layui-col-md-offset4 login">
<div class="layui-tab layui-tab-brief">
<ul class="layui-tab-title">
<li class="layui-this">登录</li>
<li>注册</li>
</ul>
<div class="layui-tab-content">
<div class="layui-tab-item layui-show">
<form action="" class="layui-form layui-form-pane">
<div class="layui-form-item">
<label class="layui-form-label"><i class="layui-icon layui-icon-user"></i></label>
<div class="layui-input-block">
<input type="text" name="username" lay-verify="required|username" class="layui-input" placeholder="用户名">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label"><i class="layui-icon layui-icon-password"></i></label>
<div class="layui-input-block">
<input type="password" name="password" required lay-verify="required|password" class="layui-input" placeholder="密码">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label"><i class="layui-icon layui-icon-friends"></i></label>
<div class="layui-input-block">
<input type="radio" name="grade" value="管理员" title="管理员">
<input type="radio" name="grade" value="用户" title="用户" checked>
</div>
</div>
<div class="layui-form-item">
<div class="layui-row layui-col-space20">
<div class="layui-col-md8 layui-col-xs8">
<input type="text" name="code" required lay-verify="required" class="layui-input" placeholder="验证码">
</div>
<div class="layui-col-md4 layui-col-xs4">
<img src="https://www.oschina.net/action/user/captcha">
</div>
</div>
</div>
<div class="layui-form-item">
<button class="layui-btn" lay-submit>登录</button>
<span style="margin-left: 30px"><a href="#">忘记密码</a></span>
</div>
</form>
</div>
<div class="layui-tab-item">
<form action="" class="layui-form layui-form-pane">
<div class="layui-form-item">
<label class="layui-form-label"><i class="layui-icon layui-icon-user"></i></label>
<div class="layui-input-block">
<input type="text" name="username2" required lay-verify="required|username" class="layui-input" placeholder="用户名">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label"><i class="layui-icon layui-icon-password"></i></label>
<div class="layui-input-block">
<input type="password" name="password2" required lay-verify="required|password" class="layui-input" placeholder="密码">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label"><i class="layui-icon layui-icon-cellphone"></i></label>
<div class="layui-input-block">
<input type="text" name="phone" required lay-verify="required|phone" class="layui-input" placeholder="手机号">
</div>
</div>
<div class="layui-form-item">
<div class="layui-row layui-col-space20">
<div class="layui-col-md8 layui-col-xs8">
<input type="text" name="code2" required lay-verify="required" class="layui-input" placeholder="验证码">
</div>
<div class="layui-col-md4 layui-col-xs4">
<a class="layui-btn">发送验证码至手机</a>
</div>
</div>
</div>
<div class="layui-form-item">
<button class="layui-btn" lay-submit>注册</button>
<span style="margin-left: 30px"><a href="#">已有账号,去登陆</a></span>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="layui/layui.js"></script>
<script>
layui.use(["element","form","layer"],function () {
var element = layui.element;
var form = layui.form;
var layer = layui.layer;
//表单验证功能
form.verify({
username: function(value, item){ //value:表单的值、item:表单的DOM对象
if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
return '用户名不能有特殊字符';
}
if(/(^\_)|(\__)|(\_+$)/.test(value)){
return '用户名首尾不能出现下划线\'_\'';
}
if(/^\d+\d+\d$/.test(value)){
return '用户名不能全为数字';
}
}
,password: [
/^[\S]{6,12}$/
,'密码必须6到12位,且不能出现空格'
]
,phone:[
/^\d{11}$/
,"手机号必须为11位数字"
]
});
})
</script>
</body>
</html>
效果图:
绑定弹出层
jquery选中登录按钮,然后添加点击事件,然后使用layui弹出层(iframe层)
<script>
$(".login").click(function () {
layui.use('layer',function () {
var layer = layui.layer;
layer.open({
type: 2,//iframe层
title: "信息",//标题
area: ['40%', '550px'],//弹出区域
content: "login.html",//所需弹出的登录/注册界面
offset: '50px'//设置弹出层在距离顶部,100px 水平居中
});
})
})
</script>
效果图: