2022-10-11  2024-09-18    274 字  1 分钟

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);
}