主要思路:用js将标签内的元素拆分并将每一个元素都生成一个个新的span标签设置不同的过渡延迟返回,特别注意span标签要更改成块级标签,不然没法transform

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Form Input Wave</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="container">
    <h1>Please Login</h1>
    <form>
      <div class="form-control">
        <input type="text" required>
        <label>Email</label>
      </div>

      <div class="form-control">
        <input type="password" required>
        <label>Password</label>
      </div>

      <button class="btn">Login</button>

      <p class="text">Don't have an account? <a href="#">Register</a></p>
    </form>
  </div>

  <script src="script.js"></script>
</body>
</html>
*{
  box-sizing: border-box;
}

body{
  background-color: steelblue;
  color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  margin: 0;
  overflow: hidden;
}

.container{
  background-color: rgba(0, 0, 0, 0.4);
  padding: 20px 40px;
  border-radius: 5px;
}

.container h1{
  text-align: center;
  margin-bottom: 30px;
}

.container a{
  text-decoration: none;
  color: lightblue;
}

.btn{
  cursor: pointer;
  display: inline-block;
  width: 100%;
  background-color: lightblue;
  padding: 15px;
  font-size: 16px;
  border: 0;
  border-radius: 5px;
}
.btn:focus{
  outline: 0;
}
.btn:active{
  transform: scale(0.98);
}

.text{
  margin-top: 30px;
}

.form-control{
  position: relative;
  margin: 20px 0 40px;
  width: 300px;
}

.form-control input{
  background-color: transparent;
  border: 0;
  border-bottom: 2px #fff solid;
  display: block;
  width: 100%;
  padding: 15px 0;
  font-size: 18px;
  color: #fff;
}
.form-control input:focus,
.form-control input:valid{
  outline: 0;
  border-bottom-color: lightblue;
}

.form-control label {
  position: absolute;
  top: 15px;
  left: 0;
  pointer-events: none;
}

.form-control label span{
  display: inline-block;
  font-size: 18px;
  min-width: 5px;
  transition: 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.form-control input:focus + label span,
.form-control input:valid + label span{
  color: lightblue;
  transform: translateY(-30px);
}
const labels = document.querySelectorAll('.form-control label')

labels.forEach(label => {
  label.innerHTML = label.innerText
      .split('')
      .map((letter, index) => `<span style="transition-delay:${index*50}ms">${letter}</span>`)
      .join('')
})

示例如下
asfsdfsdgcvhnkhgjdfg20220615103317

Q.E.D.


一个二次元web开发咸鱼