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>
從官方下載時可選擇想要的樣式與插件,可選插件包括:
$("#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
$("#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 請求且啟用了快取。
更多資訊可參考官方網站、線上演示與官方文件。