基于jquery的氣泡提示效果
初步測試暫未發(fā)現(xiàn)大的BUG,總體來說不滿意的是鼠標(biāo)移來移去不斷觸發(fā)氣泡時(shí)會(huì)出現(xiàn)XX為空或不是對象的問題,
雖然不影響效果,但看著IE左下角的黃色警告不爽,暫時(shí)不知道如何改進(jìn). 加了try/catch解決...
還有就是氣泡默認(rèn)出現(xiàn)在觸發(fā)對象的正上方,當(dāng)觸發(fā)對象在邊上時(shí),氣泡會(huì)有一部分出現(xiàn)在窗口外面......也許這種情況可以讓氣泡顯示在左邊或是右邊,感覺可能會(huì)有些麻煩,就沒去弄了,比較懶......
越用jquery就越喜歡用它...
bubble.js:
/*
* @date: 2010-5-30 11:57:22
* @author: 胡靈偉
* Depends:
* jquery.js
*
* function:氣泡提示效果
* use:$("selectors").bubble({fn:getdata, width:width, height:height});
* 對所有需要?dú)馀萏崾拘Ч膶ο笫褂胋ubble方法,
* fn為氣泡中顯示內(nèi)容獲得方法,即fn中返回的數(shù)據(jù)會(huì)顯示在氣泡中
* 以樣式指代div則有:
* width\height為contents的width\height屬性
* 氣泡總width為left.width + contents.width + right.width
* 氣泡總height為top.height + contents.height + bottom.height
*/
(function ($) {
$.fn.bubble = function (options) {
Bubble = function(){
this.defaults = {
distance : 10,
time : 250,
hideDelay : 500,
width:100,
height:100
};
this.options = $.extend(this.defaults, options);
this.hideDelayTimer = new Array();
this.shown = new Array();
this.beingShown = new Array();
this.popup = new Array();
this.trigger = new Array();
this.makebubble = function(w, h){
var tpl = $('<div class="bubble-popup"></div>').append('<div class="topleft"></div>').append('<div class="top"></div>')
.append($('<div class="topright"></div>')).append('<div class="left"></div>')
.append('<div class="contents"></div>').append('<div class="right"></div>')
.append('<div class="bottomleft"></div>')
.append($('<div class="bottom"></div>')
.append($('<div class="bottomtail"></div>')))
.append('<div class="bottomright"></div>').appendTo('body');
tpl.find('.left, .right, .contents').each(function(){$(this).height(h)});
tpl.find('.top, .bottom, .contents').each(function(){$(this).width(w)});
return tpl;
};
this.add = function(triggers, options){
//此處的options為每次調(diào)用add方法傳進(jìn)來的參數(shù),比如指定獲取數(shù)據(jù)的方法fn, 氣泡寬width高h(yuǎn)eight
//console.debug("length:"+triggers.length);
var t = this.trigger.length;
//將新加入的需要?dú)馀萏崾拘Ч膶ο蠓诺絫rigger數(shù)組中
for(var j =0;j<triggers.length;j++)
this.trigger.push(triggers[j]);
//console.debug("trigger.length:" + this.trigger.length);
var hout = this.handleout;
var hover = this.handleover;
var obj = this;
//為新加入的對象綁定鼠標(biāo)監(jiān)聽事件
triggers.each(function(ind){
$(this).unbind('mouseover').mouseover(function(){
hover(t + ind, obj, options);
}).unbind('mouseout').mouseout(function(){
hout(t + ind, obj, options);
});
});
};
this.handleover = function(i, obj, options){
//console.debug("hideDelayTimer.length:" + obj.hideDelayTimer.length);
//當(dāng)新觸發(fā)冒氣泡事件時(shí)原先的定時(shí)器還沒結(jié)束則將原來的定時(shí)器清除
if (obj.hideDelayTimer[i]) clearTimeout(obj.hideDelayTimer[i]);
if (obj.beingShown[i] || obj.shown[i]) {
//如果氣泡正在冒或已經(jīng)冒出來了則不再重復(fù)冒氣泡
return;
} else {
var trigger = $(obj.trigger[i]);
//標(biāo)記正在冒氣泡
obj.beingShown[i] = true;
//創(chuàng)建氣泡
obj.popup[i] = obj.makebubble(options.width||obj.options.width, options.height||obj.options.height);
//對于氣泡綁定同樣的事件以使得鼠標(biāo)離開觸發(fā)對象后放到氣泡上時(shí)氣泡不會(huì)消失
obj.popup[i].mouseover(function(){obj.handleover(i, obj)}).mouseout(function(){obj.handleout(i, obj)});
//調(diào)用獲取數(shù)據(jù)的方法fn來顯示數(shù)據(jù)
obj.options.fn(obj.trigger[i], function(data){
obj.popup[i].find('.contents').text(data);
});
//設(shè)定氣泡的位置和顯示屬性,氣泡默認(rèn)出現(xiàn)在觸發(fā)對象正上方
obj.popup[i].css({
top: trigger.offset().top-obj.popup[i].height(),
left: trigger.offset().left + trigger.width()/2 - obj.popup[i].width()/2,
display: 'block'
}).animate(
//由于萬惡的IE不能同時(shí)支持PNG半透明和濾鏡,所以對于IE不使用濾鏡
$.browser.msie?{
top: '-=' + obj.options.distance + 'px'
}:{
top: '-=' + obj.options.distance + 'px',
opacity: 1
}, obj.options.time, 'swing', function() {
obj.beingShown[i] = false;
obj.shown[i] = true;
});
}
return false;
};
this.handleout = function(i, obj, options){
//console.debug("hideDelayTimer["+i+"]:"+obj.hideDelayTimer[i]);
//處理當(dāng)因?yàn)槟承┮馔獠僮魇沟脹]有觸發(fā)鼠標(biāo)進(jìn)入事件而直接再次觸發(fā)鼠標(biāo)離開事件時(shí)的情況
if (obj.hideDelayTimer[i]) clearTimeout(obj.hideDelayTimer[i]);
obj.hideDelayTimer[i] = setTimeout(function () {
obj.hideDelayTimer[i] = null;
try{
obj.popup[i].animate(
$.browser.msie?{
top: '-=' + obj.options.distance + 'px'
}:{
top: '-=' + obj.options.distance + 'px',
opacity: 0//漸隱效果
}, obj.options.time, 'swing', function () {
obj.shown[i] = false;
obj.popup[i].css('display', 'none');
obj.popup[i] = null;
});}catch(e){};
}, obj.options.hideDelay);
return false;
};
};
$.bubble = new Bubble();//單例
$.bubble.add(this, options);
};
})(jQuery);
使用方法:(用到的圖片樣式img.zip,注意路徑,沒圖片是很難看的...)
<style type="text/css" media="screen">
<!--
* {
margin: 0;
padding: 0;
}
body {
padding: 10px;
}
h1 {
margin: 14px 0;
font-family: 'Trebuchet MS', Helvetica;
}
.bubbletrigger {
}
/* Bubble pop-up */
.bubble-popup {
position: absolute;
display: none;
z-index: 50;
border-collapse: collapse;
}
.bubble-popup .topleft {width:19px; height:15px;float:left;background-image: url(../images/bubble/bubble-1.png);}
.bubble-popup .top { width:1px;height:15px;float:left;background-image: url(../images/bubble/bubble-2.png); }
.bubble-popup .topright { width:19px; height:15px;float:left;background-image: url(../images/bubble/bubble-3.png); }
.bubble-popup .left { clear:left;width:19px; height:1px;float:left;background-image: url(../images/bubble/bubble-4.png); }
.bubble-popup .contents {
white-space:normal;
word-break:break-all;
float:left;
font-size: 12px;
line-height: 1.2em;
background-color: #fff;
color: #666;
font-family: "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", sans-serif;
}
.bubble-popup .right { width:19px; height:1px;float:left;background-image: url(../images/bubble/bubble-5.png); }
.bubble-popup .bottomleft { clear:left;width:19px; height:15px;float:left;background-image: url(../images/bubble/bubble-6.png); }
.bubble-popup .bottom {width:1px;height:15px;float:left;background-image: url(../images/bubble/bubble-7.png); text-align: center;}
.bubble-popup .bottomtail { width:30px; height:29px; display: block; margin: 0 auto; background-image: url(../images/bubble/bubble-tail2.png);}
.bubble-popup .bottomright { width:19px; height:15px;float:left;background-image: url(../images/bubble/bubble-8.png); }
-->
</style>
<script src="../js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="../js/bubble-1.0.js" type="text/javascript"></script>
<script type="text/javascript"><!--
aa = function(obj, callback){
$.ajax({
type : 'POST',
data : {word:$(obj).attr('alt'),rand:Math.random()},
url : 'http://localhost/xun/ajax.svl?method=getdetailinfo',
dataType : 'text',
timeout : 1000,
success : function(data){
callback(data);
}
});
};
bb = function(obj, callback){
$.ajax({
type : 'POST',
data : {word:$(obj).attr('alt'),rand:Math.random()},
url : 'http://localhost/xun/ajax.svl?method=getdetailinfo',
dataType : 'text',
timeout : 1000,
success : function(data){
callback(data + "aaaa");
}
});
};
$(function(){
$('.bubbletrigger').bubble({width:150, height: 100, fn:aa});
$('#a').bubble({fn:bb});
});
//
--></script>
</head>
<body id="page">
<h1>jQuery Bubble Example</h1>
<div>
<br/>aaaaaaaaaa
<br/>aaaaaaaaaaaaaaaaaaaa
<br/>aaaaaaaaaaaaaaaaaaaaaaaaaaaa
<br/>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
<br/>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
<br/>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
</div>
<div style="padding-left:100px;">
<img class="bubbletrigger" alt="a" src="../images/bubble/starburst.gif" />
<img class="bubbletrigger" alt="b" src="../images/bubble/starburst.gif" />
<img class="bubbletrigger" alt="c" src="../images/bubble/starburst.gif" />
<img class="bubbletrigger" alt="d" src="../images/bubble/starburst.gif" />
<img id="a" alt="e" src="../images/bubble/starburst.gif" />
</div>
</body>
servlet只要返回一段字符串就可以了,就不貼了.
- 使用jQuery UI的tooltip函數(shù)修飾title屬性的氣泡懸浮框
- JQuery實(shí)現(xiàn)簡單時(shí)尚快捷的氣泡提示插件
- jquery.cvtooltip.js 基于jquery的氣泡提示插件
- Jquery插件分享之氣泡形提示控件grumble.js
- jQuery插件HighCharts實(shí)現(xiàn)氣泡圖效果示例【附demo源碼】
- jQuery bt氣泡實(shí)現(xiàn)懸停顯示及移開隱藏功能的方法
- jquery實(shí)現(xiàn)鼠標(biāo)滑過顯示提示框的方法
- 基于JQuery 的消息提示框效果代碼
- Jquery實(shí)現(xiàn)鼠標(biāo)移上彈出提示框、移出消失思路及代碼
- jquery懸浮提示框完整實(shí)例
- jQuery實(shí)現(xiàn)鼠標(biāo)放置名字上顯示詳細(xì)內(nèi)容氣泡提示框效果的方法分析
相關(guān)文章
基于jQuery實(shí)現(xiàn)圖片的前進(jìn)與后退功能
前進(jìn)與后退在瀏覽網(wǎng)頁的時(shí)候是比較常見的而且非常的實(shí)用,接下來為大家分享下使用jQuery實(shí)現(xiàn)圖片的前進(jìn)與后退,感興趣的朋友可以參考下哈2013-04-04jQuery動(dòng)態(tài)生成Bootstrap表格
這篇文章主要介紹了jQuery動(dòng)態(tài)生成bootstrap表格的相關(guān)資料,非常不錯(cuò)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11jquery和雅虎的yql服務(wù)實(shí)現(xiàn)天氣預(yù)報(bào)服務(wù)示例
本文介紹一個(gè)利用Jquery和雅虎的YQL服務(wù)實(shí)現(xiàn)天氣預(yù)報(bào)功能,需要的朋友可以參考下2014-02-02jquery實(shí)現(xiàn)在頁面加載的時(shí)自動(dòng)為日期插件添加當(dāng)前日期
這篇文章主要介紹了通過jquery實(shí)現(xiàn)在頁面加載的時(shí)自動(dòng)為日期插件添加當(dāng)前日期,需要的朋友可以參考下2014-08-08jquery實(shí)現(xiàn)ajax提交表單信息的簡單方法(推薦)
下面小編就為大家?guī)硪黄猨query實(shí)現(xiàn)ajax提交表單信息的簡單方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08jQuery.prototype.init選擇器構(gòu)造函數(shù)源碼思路分析
jQuery的核心思想可以簡單概括為“查詢和操作dom”,今天主要是分析一下jQuery.prototype.init選擇器構(gòu)造函數(shù),處理選擇器函數(shù)中的參數(shù),感興趣的朋友可以了解下,或許本文所提供的知識對你學(xué)習(xí)有所幫助2013-02-02jquery+css實(shí)現(xiàn)絢麗的橫向二級下拉菜單-附源碼下載
這篇文章主要介紹了jquery+css實(shí)現(xiàn)絢麗的橫向二級下拉菜單-附源碼下載,需要的朋友可以參考下2015-08-08jQuery編輯器KindEditor4.1.4代碼高亮顯示設(shè)置教程
接下來介紹下編輯器KindEditor4.1.4代碼高亮顯示設(shè)置:加載需要的JS和CSS文件/編輯器初始化設(shè)置后,在里面加prettyPrint等等,感興趣的你可以參考下本文2013-03-03jquery如何實(shí)現(xiàn)點(diǎn)擊空白處隱藏元素
這篇文章主要介紹了jquery實(shí)現(xiàn)點(diǎn)擊空白處隱藏元素div等方法和實(shí)現(xiàn)代碼的講解,一起學(xué)習(xí)下。2017-12-12