DIV CSS 佈局教程網

jQuery Tools 系列:tooltip 提示框插件詳解
編輯:JQuery特效代碼     

jQuery Tools 的 tooltip 插件能為表單元素加上優雅的提示框,非常適合用於表單說明與驗證提示。本文詳細記錄它的用法、配置參數與進階 API。

基本 HTML 結構

每個輸入框後面緊跟一個 class 為 tooltip 的 div 作為提示內容:

<form id="myform">
<h3>Registration Form</h3>
<label for="username">Username</label>
<input id="username" />
<div class="tooltip">Must be at least 8 characters.</div>
<label for="password">Password</label>
<input id="password" type="password" />
<div class="tooltip">Try to make it hard to guess.</div>
<!-- email、message 同理 -->
</form>

調用方式

該功能透過 $(...).tooltip() 實現,提供兩種方式:

1. $("#myform :input").tooltip() // 默認方式
2. $("#myform :input").tooltip({config object}) // 自定義配置

配置範例:

$("#myform :input").tooltip({
position:['center','right'],
offset:[-2,10],
effect:'fade',
fadeInSpeed:300,
delay:0,
opacity:1,
tip:"",
api:false,
onBeforeShow:function(){ /* this 代表即將顯示的 tooltip 物件 */ },
onShow:function(){},
onBeforeHide:function(){},
onHide:function(){}
});

主要配置參數

effect(默認 'slideup'):提示框出現與隱藏的方式,系統提供 toggle、fade、slideup 三種,也可自定義。
delay(默認 30):滑鼠離開觸發器後,提示框的顯示延遲時間。
offset(默認 [0, 0]):精確調整提示框位置。
opacity(默認 1):提示框透明度,0 為全透明、1 為不透明。
position(默認 ['top', 'center']):粗略設置提示框相對於觸發元素的位置。
tip:設置存放提示信息的頁面元素(jQuery 選擇器格式)。
api(默認 FALSE):設置返回類型,false 返回 jQuery 物件,否則返回 JS 物件。

提示框位置說明

位置由 position 與 offset 兩個屬性共同決定,兩者都用 JS 陣列存值。position 設置提示信息相對於觸發元素的位置(如 ['bottom','center'] 表示正下方居中);offset 則進一步精確偏移,如 [10,-20] 表示向下 10px、向左 20px。

內建 effect 說明

toggle:簡單的顯示/隱藏,無對應參數。
fade:逐漸顯示/隱藏,提供 fadeInSpeed 與 fadeOutSpeed(默認皆 200ms)。
slideup:向上/下滑動,提供 slideOffset(滑動距離)、slideInSpeed 與 slideOutSpeed。

獲取 tooltip 物件的方法

var tooltip=$("#myform input").tooltip(2);//取第 3 個 tooltip 物件
tooltip.show();//顯示
tooltip.hide();//隱藏
tooltip.isShown();//是否顯示中
tooltip.getTip();//轉為 jQuery 物件取得提示框
tooltip.getTrigger();//取得觸發器
tooltip.getConf();//取得配置物件

掌握這些參數與 API,你就能為表單打造既美觀又靈活的提示框,大幅提升填寫體驗。

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