DIV CSS 佈局教程網

基於 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 為獲取氣泡內容的方法,其返回值會顯示在氣泡中
* width、height 分別對應 contents 的寬與高
* 氣泡總寬 = left.width + contents.width + right.width
* 氣泡總高 = 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){
      var t = this.trigger.length;
      for(var j =0;j<triggers.length;j++)
        this.trigger.push(triggers[j]);
      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){
      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)});
        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(
        $.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){
      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,注意路徑,缺圖會很難看)。核心樣式如下:

.bubble-popup { position: absolute; display: none; z-index: 50; border-collapse: collapse; }
.bubble-popup .contents {
  white-space:normal;
  word-break:break-all;
  float:left;
  font-size: 12px;
  line-height: 1.2em;
  background-color: #fff;
  color: #666;
}

初始化調用示例:

aa = function(obj, callback){
  $.ajax({
    type : 'POST',
    data : {word:$(obj).attr('alt'),rand:Math.random()},
    url : 'https://localhost/xun/ajax.svl?method=getdetailinfo',
    dataType : 'text',
    timeout : 1000,
    success : function(data){ callback(data); }
  });
};
$(function(){
  $('.bubbletrigger').bubble({width:150, height: 100, fn:aa});
  $('#a').bubble({fn:bb});
});

後端 servlet 只需返回一段字符串即可,這裡就不再貼出。

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