這些天忙於公司項目的改版就很少來這里發表文章了,今天趁着周末休息給大家分享一個jQuery提示控件。可用於顯示加載提示、錯誤提示、操作提示等。
先上張預覽圖:
提示條樣式可以自己定義,支持關閉回調和鎖屏,自適應居中,采用fixed定位(暫未考慮兼容IE6)。
下面是源碼:
1 /** 2 * tooltip提示 3 * @author Newton---承諾ン祗愛 4 * @date 2012年04月19日晚 5 * @update 2012年04月23日重構,保證一個實例的關閉函數只能觸發自身的關閉事件,加入動畫緩動支持。 6 * @param object{} 7 * @type string tip: '', 提示內容,支持傳入html。 8 * @type number time: 3, 自動關閉時間,以秒計時。 9 * @type boolean lock: false, 鎖屏。 10 * @type string easing: 'linear' 動畫緩動效果,需要緩動插件支持。 11 * @type string maskColor: '#000', 鎖屏顏色。 12 * @type number maskOpacity: .3, 鎖屏透明度。 13 * @type number fxSpeed: 300, 動畫速度,不建議設置過大,以毫秒計時。 14 * @type number index: 999999, z-index值。 15 * @type function afterClose: $.noop 關閉后回調。 16 */ 17 (function ($){ 18 //提示條外層包裹 19 var tooltipWrap = $([ 20 ' 21 <div style="position: fixed; top:-100%; left: 50%; margin: 0; padding: 0;">', 22 ' 23 <div style="position: relative; top:0; right: 50%; margin: 0; padding: 0;"></div> 24 25 ', 26 '</div> 27 28 ' 29 ].join('')).appendTo(document.body); 30 31 //鎖屏 32 var lockMask = $(' 33 <div style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; display: none;"></div> 34 35 ').appendTo(document.body); 36 37 //內容包裹 38 var tootipInner = tooltipWrap.children(); 39 40 //計時器id 41 var timer = null; 42 43 //實例堆棧 44 var instanceStack = null; 45 46 //默認參數 47 var defaults = { 48 tip: '', 49 time: 3, 50 fxSpeed: 300, 51 index: 999999, 52 lock: false, 53 easing: 'linear', 54 maskOpacity: .2, 55 maskBackground: '#000', 56 afterClose: $.noop 57 }; 58 59 //接口 60 var tooltip = function (config){ 61 //模仿靜態方法,不需要用new初始化 62 if (!(this instanceof tooltip)) { 63 return new tooltip(config); 64 } 65 this.config = $.extend({}, defaults, config); 66 this.config.time = this.config.time * 1000; 67 this._initialize(); 68 return this; 69 }; 70 71 //原型方法 72 tooltip.prototype = { 73 //初始化函數 74 _initialize: function (){ 75 clearTimeout(timer); 76 if (instanceStack !== null && instanceStack !== this) instanceStack.config.afterClose(); 77 instanceStack = this; 78 tooltipWrap.css('z-index', this.config.index); 79 lockMask.css({ 80 zIndex: this.config.index - 1, 81 opacity: this.config.maskOpacity, 82 background: this.config.maskBackground 83 }); 84 this._toggleMask(); 85 tootipInner.html(this.config.tip); 86 this._slideDown(); 87 }, 88 //鎖屏 89 _locked: function (){ 90 lockMask.fadeIn(this.config.fxSpeed); 91 }, 92 //關閉鎖屏 93 _unlocked: function (){ 94 lockMask.fadeOut(this.config.fxSpeed); 95 }, 96 //顯示隱藏鎖屏 97 _toggleMask: function (){ 98 if (this.config.lock) { 99 this._locked(); 100 } else { 101 this._unlocked(); 102 } 103 }, 104 //提示條滑下 105 _slideDown: function (){ 106 var t = this; 107 tooltipWrap.stop(true, false).animate({ 108 top: 0 109 }, this.config.fxSpeed, this.config.easing, function (){ 110 t._autoClose(); 111 }); 112 }, 113 //提示條收起 114 _slideUp: function (){ 115 //保證只有當前實例才能執行關閉操作 116 if (instanceStack !== this) return; 117 var t = this; 118 this._unlocked(); 119 tooltipWrap.animate({ 120 top: -tooltipWrap.height() 121 }, this.config.fxSpeed, this.config.easing, function (){ 122 instanceStack = null; 123 t.config.afterClose(); 124 }); 125 }, 126 //自動關閉 127 _autoClose: function (){ 128 var t = this; 129 timer = setTimeout(function (){ 130 clearTimeout(timer); 131 t._slideUp(); 132 }, this.config.time); 133 }, 134 //關閉接口 135 close: function (){ 136 this._slideUp(); 137 } 138 }; 139 140 //公開接口 141 window.tooltip = tooltip; 142 })(jQuery);
調用方法(依賴jQuery,每個參數的意義注釋里都有,不多解釋了):
1 tooltip({ 2 tip: '消息', 3 lock: true, 4 time: 6, 5 afterClose: function(){ 6 alert('我關閉了!'); 7 } 8 });
更新日志: 2012年04月23日:詳細請見注釋。 示例下載: tooltip.rar(前端組-Newton)