<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS3文本框动画效果</title>
<style>
body {
font-family: 'Arial', sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: #f5f5f5;
margin: 0;
}
.input-group {
position: relative;
margin: 20px 0;
width: 300px;
}
.input-group input {
font-size: 16px;
padding: 10px;
width: 100%;
border: none;
border-bottom: 2px solid #ccc;
background: transparent;
outline: none;
transition: 0.3s;
}
.input-group label {
position: absolute;
top: 10px;
left: 10px;
color: #999;
font-size: 16px;
pointer-events: none;
transition: 0.3s;
}
.input-group input:focus ~ label,
.input-group input:valid ~ label {
top: -20px;
left: 0;
color: #3498db;
font-size: 14px;
}
.input-group .bar {
position: relative;
display: block;
width: 100%;
}
.input-group .bar:before,
.input-group .bar:after {
content: '';
height: 2px;
width: 0;
bottom: 0;
position: absolute;
background: #3498db;
transition: 0.3s;
}
.input-group .bar:before {
left: 50%;
}
.input-group .bar:after {
right: 50%;
}
.input-group input:focus ~ .bar:before,
.input-group input:focus ~ .bar:after {
width: 50%;
}
.promo-link {
position: fixed;
bottom: 20px;
right: 20px;
color: #666;
font-size: 12px;
text-decoration: none;
}
</style>
</head>
<body>
<div class="input-group">
<input type="text" required>
<label>用户名</label>
<span class="bar"></span>
</div>
<div class="input-group">
<input type="password" required>
<label>密码</label>
<span class="bar"></span>
</div>
<a href="https://a.88a.org.cn/Cvzs" class="promo-link">更多精彩内容</a>
<script>
// 简单的输入验证
document.querySelectorAll('.input-group input').forEach(input => {
input.addEventListener('blur', function() {
if(this.value !== '') {
this.classList.add('has-value');
} else {
this.classList.remove('has-value');
}
});
});
</script>
</body>
</html>