DIV CSS 佈局教程網

qTip2:精緻的 jQuery 提示信息插件詳解
編輯:JQuery特效代碼     

qTip2 採用 MIT/GPLv2 許可,官方網站為 https://craigsworks.com/projects/qtip2/。它是一款功能豐富、外觀精緻的提示信息插件,支援多種資料來源與樣式擴展。

簡介與引入

若從 qTip 升級到 qTip2,可使用官方提供的轉換工具:https://craigsworks.com/projects/qtip2/converter/。若使用時遇到問題,可直接引入以下三個檔案:

<link href="https://craigsworks.com/projects/qtip2/packages/latest/jquery.qtip.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://craigsworks.com/projects/qtip2/packages/latest/jquery.qtip.min.js"></script>

從官方下載時可選擇想要的樣式與插件,可選插件包括:

  1. Ajax:請求遠程內容
  2. Tips:氣泡對話效果,如箭頭
  3. Modal:模態對話框效果
  4. Image map:對 map 內 area 標記提供提示支援
  5. SVG:對 SVG 元素提供提示支援
  6. BGIFrame:用於 IE6,可遮住 select 控制項

基本用法

最簡單的提示

$("#demo2").qtip({
content: "這是提示內容"
});

帶標題的提示

$("#demo3").qtip({
content: {
text: "這是提示內容",
title: "提示標題"
}
});

帶關閉按鈕

$("#demo3").qtip({
content: {
text: "這是提示內容",
title: {
text: "提示標題",
button: "關閉"
}
}
});

使用元素屬性作為提示

$("a[title]").qtip(); //從鏈接的title
$("img[alt]").qtip(); //從img的alt
$("div[title]").qtip(); //從div的title

使用 AJAX 請求遠程

$("#demo4").qtip({
content: {
text: "加載中...",
ajax: {
url: "lwmeAtCnblogs.aspx?name=囧月"
}
}
});

設置位置與樣式

$("#demo5").qtip({
position: {
my: 'bottom left',
at: 'top center'
},
style: {
classes: 'ui-tooltip-red'
}
});

模態對話框

$('button').qtip({
content: "這是提示內容",
show: {
event: 'click',
solo: true,
modal: true
},
hide: false
});

常用參數

qTip2 參數眾多,但使用頻率最高的主要有:

$.fn.qtip.defaults = {
content: {
text: true,
attr: 'title',
ajax: false,
title: {
text: false,
button: false
}
},
position: {
my: 'top left',
at: 'bottom right',
},
show: {
event: 'mouseenter',
solo: false,
ready: false,
modal: false
},
hide: {
event: 'mouseleave'
},
style: 'ui-tooltip-default'
};

顯示位置可設定的值:

my = ['top left', 'top right', 'top center', 'bottom left', 'bottom right', 'bottom center',
'right center', 'right top', 'right bottom', 'left center', 'left top', 'left bottom', 'center']
at = ['bottom left', 'bottom right', 'bottom center', 'top left', 'top right', 'top center',
'left center', 'left top', 'left bottom', 'right center', 'right top', 'right bottom', 'center']

色彩風格可選:

['red', 'blue', 'dark', 'light', 'green', 'jtools', 'plain', 'youtube', 'cluetip', 'tipsy', 'tipped']

例如 red 對應 ui-tooltip-red,預設為 default。另有 ui-tooltip-shadow(陰影)、ui-tooltip-rounded(圓角)可疊加使用:

$("#demo2").qtip({
content: "這是提示內容",
style: {
classes: 'ui-tooltip-red ui-tooltip-shadow ui-tooltip-rounded'
}
});

Ajax 主要參數與 jQuery.ajax 一致,需注意預設使用 GET 請求且啟用了快取。

更多資訊可參考官方網站、線上演示與官方文件。

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