<div class="inputBox"> <input class="effect-1" type="text" placeholder="Placeholder Text"> <span class="focus-border"></span> </div>
inputBox class類用於佈局,就是一個input容器,便於觀看,我這裏設置每一行分爲3個列(可根據本身需求設置)。css
.inputBox { float: left; width: 27.33%; margin: 40px 3%; position: relative; }
而後爲input元素設置一些通用樣式。html
input[type="text"]{ font: 15px/24px "Lato", Arial, sans-serif; color: #333; width: 100%; box-sizing: border-box; letter-spacing: 1px; } :focus{ outline: none; }
effect-1 class類就表明這不一樣的input表單獲取焦點的動畫樣式種類, effect-1就表明第一種,以此類推。佈局
.focus-border是輸入框聚焦後的邊框樣式。它採用絕對單位,位置在輸入框的左下角位置,高度爲2像素,開始時寬度被設置爲0,不可見。並設置了0.4秒的過渡動畫效果。 動畫
當輸入框聚焦時以及在輸入框中有內容時,將.focus-border的寬度設置爲100%。spa
.effect-3{ border: 0; padding: 7px 0; border-bottom: 1px solid #ccc; } .effect-1 ~ .focus-border{ position: absolute; bottom: 0; left: 0; width: 0; height: 2px; background-color: #3399FF; transition: 0.4s; } .effect-1:focus ~ .focus-border{ width: 100%; transition: 0.4s; }
<div class="inputBox"> <input class="effect-2" type="text" placeholder="Placeholder Text"> <span class="focus-border"></span> </div>
.effect-2{ border: 0; padding: 7px 0; border-bottom: 1px solid #ccc; } .effect-2 ~ .focus-border{ position: absolute; bottom: 0; left: 50%; width: 0; height: 2px; background-color: #3399FF; transition: 0.4s; } .effect-2:focus ~ .focus-border{ width: 100%; transition: 0.4s; left: 0; }
<div class="inputBox"> <input class="effect-3" type="text" placeholder="Placeholder Text"> <span class="focus-border"></span> </div>
.effect-3{ border: 0; padding: 7px 0; border-bottom: 1px solid #ccc; } .effect-3 ~ .focus-border{ position: absolute; bottom: 0; left: 0; width: 100%; height: 2px; z-index: 99; } .effect-3 ~ .focus-border:before, .effect-3 ~ .focus-border:after{ content: ""; position: absolute; bottom: 0; left: 0; width: 0; height: 100%; background-color: #3399FF; transition: 0.4s; } .effect-3 ~ .focus-border:after{ left: auto; right: 0; } .effect-3:focus ~ .focus-border:before, .effect-3:focus ~ .focus-border:after{ width: 50%; transition: 0.4s; }
搬你想搬,蓋你所需,碼字不易,且行且珍惜!code