今天分享一个登录注册页面,正好在学习媒体查询,在此记录一下。、
目录
一、页面展示
二、准备
引入谷歌字体@import url('https://fonts.font.im/css?family=Poppins');
左上方logo为html5标志图片,也可使用其他图片根据自己的喜好,或者是引入图标库
boxincons图标库:
<link href='https://unpkg.com/boxicons@2.1.2/css/boxicons.min.css' rel='stylesheet'>
Boxicons : Premium web friendly icons for free
三、代码部分
1.HTML
(1)autocomplete属性: on或off。on: 表单有自动完成功能。off: 表单无自动完成功能。注:autocomplete属性不仅可以用于form元素,还可以用于所以输入类型的input元素。
(2)minlength属性用来指定文本框或 textarea 中,允许输入的最少字符数。一个汉字被看作一个字符。在以下文本框中,用户最少要输入 4个字符,同理maxlength为输入的最多字符。
(3)required属性为html5的新属性,定义及用法:required 属性是一个布尔属性,规定必需在提交之前填写输入字段。如果使用该属性,则字段是必填(或必选)的。
(4)bullets:自定义轮播图指示点(indicator-dots),定义data-value给指示点赋值
<main>
<div class="box">
<div class="inner_box">
<div class="forms_wrap">
<form action="login2.html" autocomplete="off" class="login_form">
<div class="logo">
<!-- <i class='bx bxl-html5 bx-lg'></i> -->
<img src="/login_template//login2/HTML5.jpg">
<h4>easyclass</h4>
</div>
<div class="heading">
<h2>欢迎回来</h2>
<h6>还没有注册?</h6>
<a href="#" class="toggle">去注册</a>
</div>
<div class="actual_form">
<div class="input_wrap">
<input type="text" minlength="4" class="input_field" autocomplete="off" required>
<label>账号</label>
</div>
<div class="input_wrap">
<input type="password" minlength="4" class="input_field" autocomplete="off" required>
<label>密码</label>
</div>
<input type="submit" value="注册" class="sign_btn">
<p class="text">
忘记账号或密码,<a href="#">点击</a>找回
</p>
</div>
</form>
<form action="login2.html" autocomplete="off" class="register_form">
<div class="logo">
<!-- <i class='bx bxl-html5 bx-lg'></i> -->
<img src="/login_template//login2/HTML5.jpg">
<h4>easyclass</h4>
</div>
<div class="heading">
<h2>开始吧</h2>
<h6>已有账号?</h6>
<a href="#" class="toggle">去登录</a>
</div>
<div class="actual_form">
<div class="input_wrap">
<input type="text" minlength="4" class="input_field" autocomplete="off" required>
<label>账号</label>
</div>
<div class="input_wrap">
<input type="email" class="input_field" autocomplete="off" required>
<label>邮箱</label>
</div>
<div class="input_wrap">
<input type="password" minlength="4" class="input_field" autocomplete="off" required>
<label>密码</label>
</div>
<div class="input_wrap">
<input type="password" minlength="4" class="input_field" autocomplete="off" required>
<label>再次输入密码</label>
</div>
<input type="submit" value="注册" class="sign_btn">
<p class="text">
通过注册,我同意<a href="#">服务</a>和<a href="#">隐私政策</a>
</p>
</div>
</form>
</div>
<div class="carousel">
<div class="images_wrapper">
<img src="/login_template/login2/img/gallery-img/2.jpg" class="image img1 show" alt="">
<img src="/login_template/login2/img/gallery-img/3.jpg" class="image img2" alt="">
<img src="/login_template/login2/img/gallery-img/4.jpg" class="image img3" alt="">
</div>
<div class="text_slider">
<div class="text_wrapper">
<div class="text_group">
<h2>创建属于自己的账号</h2>
<h2>打造专属个人空间</h2>
<h2>分享多彩生活</h2>
</div>
</div>
<div class="bullets">
<span class="active" data-value="1"></span>
<span data-value="2"></span>
<span data-value="3"></span>
</div>
</div>
</div>
</div>
</div>
</main>
2.CSS
(1)::before ::after伪元素,css3为了区分伪类和伪元素,伪元素采用双冒号写法。::before和::after下特有的content,用于在css渲染中向元素逻辑上的头部或尾部添加内容。
(2)rem,rem是相对长度单位,相对于根元素(即html元素)的font-size计算值的倍数。px像素(Pixel)。相对长度单位。像素px是相对于显示器屏幕分辨率而言的。rem可以更好解决媒体查询自适应问题。rem与px的转换:假设我们设置html的字体大小的值为html{font-size: 87.5%;}(也就是14px)。然后其他的字体就是将你要的值除以14得到的值;比如默认的twentytwelve主题大小是960px;换算成rem就是960/14=68.57142857142857rem;padding的24px也就是24/14=1.714285714285714rem。
(3)display:grid ,设置该元素为网格容器,grid-template-columns: 列宽,grid-template-rows:行宽。详细讲解请看http://t.csdn.cn/pTEqf
@import url('https://fonts.font.im/css?family=Poppins');
*,
*::before,
*::after {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
font-family: 'Poppins', sans-serif;
}
main {
width: 100%;
min-height: 100vh;
overflow: hidden;
background-color: #ff8c6b;
padding: 2rem;
display: flex;
align-items: center;
justify-content: center;
}
.box {
position: relative;
width: 100%;
max-width: 820px;
height: 540px;
background-color: #fff;
border-radius: 3.3rem;
box-shadow: 0 60px 40px -30px rgba(0, 0, 0, 0.3);
}
.inner_box {
position: absolute;
width: calc(100% - 4.1rem);
height: calc(100% - 4.1rem);
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
/* background-color: red; */
}
.forms_wrap {
position: absolute;
height: 100%;
width: 45%;
top: 0;
left: 0;
/* background-color: green; */
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
transition: 0.8s ease-in-out;
}
.carousel {
position: absolute;
height: 100%;
width: 55%;
top: 0;
left: 45%;
background-color: #ffe0d2;
border-radius: 2rem;
display: grid;
grid-template-rows: auto 1fr;
overflow: hidden;
transition: 0.8s ease-in-out;
}
form {
/* border: 2px solid #000; */
max-width: 260px;
width: 100%;
/* background-color: red; */
margin: 0 auto;
height: 100%;
display: flex;
flex-direction: column;
justify-content: space-evenly;
grid-column: 1 / 2;
grid-row: 1 / 2;
transition: opacity 0.02s 0.4s;
}
/* form.login_form{
display: none;
} */
form.register_form {
opacity: 0;
pointer-events: none;
}
.logo {
/* border: 2px solid #000; */
display: flex;
align-items: center;
}
.logo img {
width: 35px;
margin: 0.3rem;
}
.logo h4 {
font-size: 1.2rem;
font-weight: 800;
margin-top: -5px;
letter-spacing: -0.5px;
color: #151111;
}
.heading {
text-align: center;
}
.heading h2 {
font-size: 2.1rem;
font-weight: 600;
color: #151111;
}
.heading h6 {
color: #bababa;
font-weight: 400;
font-size: 0.9rem;
display: inline;
}
.toggle {
color: #151111;
text-decoration: none;
font-size: 0.9rem;
font-weight: 700;
transition: 0.3s;
}
.toggle:hover {
color: #8371fd;
}
.input_wrap {
position: relative;
height: 37px;
margin-bottom: 2rem;
/* background-color: red; */
}
.input_field {
position: absolute;
width: 100%;
height: 100%;
background: none;
border: none;
outline: none;
border-bottom: 1px solid #bbb;
padding: 0;
font-size: 0.95rem;
color: #151111;
transition: 0.4s;
}
label {
position: absolute;
left: 0;
top: 50%;
transform: translateY(-50%);
font-size: 0.95rem;
color: #bbb;
pointer-events: none;
transition: 0.4s;
}
.input_field.active {
border-bottom-color: #151111;
}
.input_field.active+label {
font-size: 0.75rem;
top: -2px;
}
.sign_btn {
height: 2.5rem;
width: 100%;
background-color: #151111;
color: #fff;
border: none;
border-radius: 10px;
font-size: 15px;
cursor: pointer;
transition: 0.3s;
}
.sign_btn:hover {
background-color: #8371fd;
}
.text {
color: #bbb;
font-size: 0.8rem;
text-align: center;
margin-top: 10px;
}
.text a {
color: #151111;
transition: 0.3s;
text-decoration: none;
}
.text a:hover {
color: #8371fd;
}
main.sign_up_mode .forms_wrap {
left: 55%;
}
main.sign_up_mode .carousel {
left: 0;
}
main.sign_up_mode form.login_form {
opacity: 0;
pointer-events: none;
}
main.sign_up_mode form.register_form {
opacity: 1;
pointer-events: all;
}
.images_wrapper {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
}
.image {
width: 100%;
height: 300px;
grid-column: 1/2;
grid-row: 1/2;
opacity: 0;
transition: opacity 0.3s, transform 0.5s;
}
.img1 {
transform: translate(0, -50px);
}
.img2 {
transform: scale(0.4, 0.5);
}
.img3 {
transform: scale(0.3) rotate(-20deg);
}
.image.show {
opacity: 1;
transform: none;
}
.text_slider {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
/* margin-top: 5rem; */
}
.text_wrapper {
max-height: 2.2rem;
overflow: hidden;
margin-bottom: 2.5rem;
}
.text_group {
display: flex;
flex-direction: column;
text-align: center;
transform: translateY(0);
transition: 0.3s;
}
.text_group h2 {
/* line-height: 2.2rem; */
font-weight: 600;
font-size: 1.55rem;
}
.bullets {
display: flex;
align-items: center;
justify-content: center;
}
.bullets span {
display: block;
width: 0.5rem;
height: 0.5rem;
background-color: #aaa;
margin: 0 0.25rem;
border-radius: 50%;
cursor: pointer;
transition: 0.3s;
}
.bullets span.active {
width: 1.1rem;
background-color: #151111;
border-radius: 1rem;
}
@media (max-width:850px) {
.box {
height: auto;
max-width: 550px;
overflow: hidden;
}
.inner_box {
position: static;
transform: none;
width: revert;
height: revert;
padding: 2rem;
}
.forms_wrap {
position: revert;
width: 100%;
height: auto;
}
form {
max-width: revert;
padding: 1.5rem 2.5rem 2rem;
transition: transform 0.8s ease-in-out, opacity 0.45s linear;
}
.heading {
margin: 2rem 0;
}
form.register_form {
transform: translateX(100%);
}
main.sign_up_mode form.login_form {
transform: translateX(-100%);
}
main.sign_up_mode form.register_form {
transform: translateX(0);
}
.carousel {
position: revert;
width: 100%;
height: auto;
padding: 3rem 2rem;
display: flex;
}
.images_wrapper {
display: none;
}
.text_slider {
width: 100%;
}
}
@media(max-width:530px) {
main {
padding: 1rem;
}
.box {
border-radius: 2rem;
}
.inner_box {
padding: 1rem;
}
.carousel {
padding: 1.5rem 1rem;
border-radius: 1.6rem;
}
.text_wrapper {
margin-bottom: 1rem;
}
.text_group h2 {
font-size: 1.4rem;
}
form {
padding: 1rem 2rem 1.5rem;
}
}
3.JS
(1)获取组件
constinput = document.querySelectorAll(".input_field");
const toggle_btn = document.querySelectorAll(".toggle");
const main = document.querySelector("main");
const bullets = document.querySelectorAll(".bullets span");
const images = document.querySelectorAll(".image");
(2) input.forEach遍历显示输入框的样式,也可直接定义输入框的点击事件。moveSlider方法为点击指示点轮播图变换方法(思路可以学习一下)
const input = document.querySelectorAll(".input_field");
const toggle_btn = document.querySelectorAll(".toggle");
const main = document.querySelector("main");
const bullets = document.querySelectorAll(".bullets span");
const images = document.querySelectorAll(".image");
input.forEach((inp) => {
inp.addEventListener("focus", () => {
inp.classList.add("active");
});
inp.addEventListener("blur", () => {
if (inp.value != "") return;
inp.classList.remove("active");
});
});
toggle_btn.forEach((btn) => {
btn.addEventListener("click", () => {
main.classList.toggle("sign_up_mode");
});
});
bullets.forEach((bullets) => {
bullets.addEventListener("click", moveSlider);
});
function moveSlider() {
let index = this.dataset.value;
let currentImage = document.querySelector(`.img${index}`);
images.forEach(img => img.classList.remove("show"));
currentImage.classList.add("show");
const textSlider = document.querySelector(".text_group");
textSlider.style.transform = `translateY(${-(index - 1) * 2.2}rem)`;
bullets.forEach(bull => bull.classList.remove("active"));
this.classList.add("active");
}
四、尾语
以上为全部内容,请大家参考!
如有问题可私信,欢迎探讨!
本文含有隐藏内容,请 开通VIP 后查看