Form Animation

Form Animation

We are going to build a simple login form here and add a wave affect on the label using javaScript

let’s see how

HTML

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

        <div class="form-section">
          <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="main.js"></script>
  </body>
</html>

You can add more input and label type.


CSS

@import url("https://fonts.googleapis.com/css2?family=Chango&display=swap");

* {
  box-sizing: border-box;
}

body {
  background-color: #ccfff7;
  font-family: "Chango", cursive;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  height: 100vh;
  margin: 0;
}

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

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

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

.btn {
  cursor: pointer;
  display: inline-block;
  width: 100%;
  background: lightseagreen;
  padding: 15px;
  font-family: inherit;
  font-size: 16px;
  border: 0;
  border-radius: 5px;
}

.btn:focus {
  outline: 0;
}

.btn:active {
  transform: scale(0.98);
}

.text {
  margin-top: 30px;
}

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

.form-section input {
  background: transparent;
  border: 0;
  border-bottom: 4px #fff solid;
  display: block;
  width: 100%;
  padding: 15px 10;
  font-size: 20px;
  color: #fff;
}

.form-section input:focus,
.form-section input:valid {
  outline: 0;
  border-bottom-color: lightseagreen;
}

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

.form-section 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-section input:focus + label span,
.form-section input:valid + label span {
  color: lightblue;
  transform: translateY(-30px);
}

This is a CSS heavy project as we will be using css animation properties. You can choose your own font, styles and ideas.

JavaScript

const labels = document.querySelectorAll(".form-section label");

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

We have given a simple animation by getting hold of the form-section label with document.querySelectorAll(). Rest is quite self explanatory.


If you find this blog helpful. Please like and share

Thank You

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: