jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
 * jQuery alert plugin
 * Version 1.0.3  (11/27/2009)
 * @requires jQuery v1.2.6+
 *
 */
;(function($) {
/**
* @name alert
* @type jQuery
* @cat Plugins/alerttip
* @return jQuery
* @author   li xiangyang <lxy19791111@163.com>
* @param    options 可选参数
*/
 
/**
 * 
 * 此插件旨在替代javascript原生的alert弹出窗口,使我们在表单校验时显示的提示信息对用户来说更友好. 
 * 
 * 
 * @example $('#tip).alert('hello world.');
 * @desc 这是一个最基本的使用方法,它将会在id为tip的元素左边显示一个DIV浮动层.
 *
 *
 * @example $('#tip').alert({
 *  position: 'right',
 *  focus: true,
 *  alertzIndex: 999,
 *  alertClass: 'corner'
 * });
 * @desc 我们可以通过插件提供的options参数,
 使DIV浮动层显示在我们想要的地方,
 或者是否在同时使其获得焦点,
 调整其显示的样式(样式定义请参照jquery.alert.css)
 * 
 */
 
$.alert = {version: '1.0.3'};
var $alertContainer,$alertMsg,$alertTable;
$.fn.alert = function(msg, options) {
    return this.each(function(){
        var opts = $.extend(true, {}, $.fn.alert.defaults, options || {});
        var alertzIndex = +opts.alertzIndex;
        var position = opts.position ? opts.position : 'right'; 
        var _timeout = opts.timeout ? options.timeout : 5000;
 
        if(!$('#alertContainer').length){
 
//  <div style="width: 216px;" class="layer_form_tips">
//  <div class="bg">
//    <div class="tipsContent">
//    <a class="W_ico12 icon_close"></a>
//      <p class="tips"><span class="icon_rederrorS"></span>请输入登录名</p>
//      
//    </div>
//    <div class="arrow arrow_tips" node-type="arrow"></div>
//  </div>
//</div>
 
            $(['<div class="warn_tips" id="alertContainer">',
                 '<div class="layer_form_tips">',
                    '<div class="bg">',
                        '<div class="tipsContent">',
                            '<a class="W_ico12 icon_close"></a>',
                            '<p class="tips"><span class="icon_rederrorS"></span><span id="alertMsg"></span></p>',
                            '</div>',
                            '<div class="arrow arrow_tips" node-type="arrow"></div>',
                    '</div>',
                  '</div>',
                '</div>'].join('')).appendTo('body');
 
 
//            $(['<div id="alertContainer">',
//                    '<table cellspacing="0" cellpadding="0" border="0">',
//                            '<tbody>',
//                                '<tr>',
//                                    '<td class="alert_tl" />',
//                                    '<td class="alert_tc">',
//                                        '<div />',
//                                    '</td>',
//                                    '<td class="alert_tr" />',
//                                '</tr>',
//                                '<tr>',
//                                    '<td class="alert_ml" />',
//                                    '<td class="alert_mc" id="alertMsg"></td>',
//                                    '<td class="alert_mr" />',
//                                '</tr>',
//                                '<tr>',
//                                    '<td class="alert_bl" />',
//                                    '<td class="alert_bc">',
//                                        '<div />',
//                                    '</td>',
//                                    '<td class="alert_br" />',
//                                '</tr>',
//                            '</tbody>',
//                    '</table>',
//               '</div>'].join('')).appendTo('body');
            $alertContainer = $('#alertContainer').css({position:'absolute',overflow:'hidden'}).hide();
            $alertTable = $alertContainer.find('table');
            $alertMsg = $('#alertMsg');
            if($.fn.bgiframe){
                $alertContainer.bgiframe();
            }
        }// end if
        var $obj = $(this);
        // 提示内容
        $alertMsg.html(msg);
        // 显示tuna_alert 为提示信息的容器
        $alertTable.removeClass().addClass('alert-' + opts.alertClass);
        // 设置zIndex并显示提示DIV
        $alertContainer.css({zIndex:alertzIndex}).show();
        // 设置位置
        var offset = $obj.offset();
        var alertTop = offset.top;
        var alertLeft = offset.left;
        var oHeight = $obj.height();
        var oWidth = $obj.width();
        var offsetTop = ($obj.outerHeight() - oHeight)/2;
        var offsetLeft = ($obj.outerWidth() - oWidth)/2;
        if(position == 'right'){
            alertTop = alertTop - ($alertContainer.height()/2 - $obj.height()/2) + offsetTop;
            alertLeft = alertLeft + oWidth + offsetLeft;
        }else if(position == 'top'){
            alertTop = alertTop - $alertContainer.height();
        }else if(position == 'bottom'){
            alertTop = alertTop + $obj.height() + (offsetTop * 2);
        } 
       // alertTop=alertTop-9;
        $alertContainer.css('left', alertLeft + 'px').css('top', alertTop + 'px');
        // 给对象增加样式
        //$obj.addClass('invalid');    
        /** 校验通过调用此函数 */
        function hideMsg(event) {
            // 给对象增加删除样式
            //$obj.removeClass('invalid');
            // 隐藏校验提示框
            $alertContainer.hide();
            // 删除对象上blur事件对m函数的绑定
            $obj.unbind('blur', hideMsg);
            // 删除body上blur事件对hideMsg函数的绑定
            $('body').unbind('mousedown', hideMsg);
        }
    
        var flag = 1;
        var events = $obj.data("events");
        // 如果元素不可用 flag=0
        if ($obj[0].disabled) {
            flag = 0;
        } else {
            // 如果对象上已有blur事件,则将flag设为0,使不在对象上将hideMsg方法注册给blur事件
            // 避免触发对象上的blur事件时同时执行hideMsg方法将本来已显示的提示DIV隐藏
            if(events && events['blur']){
                flag = 0;
            }
            // 解决blur事件上有校验时死循环问题
            if(opts.focus){
                // 如果可用则马上执行校验对象上的焦点事件
                setTimeout(function () {
                    try {
                        $obj.trigger('focus');
                    } catch (e) {
                        flag = 0;
                    }}, 0);
            }
        }
        if (flag) {
            // 如果对象可用则给对象注册hideMsg函数给blur事件
            $obj.bind('blur', hideMsg);
        } else {
            // 如果不可用则在body上注册hideMsg函数给mousedown事件
            $('body').bind('mousedown', hideMsg);
        }
 
        if(!opts.focus){
            // 如果可用则马上执行校验对象上的焦点事件
            setTimeout(hideMsg, _timeout);
            }
    });//end each
};
// 默认参数
$.fn.alert.defaults = {
    position:             'right',     // 位置字符串(可选)默认为right,可选为top,bottom
    focus:                 true,          // 是否让校验对象获得焦点(解决blur事件上有校验时死循环问题)默认为true让对象获得焦点
    alertzIndex:         199,
    alertClass:            'default',    // 'alert-' + alertClass.
    timeout:            5000        // 默认显示时间  focus=false时有效
};
})(jQuery);