实现一个翻转注册登陆页面

使用语言为CSS和原生JS

大致思路如下:

1.创造两个模态框,一个为login,一个为register.

HTML
1
2
3
4
5
6
7
8
9
<main>
<div class="flip-modal login">
/* 登陆模态框 */
<div class="modal modal-login"></div>

/* 注册模态框 */
<div class="modal modal-login"></div>
</div>
</main>

2.并使用JS来控制flip-modal下的class为login或者register.

JS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$('.flip-modal').addEventListener('click', function(e){
e.stopPropagation();
if(e.target.classList.contains('login')){
$('.flip-modal').classList.remove('register');
$('.flip-modal').classList.add('login');
}

if(e.target.classList.contains('register')){
$('.flip-modal').classList.add('register');
$('.flip-modal').classList.remove('login');
}

if(e.target.classList.contains('close')){
$('.flip-modal').style.display = 'none';
}
});

3.CSS控制模态框翻转时,根据flip-modal下的class类型进行两个模态框的水平Y轴翻转,并隐藏翻转后的背面.
加上transition的过渡效果使翻转更自然。

1
2
3
4
5
6
7
8
9
10
11
12
13
.flip-modal.login .modal-login,
.flip-modal.register .modal-register {
transform: translate(-50%,-50%) rotateY(0deg);
backface-visibility: hidden;
transition: all 1s;
}

.flip-modal.login .modal-register,
.flip-modal.register .modal-login {
transform: translate(-50%,-50%) rotateY(180deg);
backface-visibility: hidden;
transition: all 1s;
}

效果图如下

代码预览地址


完整代码

HTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login|Regist</title>
<link rel="stylesheet" href="https://unpkg.com/font-awesome@4.7.0/css/font-awesome.min.css">
</head>
<body>
<main>
<header>
<a class="login" href="#">
<i class="fa fa-user-circle"></i>
</a>
</header>
<div class="flip-modal login">
<div class="modal modal-login">
<a class="close fa fa-close" href="#"></a>
<div class="tabs">
<a href="#" class="login active">登录</a>
<a href="#" class="register">注册</a>
</div>
<div class="content">
<div class="errormsg"></div>
<form action="/login" method="post">
<div class="input-field">
<i class="fa fa-user-o"></i>
<input name="username" type="text" placeholder="用户名">
</div>
<div class="input-field">
<i class="fa fa-lock"></i>
<input name="password" type="password" placeholder="密码">
</div>
<div class="input-field">
<input type="submit" value="登录">
</div>
</form>
</div>
</div>

<div class="modal modal-register">
<a class="close fa fa-close" href="#"></a>
<div class="tabs">
<a href="#" class="login">登录</a>
<a href="#" class="register active">注册</a>
</div>
<div class="content">
<div class="errormsg"></div>
<form action="/login" method="post">
<div class="input-field">
<i class="fa fa-user-o"></i>
<input name="username" type="text" placeholder="用户名">
</div>
<div class="input-field">
<i class="fa fa-lock"></i>
<input name="password" type="password" placeholder="密码">
</div>
<div class="input-field">
<i class="fa fa-lock"></i>
<input name="password2" type="password" placeholder="再次输入密码">
</div>
<div class="input-field">
<input type="submit" value="注册">
</div>
</form>
</div>
</div>
</div>
</main>
</body>
</html>
JS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function $(selector){
return document.querySelector(selector);
}

function $$(selector){
return document.querySelectorAll(selector);
}

$('header .login').onclick = function(e){
e.stopPropagation();
$('.flip-modal').style.display = 'block';
};

$('.flip-modal').addEventListener('click', function(e){
e.stopPropagation();
if(e.target.classList.contains('login')){
$('.flip-modal').classList.remove('register');
$('.flip-modal').classList.add('login');
}

if(e.target.classList.contains('register')){
$('.flip-modal').classList.add('register');
$('.flip-modal').classList.remove('login');
}

if(e.target.classList.contains('close')){
$('.flip-modal').style.display = 'none';
}
});

document.addEventListener ('click',function(){
$('.flip-modal').style.display = 'none';
});

$('.modal-login form').addEventListener('submit',function(e){
e.preventDefault();
if(!/^\w{3,8}$/.test($('.modal-login input[name=username]').value)){
$('.modal-login .errormsg').innerText = '用户名需要输入3-8个字符,包括字母数字和下划线';
return false;
}

if(!/^\w{6,10}$/.test($('.modal-login input[name=password]').value)){
$('.modal-login .errormsg').innerText = '密码需要输入6-10个字符,包括字母数字和下划线';
return false;
}
this.submit();
});

$('.modal-register form').addEventListener('submit',function(e){
e.preventDefault();
if(!/^\w{3,8}$/.test($('.modal-register input[name=username]').value)){
$('.modal-register .errormsg').innerText = '用户名需要输入3-8个字符,包括字母数字和下划线';
return false;
}

if(!/^\w{6,10}$/.test($('.modal-register input[name=password]').value)){
$('.modal-register .errormsg').innerText = '密码需要输入6-10个字符,包括字母数字和下划线';
return false;
}

if($('.modal-register input[name = password]').value != $('.modal-register input[name = password2]').value){
$('.modal-register .errormsg').innerText = '两次密码输入不一致';
return false;
}
this.submit();
});
CSS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

html, body, main {
height: 100%;
}

body {
font: 14px/1.5 Arial;
}

a {
text-decoration: none;
color: #444;
}

main {
background: linear-gradient(to right top, rgba(38,50,105,0.78), rgba(255,152,0,0.45)), url("http://n.sinaimg.cn/sinacn20111/348/w690h458/20190317/95f8-hukwxnu7092764.jpg") center center no-repeat;
}

herder {
position: absolute;
top: 10px;
width: 100%;
}

header .login {
float: right;
margin-right: 20px;
font-size: 20px;
}

.flip-modal {
display: none;
}

.show-modal {
display: block!important;
}

.modal {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 400px;
padding: 16px;
background-color: #fff;
box-shadow: 0 3px 4px 5px rgba(0,0,0,0.2);
}

.modal .close {
position: absolute;
right: 10px;
top: 10px;
}

.modal .tabs {
text-align: center;
}

.modal .tabs a {
display: inline-block;
font-size: 16px;
margin: 0 10px;
color: #666;
}

.modal .tabs a.active {
border-bottom: 2px solid #444;
color: #444;
}

.modal .content {
padding: 20px 40px;
}

.modal .errormsg {
color: #ff9800;
font-size: 12px;
}

.modal .content .input-field {
position: relative;
margin-top: 10px;
}

.modal .content .input-field .fa {
position: absolute;
left: 13px;
top: 13px;
}

.modal .content input[type=text],
.modal .content input[type=password] {
width: 100%;
padding: 10px 10px 10px 28px;
outline: none;
}

.modal .content input[type=submit] {
padding: 6px 10px;
border: none;
background: #fff;
box-shadow: 0 0 2px rgba(0,0,0,0.4);
outline: none;
}

.flip-modal.login .modal-login,
.flip-modal.register .modal-register {
transform: translate(-50%,-50%) rotateY(0deg);
backface-visibility: hidden;
transition: all 1s;
}

.flip-modal.login .modal-register,
.flip-modal.register .modal-login {
transform: translate(-50%,-50%) rotateY(180deg);
backface-visibility: hidden;
transition: all 1s;
}