DIV CSS 佈局教程網

用 jQuery 實作元素淡入淡出效果
編輯:JQuery特效代碼     

淡入淡出比硬生生的顯示/隱藏柔和得多。fadeIn() 與 fadeOut() 兩個方法就能搞定,還能帶入速度參數控制過渡快慢。

樣式

<style type="text/css">
  #win {
    width: 98%;
    position: absolute;
    display: none;   /* 初始隱藏,等 fadeIn 顯示 */
    float: left;
  }
  /* 控制關閉按鈕的位置 */
  #close {
    margin-left: 155px;
    cursor: pointer;
  }
</style>

腳本

<script type="text/javascript">
  function showwin() { $("#win").fadeIn("slow"); }
  function hide()    { $("#win").fadeOut("slow"); }
</script>

"slow" 約等於 600ms,"fast" 約 200ms,也可以直接給毫秒數如 fadeIn(300)。

HTML 結構

<div id="win">
  <div id="title">
    <table style="width:100%; background:#78b3ef" cellpadding="4" cellspacing="1">
      <tr>
        <td>ID</td><td>網站名</td><td>域名</td>
        <td>百度收錄</td><td>是否存在鏈接</td>
        <td>操作 <span id="close" onclick="hide()">關閉</span></td>
      </tr>
      <tr style="background:#FFFFFF;">
        <td><span id="w_id"></span></td>
        <td><input id="txt_title" type="text" style="width: 237px" /></td>
        <td><input id="txt_classurl" type="text" style="width: 259px" /></td>
        <td><span id="w_baidu"></span></td>
        <td><input id="txt_exist" type="text" style="width: 83px" /></td>
        <td><input id="Button1" type="button" value="修改" onclick="w_modify()" /></td>
      </tr>
      <tr style="background:#FFFFFF;"><td colspan="6"><a href="Default.aspx">返回首頁</a></td></tr>
    </table>
  </div>
</div>

可以再加強的地方

  • 帶入第二個參數作為 complete 回呼,就能在動畫結束後接著做事:$("#win").fadeIn("slow", function () { $("#txt_title").focus(); })。
  • 淡入淡出只改變透明度,元素在 display: none 期間仍參與 Accessibility 樹的計算方式依瀏覽器而異。若內容是可互動表單,記得處理焦點順序。
  • 頻繁切換時動畫會堆疊,可在使用前先 $("#win").stop(true, true) 清掉佇列。
  • onclick 內聯寫法可以改成用 jQuery 綁定,讓結構與行為分離。

這個「淡入的編輯面板」是很實用的模式:不佔用常駐版面,需要時才出現,也不打斷使用者的閱讀位置。

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