跳至主要內容

搜索框变形动画

望间代码HTML&CSS搜索框动画小于 1 分钟

CSS 世界之搜索框变形动画

效果展示

<div class="container">
  <label>
    <input type="text" required />
    <span class="line"></span>
  </label>
</div>
.container {
  position: relative;
  height: 60px;
  width: 100%;
  display: flex;
  justify-content: center;
}
.container label {
  position: relative;
}
.container input {
  width: 38px;
  height: 38px;
  line-height: 38px;
  outline-style: none;
  font-size: 16px;
  color: #333;
  border: 3px solid #a8a8a8;
  border-radius: 19px;
  padding: 0 16px;
  box-sizing: border-box;
  transition: all 0.4s ease-in-out;
}
.container .line {
  width: 3px;
  height: 14px;
  display: block;
  background-color: #a8a8a8;
  transform: rotate(320deg);
  position: absolute;
  left: 32px;
  top: 30px;
  z-index: 10;
  opacity: 1;
  transition: all 0.4s ease-in-out;
}
.container input:focus,
input:valid {
  width: 180px;
}
.container input:focus + .line,
input:valid + .line {
  width: 1px;
  height: 16px;
  left: 19px;
  top: 10px;
  opacity: 0;
  transform: rotate(360deg);
}

实现思路

<label> 作为容器盒子

利用 <input><span> 拼接出搜索图标的样式

选中输入框时,将 <span> 宽度变长,隐藏 <spna>,以实现动画效果

上次编辑于:
贡献者: ViewRoom