網站中比較不錯的表單特效執行個體代碼

來源:互聯網
上載者:User
今天來說說對input輸入框在處理上的細節處理和心得,其實製作一個符合CSS標準、FF/IE7/IE6等主流瀏覽器全相容、符合使用者體驗的input其實並不難。先點擊看看下面的效果先!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>表單效果</title><style type="text/css">*{margin:0;padding:0;}body{font-size:63%;color:#000;}/*input*/.input_on{padding:2px 8px 0pt 3px;height:18px;border:1px solid #999;background-color:#FFFFCC;}.input_off{padding:2px 8px 0pt 3px;height:18px;border:1px solid #CCC;background-color:#FFF;}.input_move{padding:2px 8px 0pt 3px;height:18px;border:1px solid #999;background-color:#FFFFCC;}.input_out{/*height:16px;預設高度*/padding:2px 8px 0pt 3px;height:18px;border:1px solid #CCC;background-color:#FFF;}/*form*/ul.input_test{margin:20px auto 0 auto;width:500px;list-style-type:none;}ul.input_test li{width:500px;height:22px;margin-bottom:10px;}.input_test label{float:left;padding-right:10px;width:100px;line-height:22px;text-align:right;font-size:1.4em;}.input_test p{float:left;_margin-top:-1px;}.input_test span{float:left;padding-left:10px;line-height:22px;text-align:left;font-size:1.2em;color:#999;}</style></head><body><ul class="input_test"><li><label for="inp_name">姓名:</label><p><input id="inp_name" class="input_out" name="" type="text" onfocus="this.className='input_on';this.onmouseout=''" onblur="this.className='input_off';this.onmouseout=function(){this.className='input_out'};" onmousemove="this.className='input_move'" onmouseout="this.className='input_out'" /></p><span>請輸入您的姓名</span></li><li><label for="inp_email">Email:</label><p><input id="inp_email" class="input_out" name="" type="text" onfocus="this.className='input_on';this.onmouseout=''" onblur="this.className='input_off';this.onmouseout=function(){this.className='input_out'};" onmousemove="this.className='input_move'" onmouseout="this.className='input_out'" /></p><span>請輸入您的Email</span></li><li><label for="inp_web">網站:</label><p><input id="inp_web" class="input_out" name="" type="text" onfocus="this.className='input_on';this.onmouseout=''" onblur="this.className='input_off';this.onmouseout=function(){this.className='input_out'};" onmousemove="this.className='input_move'" onmouseout="this.className='input_out'" /></p><span>請輸入您的網站</span></li></ul></body></html>

滑鼠經過input時的顏色會發生變化,此外當點擊標題處(<label>的作用)或者輸入框時,游標停留所在的input的顏色也和其他input輸入框有所不同,這是<input>中JS的作用。在使用者體驗上告訴的使用者什麼是可以輸入以及當前在什麼輸入位置。此外通過鍵盤上Tab鍵的切換,輸入完當前內容移動到下一個輸入框變得更方便了,這是CSS合理布局結構的作用。

整體的結構通過<ul>和<li>來組織,每個<li>顯示一行內容。<label>標籤顯示標題,<p>input控制輸入框,<span>顯示備忘資訊。這裡要特別說一下<input>在各個瀏覽器下不同的表現,對<input>設定line-height對FF是不起作用的,所以建議用padding來控制文本在輸入框的位置。<input>在瀏覽器下的預設高度和字型一樣是16px,加上下邊框就是18px。特別是在需要將<input>變大的情況下,用padding來控制比較好。

再來說說JS部分,這裡用到onblur(游標離開)、onfocus(游標停留)、onmousemove(滑鼠停留)、onmouseout(滑鼠離開)這4個屬性來控制滑鼠的動作。不會JS也沒關係,只要定義其所對應的CSS樣式就可以了。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.