DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> 基於jquery的氣泡提示效果
基於jquery的氣泡提示效果
編輯:JQuery特效代碼     
代碼注釋已經盡可能的詳細了,也不多說了.
初步測試暫未發現大的BUG,總體來說不滿意的是鼠標移來移去不斷觸發氣泡時會出現XX為空或不是對象的問題,
雖然不影響效果,但看著IE左下角的黃色警告不爽,暫時不知道如何改進. 加了try/catch解決...
還有就是氣泡默認出現在觸發對象的正上方,當觸發對象在邊上時,氣泡會有一部分出現在窗口外面......也許這種情況可以讓氣泡顯示在左邊或是右邊,感覺可能會有些麻煩,就沒去弄了,比較懶......
越用jquery就越喜歡用它...
bubble.js:
代碼如下:
/*
* @date: 2010-5-30 11:57:22
* @author: 胡靈偉
* Depends:
* jquery.js
*
* function:氣泡提示效果
* use:$("selectors").bubble({fn:getdata, width:width, height:height});
* 對所有需要氣泡提示效果的對象使用bubble方法,
* fn為氣泡中顯示內容獲得方法,即fn中返回的數據會顯示在氣泡中
* 以樣式指代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為每次調用add方法傳進來的參數,比如指定獲取數據的方法fn, 氣泡寬width高height
//console.debug("length:"+triggers.length);
var t = this.trigger.length;
//將新加入的需要氣泡提示效果的對象放到trigger數組中
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;
//為新加入的對象綁定鼠標監聽事件
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);
//當新觸發冒氣泡事件時原先的定時器還沒結束則將原來的定時器清除
if (obj.hideDelayTimer[i]) clearTimeout(obj.hideDelayTimer[i]);
if (obj.beingShown[i] || obj.shown[i]) {
//如果氣泡正在冒或已經冒出來了則不再重復冒氣泡
return;
} else {
var trigger = $(obj.trigger[i]);
//標記正在冒氣泡
obj.beingShown[i] = true;
//創建氣泡
obj.popup[i] = obj.makebubble(options.width||obj.options.width, options.height||obj.options.height);
//對於氣泡綁定同樣的事件以使得鼠標離開觸發對象後放到氣泡上時氣泡不會消失
obj.popup[i].mouseover(function(){obj.handleover(i, obj)}).mouseout(function(){obj.handleout(i, obj)});
//調用獲取數據的方法fn來顯示數據
obj.options.fn(obj.trigger[i], function(data){
obj.popup[i].find('.contents').text(data);
});
//設定氣泡的位置和顯示屬性,氣泡默認出現在觸發對象正上方
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不能同時支持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]);
//處理當因為某些意外操作使得沒有觸發鼠標進入事件而直接再次觸發鼠標離開事件時的情況
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只要返回一段字符串就可以了,就不貼了.
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved