天天看點

【每日一練】46—登入頁面表單輸入動畫效果的實作

【每日一練】46—登入頁面表單輸入動畫效果的實作

寫在前面

登入功能是我們在做網站時,一個非常高頻的功能需求。當然,也有很多部落格個人小站什麼的,不需要這個功能,一個功能是否需要,主要還是看我們産品的實際需求,需要我們就加上,不需要自然就是舍棄,不要覺得,這個功能别人有,我沒有,好像不行一樣,千萬不要這麼覺得。

但是,對于我們的日常練習,我們肯定是每個常用功能都會嘗試着去寫一寫,練一練,是以,今天我們就來練習一個登入頁面的效果,具體效果可以看下面的GIF動畫:

【每日一練】46—登入頁面表單輸入動畫效果的實作

下面就是具體的代碼實作過程:

HTML代碼:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>【每日一練】46—登入頁面表單輸入動畫效果的實作</title>
  </head>
  <body>
    <div class="container">
      <h1>請登入</h1>
      <form>
        <div class="form-control">
          <input type="text" required>
          <label>郵箱位址</label>
        </div>
        <div class="form-control">
          <input type="password" required>
          <label>登入密碼</label>
        </div>
        <button class="btn">登入</button>
        <p class="text">沒有賬戶 <a href="#">馬上注冊</a> </p>
      </form>
    </div>
  </body>
</html>      

CSS代碼:

* {
  box-sizing: border-box;
}


body {
  background-color: steelblue;
  color: #fff;
  font-family: 'Muli', sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  overflow: hidden;
  margin: 0;
}


.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: lightblue;
  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-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);
}      

JS代碼:

const labels = document.querySelectorAll('.form-control label')


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

寫在最後

以上就是每日一練的全部内容,希望今天的小練習對你有用,如果你覺得有幫助的話,請點贊我,并将它分享給你身邊做開發的朋友,也許能夠幫助到他。

我是楊小愛,我們明天見。

【每日一練】46—登入頁面表單輸入動畫效果的實作