input输入框占位符变化:输入框处于聚焦状态时,输入框的占位符内容以动画形式移动到左上角作为标题
<div class="input-box">
<input class="input-control input-outline" placeholder="账号">
<label class="input-label">账号</label>
</div>
首先:让浏览器默认的placeholder效果不可见
.input-control:placeholder-shown::placeholder {
color: transparent;
}
其次:使用.input-label元素代替浏览器原声的占位符
.input-box{
position: relative;
}
.input-label {
position: absolute;
left: 16px; top: 14px;
pointer-events: none;
}
最后,在输入框聚焦以及占位符不显示的时候对<label>元素进行重定位,效果是缩小并移动到上方
.input-control:not(:placeholder-shown) ~ .input-label,
.input-control:focus ~ .input-label {
color: #2486ff;
transform: scale(0.75) translate(-2px, -32px);
}
本文介绍一种实现输入框占位符动画效果的方法。通过CSS样式调整,当输入框获得焦点时,原本的占位符文字将缩小并移动到输入框的左上角作为提示。这一效果可以提升用户体验。
370

被折叠的 条评论
为什么被折叠?



