大家可以先看一下輸入框輸入效果


這個效果需要注意的就是輸入框觸發的時機問題,在輸入框獲取焦點的時候:Name文字會向上移動,輸入框的下邊距會出現,
會用到input標簽的focus和valid
:focus
定義:獲得焦點的元素,一般用于表單(input、textarea);
觸發條件:當用戶點擊或觸摸元素或通過鍵盤的 “tab” 鍵選擇它時會被觸發,
:valid
定義:偽類指定一個通過匹配正確的所要求的表單元素;
在這里:valid主要的功能是在input輸入框失去焦點是保持最后的狀態,
下面是代碼部分:
html部分:
<div class="wrapper">
<div class="input-data">
<input type="text" required="" />
<div class="underline"></div>
<label>Name</label>
</div>
</div>
下面是css代碼部分
*{
margin: 0;
padding: 0;
outline: none;
box-sizing: border-box;
}
body{
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
background: linear-gradient(-135deg,#c850c0,#4158d0);
}
.wrapper{
width: 450px;
background-color: #fff;
padding: 30px;
box-shadow: 0px 0px 10px rgba(0,0,0,0.1);
}
.wrapper .input-data{
width: 100%;
height: 40px;
position: relative;
}
.wrapper .input-data input{
width: 100%;
height: 100%;
border: none;
border-bottom: 2px solid silver;
font-size: 17px;
}
.input-data input:focus ~ label,
.input-data input:valid ~ label{
transform: translateY(-20px);
font-size: 15px;
color: #4158D0;
}
.wrapper .input-data label{
position: absolute;
bottom: 10px;
left: 0;
color: grey;
pointer-events: none;
transition: all 0.3s ease;
}
.wrapper .input-data .underline{
position: absolute;
bottom: 0px;
height: 2px;
width: 100%;
}
.input-data .underline:before{
position: absolute;
content: "";
height: 100%;
width: 100%;
background: #4158D0;
transform: scaleX(0);
transition:transform 0.3s ease;
}
.input-data input:focus ~ .underline:before,
.input-data input:valid ~ .underline:before{
transform: scaleX(1);
}
下面就是完整的代碼部分,需要的小伙伴直接復制就可以了,
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="./css/index.css" />
</head>
<style type="text/css">
*{
margin: 0;
padding: 0;
outline: none;
box-sizing: border-box;
}
body{
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
background: linear-gradient(-135deg,#c850c0,#4158d0);
}
.wrapper{
width: 450px;
background-color: #fff;
padding: 30px;
box-shadow: 0px 0px 10px rgba(0,0,0,0.1);
}
.wrapper .input-data{
width: 100%;
height: 40px;
position: relative;
}
.wrapper .input-data input{
width: 100%;
height: 100%;
border: none;
border-bottom: 2px solid silver;
font-size: 17px;
}
.input-data input:focus ~ label,
.input-data input:valid ~ label{
transform: translateY(-20px);
font-size: 15px;
color: #4158D0;
}
.wrapper .input-data label{
position: absolute;
bottom: 10px;
left: 0;
color: grey;
pointer-events: none;
transition: all 0.3s ease;
}
.wrapper .input-data .underline{
position: absolute;
bottom: 0px;
height: 2px;
width: 100%;
}
.input-data .underline:before{
position: absolute;
content: "";
height: 100%;
width: 100%;
background: #4158D0;
transform: scaleX(0);
transition:transform 0.3s ease;
}
.input-data input:focus ~ .underline:before,
.input-data input:valid ~ .underline:before{
transform: scaleX(1);
}
</style>
<body>
<div class="wrapper">
<div class="input-data">
<input type="text" required="" />
<div class="underline"></div>
<label>Name</label>
</div>
</div>
</body>
</html>
最后,整理不易,走過路過的小伙伴們,留個贊再走吧,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/241860.html
標籤:其他
上一篇:文字點閃特效 html+css
下一篇:看完包會的Vuejs基礎
