DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5特效代碼
CSS3 動畫登入表單:完整程式碼與原理解析
編輯:HTML5特效代碼     

登入表單是使用者進入網站的第一道門,只要在小細節上多下點功夫,就能大幅提升第一印象。本文示範的這組表單,僅靠 CSS3 就完成了內凹輸入框、圓形箭頭按鈕,以及滑鼠移入時旋轉 90 度的過場效果,完全不需要 JavaScript,也不必準備任何圖片素材。

點擊這裡查看效果

這個表單做了哪些事

輸入框以 box-shadow: inset 模擬凹陷的質感,搭配半透明的 rgba(0,0,0,.1) 背景,讓深色底上的欄位看起來像是嵌進去的一樣。首尾兩個 li 用 :after 偽元素各畫一個半圓,營造出金屬螺絲般的裝飾;背景則是一張 1×2 像素的 base64 內嵌 PNG 細紋,用來避免純色過於單調。

右側的圓形按鈕靠 -webkit-transition 與 -webkit-transform: rotate(90deg) 完成旋轉動畫,箭頭圖示則由 FontAwesome 字型提供,因此不需要切圖。

完整原始碼

以下為可直接存檔執行的完整程式碼:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>CSS3 動畫登入表單</title>
<link rel="stylesheet" href="assets/font-awesome/css/font-awesome.css" />
<style type="text/css">
body, ul { margin: 0; padding: 0; }
a { color: rgb(1, 124, 185); text-decoration: none; }
input:focus { outline: none 0; }
body { color: #b5b5b5; font: 14px 'Arial'; }
/* 背景細紋使用 1x2 像素的 base64 內嵌 PNG,這裡以純色代替 */
body { background-image: url(你的細紋圖.png); }
.box { position: relative; width: 384px; height: 140px; margin: 50px auto; }
.box li { list-style-type: none; margin-bottom: 10px; border-radius: 5px; overflow: hidden; position: relative; height: 42px; }
.box li input {
box-shadow: inset 0 0 5px rgba(0,0,0,.5), -1px 1px 0 rgba(255,255,255,.05);
border: 0 none;
padding: 8px 5px 5px;
border-radius: 5px;
width: 300px;
height: 28px;
box-sizing: content-box;
background: rgba(0,0,0,.1);
color: #fff;
}
.box li:first-child:after,
.box li:last-child:after {
position: absolute;
width: 50px;
height: 50px;
content: "";
border-radius: 25px;
z-index: 2;
right: -23px;
box-shadow: 0 0 8px rgba(0,0,0,.5);
}
::-webkit-input-placeholder { color: #fff; font-weight: bold; }
.box li:first-child:after { top: 15px; }
.box li:last-child:after { bottom: 15px; }
.box label { width: 70px; display: inline-block; text-align: right; }
.box span { display: block; color: #6296b4; padding-left: 75px; }
.button {
position: absolute;
top: 24px;
right: -30px;
width: 44px;
height: 44px;
border-radius: 22px;
border: 1px solid #00a1d2;
background: -webkit-linear-gradient(top, #029ecd, #0d7796);
color: #fff;
text-shadow: 1px 1px 0 #666;
box-shadow: 0 0 0 5px #2c2c2c;
z-index: 3;
text-align: center;
line-height: 46px;
-webkit-transition: all 0.28s ease-in;
}
.button:hover { -webkit-transform: rotate(90deg); }
.icon-arrow-right:before {
font-family: FontAwesome;
font-size: 26px;
content: "\f061";
}
</style>
</head>
<body>
<div class="box">
<form action="#" method="get">
<ul>
<li>
<label for="username">Email:</label>
<input type="text" id="username" class="username" placeholder="hello@" />
</li>
<li>
<label for="password">Password:</label>
<input type="password" id="password" class="password" placeholder="******" />
</li>
</ul>
<a class="button" href="javascript:;"><i class="icon-arrow-right"></i></a>
</form>
</div>
</body>
</html>

實作小提醒

原始範例大量使用 -webkit- 前綴,因此在 Chrome、Safari 上效果最完整;若要在 Firefox、Opera 也能得到同樣的旋轉動畫,記得補上 -moz-transition、-o-transition 與標準的 transition。另外 placeholder 的樣式同樣有 ::-moz-placeholder 等版本,跨瀏覽器時建議一併寫入。

由於表單只是視覺示範,實際上線時仍須加上後端驗證與 CSRF 防護,別把安全機制交給前端樣式。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved