; !function (window, undefined) { var pathtype = true, //是否采用自动获取绝对路径。false:将采用下述变量中的配置 pathurl = 'js/', //当前js所在路径,上述变量为false才有效 $, win, ready = { ie6: !-[1,] && !window.xmlhttprequest, times: 0 }; //获取当前路径 ready.getpath = function(){ var js = document.scripts || $('script'), jspath = js[js.length - 1].src; return jspath.substring(0, jspath.lastindexof("/") + 1); }; //载入css ready.load = function(){ pathtype && (pathurl = this.getpath()); var head = $('head')[0], link = document.createelement("link"); link.setattribute('type', 'text/css'); link.setattribute('rel', 'stylesheet'); link.setattribute('href', pathurl +'css/layer.css'); head.appendchild(link); }; //默认内置方法。 this.layer = { v : '1.5.2', //版本号 ready: function(callback){ var load = '#layercss'; return $(load).ready(function(){ callback(); }); }, alert: function(alertmsg , alerttype, alerttit , alertyes){ //普通对话框,类似系统默认的alert() return $.layer({ dialog : {msg : alertmsg, type : alerttype, yes : alertyes}, title : alerttit, area: ['auto', 'auto'] }); }, confirm: function(conmsg , conyes , contit , conno){ //询问框,类似系统默认的confirm() return $.layer({ dialog : {msg : conmsg, type : 4, btns : 2, yes : conyes, no : conno}, title : contit }); }, msg: function(msgtext , msgtime , msgtype , callback){ //普通消息框,一般用于行为成功后的提醒,默认两秒自动关闭 (msgtext == '' || msgtext == undefined) && (msgtext = ' '); (msgtime == undefined || msgtime == '') && (msgtime = 2); return $.layer({ dialog : {msg : msgtext, type : msgtype}, time : msgtime, title : ['', false], closebtn : ['', false], end: function(){callback && callback()} }); }, tips: function(html , follow , time , maxwidth, guide, style){ return $.layer({ type : 4, shade : false, time : time, maxwidth : maxwidth, tips : {msg: html, guide: guide, follow: follow, style: style} }) }, load: function(loadtime , loadgif , loadshade){ var border = true; loadgif === 3 && (border = false); return $.layer({ time : loadtime, shade : loadshade, loading : {type : loadgif}, border :[10,0.3,'#000',border], type : 3, title : ['',false], closebtn : [0 , false] }); } }; var class = function(setings){ ready.times++; this.index = ready.times; var config = this.config; this.config = $.extend({} , config , setings); this.config.dialog = $.extend({}, config.dialog , setings.dialog); this.config.page = $.extend({}, config.page , setings.page); this.config.iframe = $.extend({}, config.iframe , setings.iframe); this.config.loading = $.extend({}, config.loading , setings.loading); this.config.tips = $.extend({}, config.tips , setings.tips); this.creat(); }; class.pt = class.prototype; //默认配置 class.pt.config = { type: 0, shade: [0.3 , '#000' , true], shadeclose: false, fix: true, move: ['.xubox_title' , true], moveout: false, title: ['信息' , true], offset: ['200px' , '50%'], area: ['310px' , 'auto'], closebtn: [0 , true], time: 0, bgcolor: '#fff', border: [10 , 0.3 , '#000', true], zindex: 19891014, maxwidth: 400, dialog: {btns : 1, btn : ['确定','取消'], type : 3, msg : '', yes : function(index){ layer.close(index);}, no : function(index){ layer.close(index);} }, page: {dom: '#xulayer', html: ''}, iframe: {src: 'http://sentsin.com'}, loading: {type: 0}, tips: {msg: '', follow: '', guide: 0, isguide: true, style: ['background-color:#ff9900; color:#fff;', '#ff9900']}, success: function(layer){}, //创建成功后的回调 close: function(index){ layer.close(index);}, //右上角关闭回调 end: function(){} //终极销毁回调 }; class.pt.type = ['dialog', 'page', 'iframe', 'loading', 'tips']; //容器 class.pt.space = function(html){ var html = html || '', times = this.index, config = this.config, dialog = config.dialog, frame = [ '
' + dialog.msg + '
', '
'+ html +'
', '', '', '
'+ config.tips.msg +'
' ], shade = '' , border = '', zindex = config.zindex + times, shadestyle = 'z-index:'+ zindex +'; background-color:'+ config.shade[1] +'; opacity:'+ config.shade[0] +'; filter:alpha(opacity='+ config.shade[0]*100 +');'; config.shade[2] && (shade = '
'); config.zindex = zindex; var title = '', closebtn = '', borderstyle = "z-index:"+ (zindex-1) +"; background-color: "+ config.border[2] +"; opacity:"+ config.border[1] +"; filter:alpha(opacity="+ config.border[1]*100 +"); top:-"+ config.border[0] +"px; left:-"+ config.border[0] +"px;"; config.border[3] && (border = '
'); config.closebtn[1] && (closebtn = ''); config.title[1] && (title = '

' + config.title[0] + '

') var boxhtml = '
' + '
' + frame[config.type] + title + closebtn + '' + '
'+ border + '
'; return [shade , boxhtml]; }; //创建骨架 class.pt.creat = function(){ var that = this , space = '', config = this.config, dialog = config.dialog, title = that.config.title; title.constructor === array || (that.config.title = [title, true]); title === false && (that.config.title = [title, false]); var page = config.page, body = $("body"), setspace = function(html){ var html = html || '' space = that.space(html); body.append(space[0]); }; switch(config.type){ case 1: if(page.html !== ''){ setspace('
'+ page.html +'
'); body.append(space[1]); }else{ if($(page.dom).parents('.xubox_page').length == 0){ setspace(); $(page.dom).show().wrap(space[1]); }else{ return; } } break; case 2: $('.xubox_iframe')[0] && layer.close($('.xubox_iframe').parents('.xubox_layer').attr('times')); setspace(); body.append(space[1]); break; case 3: config.title = ['', false]; config.area = ['auto', 'auto']; config.closebtn = ['', false]; $('.xubox_loading')[0] && layer.close($('.xubox_loading').parents('.xubox_layer').attr('times')); setspace(); body.append(space[1]); break; case 4: config.title = ['', false]; config.area = ['auto', 'auto']; config.fix = false; config.border = false; $('.xubox_tips')[0] && layer.close($('.xubox_tips').parents('.xubox_layer').attr('times')); setspace(); body.append(space[1]); $('#xubox_layer' + ready.times).find('.xubox_close').css({top: 5, right: 5}); break; default : config.title[1] || (config.area = ['auto','auto']); $('.xubox_dialog')[0] && layer.close($('.xubox_dialog').parents('.xubox_layer').attr('times')); setspace(); body.append(space[1]); break; }; var times = ready.times; this.layers = $('#xubox_shade' + times); this.layerb = $('#xubox_border' + times); this.layere = $('#xubox_layer' + times); var layere = this.layere; this.layermian = layere.find('.xubox_main'); this.layertitle = layere.find('.xubox_title'); this.layertext = layere.find('.xubox_text'); this.layerpage = layere.find('.xubox_page'); this.layerbtn = layere.find('.xubox_botton'); //设置layer面积坐标等数据 if(config.offset[1].indexof("px") != -1){ var _left = parseint(config.offset[1]); }else{ if(config.offset[1] == '50%'){ var _left = config.offset[1]; }else{ var _left = parseint(config.offset[1])/100 * win.width(); } }; layere.css({left: _left + config.border[0], width: config.area[0], height: config.area[1]}); config.fix ? layere.css({top: parseint(config.offset[0]) + config.border[0]}) : layere.css({top: parseint(config.offset[0]) + win.scrolltop() + config.border[0], position: 'absolute'}); //配置按钮 对话层形式专有 if(config.type == 0 && config.title[1]){ switch(dialog.btns){ case 0: that.layerbtn.html('').hide(); break; case 2: that.layerbtn.html(''+ dialog.btn[0] +'' + ''+ dialog.btn[1] + ''); break; default: that.layerbtn.html(''+ dialog.btn[0] +''); } }; if(layere.css('left') === 'auto'){ layere.hide(); settimeout(function(){ layere.show(); that.reset(times); }, 500); }else{ that.reset(times); } this.callback(); }; //调整骨架 class.pt.reset = function(times){ var that = this, layere = this.layere, config = this.config, page = config.page; that.autoarea(times); config.time <= 0 || that.autoclose(); config.move[1] ? layere.find(config.move[0]).css({cursor: 'move'}) : layere.find(config.move[0]).css({cursor: 'auto'}); if(config.title[1]){ ready.ie6 && that.layertitle.css({width : layere.outerwidth()}); }else{ config.type != 4 && layere.find('.xubox_close').addclass('xubox_close1'); }; layere.attr({'type' : that.type[config.type]}); switch(config.type){ case 1: layere.find(page.dom).addclass('layer_pagecontent'); that.layerpage.css({width: layere.width()}); config.shade[2] && layere.css({zindex: config.zindex + 1}); config.title[1] && that.layerpage.css({top: that.layertitle.outerheight()}); break; case 2: var iframe = layere.find('.xubox_iframe'), heg = layere.height(); iframe.addclass('xubox_load').css({width: layere.width()}); config.title[1] ? iframe.css({top: that.layertitle.height(), height: heg - that.layertitle.height()}) : iframe.css({top: 0, height : heg}); ready.ie6 && iframe.attr('src', config.iframe.src); break; case 4 : var fow = $(config.tips.follow), ftop = fow.offset().top, top = ftop - layere.outerheight(); var fleft = fow.offset().left, left = fleft, color = config.tips.style[1]; var fheight = fow.outerheight(), fwidth = fow.outerwidth(), tipsg = layere.find('.layertipsg'); fwidth > config.maxwidth && fow.width(config.maxwidth); if(config.tips.guide === 1){ var offleft = win.width() - left - fwidth - layere.outerwidth() - 10, top = ftop; if(offleft > 0){ left = left + fow.outerwidth() + 10; tipsg.removeclass('layertipsl').addclass('layertipsr').css({'border-right-color': color}); }else{ left = left - layere.outerwidth() - 10 tipsg.removeclass('layertipsr').addclass('layertipsl').css({'border-left-color': color}); } }else{ if(top - win.scrolltop() - 12 <= 0){ top = ftop + fheight + 10; tipsg.removeclass('layertipst').addclass('layertipsb').css({'border-bottom-color': color}); }else{ top = top - 10; tipsg.removeclass('layertipsb').addclass('layertipst').css({'border-top-color': color}); } } config.tips.isguide || tipsg.remove(); layere.css({top: top , left: left}); break; default : this.layermian.css({'background-color': '#fff'}); if(config.title[1]){ this.layertext.css({paddingtop: 18 + that.layertitle.outerheight()}); }else{ layere.find('.xubox_msgico').css({top : '10px'}); that.layertext.css({margintop : 12}); } break; }; this.move(); }; //自适应宽高 class.pt.autoarea = function(times){ var that = this, layere = that.layere, config = that.config, page = config.page, layermian = that.layermian, layerbtn = that.layerbtn, layertext = that.layertext, layerpage = that.layerpage, layerb = that.layerb, titheight; if(config.area[0] === 'auto' && layermian.outerwidth() >= config.maxwidth){ layere.css({width : config.maxwidth}); } config.title[1] ? titheight = that.layertitle.innerheight() : titheight = 0; switch(config.type){ case 0: var abtn = layerbtn.find('a'), outheight = layertext.outerheight() + 20; if(abtn.length > 0){ var btnheight = abtn.outerheight() + 20; }else{ var btnheight = 0; } break; case 1: var btnheight = 0, outheight = $(page.dom).outerheight(); config.area[0] === 'auto' && layere.css({width : layerpage.outerwidth()}); if(page.html !== ''){ outheight = layerpage.outerheight(); } break; case 3: var load = $(".xubox_loading"), btnheight = 0; var outheight = load.outerheight(); layermian.css({width: load.width()}); break; }; (config.offset[1] === '50%' || config.offset[1] == '') && (config.type !== 4) ? layere.css({marginleft : -layere.outerwidth()/2}) : layere.css({marginleft : 0}); (config.area[1] === 'auto') && layermian.css({height: titheight + outheight + btnheight}); layerb.css({width: layere.outerwidth() + 2*config.border[0] , height: layere.outerheight() + 2*config.border[0]}); (ready.ie6 && config.area[0] != 'auto') && layermian.css({width : layere.outerwidth()}); }; //拖拽层 class.pt.move = function(){ var that = this, config = this.config, layermove = that.layere.find(config.move[0]), layere, ismove; var movex, movey, move, setx = 0, sety = 0; config.move[1] && layermove.attr('move','ok'); $(config.move[0]).on('mousedown', function(m){ m.preventdefault(); if($(this).attr('move') === 'ok'){ ismove = true; layere = $(this).parents('.xubox_layer'); var xx = layere.offset().left, yy = layere.offset().top, ww = layere.width() - 6, hh = layere.height() - 6; if(!$('#xubox_moves')[0]){ $('body').append('
'); } move = $('#xubox_moves'); movex = m.pagex - move.position().left; movey = m.pagey - move.position().top; setx = win.scrollleft(); layere.css('position') !== 'fixed' || (sety = win.scrolltop()); } }); $(document).mousemove(function(m){ if(ismove){ m.preventdefault(); var offsetx = m.pagex - movex; if(layere.css('position') === 'fixed'){ var offsety = m.pagey - movey; }else{ var offsety = m.pagey - movey; } //控制元素不被拖出窗口外 if(!config.moveout){ var setrig = win.width() - move.outerwidth() - config.border[0], sety = win.scrolltop(), settop = config.border[0] + sety; offsetx < config.border[0] && (offsetx = config.border[0]); offsetx > setrig && (offsetx = setrig); offsety < settop && (offsety = settop); offsety > win.height() - move.outerheight() - config.border[0] + sety && (offsety = win.height() - move.outerheight() - config.border[0] + sety) } move.css({left: offsetx, top: offsety}); } }).mouseup(function(){ try{ if(ismove){ if(parseint(layere.css('margin-left')) == 0){ var lefts = parseint(move.css('left')); }else{ var lefts = parseint(move.css('left')) + (-parseint(layere.css('margin-left'))) } layere.css('position') === 'fixed' || (lefts = lefts - layere.parent().offset().left); layere.css({left: lefts, top: parseint(move.css('top')) - sety}); move.remove(); } ismove = false; }catch(e){ ismove = false; } }); }; //自动关闭layer class.pt.autoclose = function(){ var that = this, time = this.config.time; var maxload = function(){ time--; if(time === 0){ layer.close(that.index); clearinterval(that.autotime); } }; this.autotime = setinterval(maxload , 1000); }; ready.config = { end : {} }; class.pt.callback = function(){ this.openlayer(); var that = this, layere = this.layere, config = this.config, dialog = config.dialog; this.config.success(layere); ready.ie6 && this.ie6(); layere.find('.xubox_close').off('click').on('click', function(e){ e.preventdefault(); config.close(that.index); }); layere.find('.xubox_yes').off('click').on('click',function(e){ e.preventdefault(); dialog.yes(that.index); }); layere.find('.xubox_no').off('click').on('click',function(e){ e.preventdefault(); dialog.no(that.index); }); this.layers.off('click').on('click', function(e){ e.preventdefault(); that.config.shadeclose && layer.close(that.index); }); ready.config.end[this.index] = config.end; }; class.pt.ie6 = function(){ var that = this, layere = this.layere, select = $('select'); var _ietop = layere.offset().top; //ie6的固定与相对定位 if(this.config.fix){ var ie6fix = function(){ layere.css({top : $(document).scrolltop() + _ietop}); }; }else{ var ie6fix = function(){ layere.css({top : _ietop}); }; } ie6fix(); win.scroll(ie6fix); //隐藏select $.each(select, function(index , value){ var sthis = $(this); if(!sthis.parents('.xubox_layer')[0]){ sthis.css('display') == 'none' || sthis.attr({'layer' : '1'}).hide(); } }); //恢复select this.reselect = function(){ $.each(select, function(index , value){ var sthis = $(this); if(!sthis.parents('.xubox_layer')[0]){ (sthis.attr('layer') == 1 && $('.xubox_layer').length < 1) && sthis.removeattr('layer').show(); } }); }; }; //给layer对象拓展方法 class.pt.openlayer = function(){ var that = this; //自适应宽高 layer.autoarea = function(index){ return that.autoarea(index); }; //获取layer当前索引 layer.getindex = function(selector){ return $(selector).parents('.xubox_layer').attr('times'); }; //获取子iframe的dom layer.getchildframe = function(selector){ return $("#xubox_iframe").contents().find(selector); }; //得到iframe层的索引,子iframe时使用 layer.getframeindex = function(){ return $('#xubox_iframe').parents('.xubox_layer').attr('times'); }; //iframe层自适应宽高 layer.iframeauto = function(){ var wh = [this.getchildframe('body').outerwidth(), this.getchildframe('body').outerheight()], lbox = $('#xubox_iframe').parents('.xubox_layer'), ids = lbox.attr('times'), tit = lbox.find('.xubox_title'), titht = 0; !tit || (titht = tit.height()); lbox.css({width: wh[0], height: wh[1] + titht}); tit.css({width: wh[0]}); var bs = -parseint($('#xubox_border'+ ids).css('left')); $('#xubox_border'+ ids).css({width: wh[0] + 2*bs, height: wh[1] + 2*bs + titht}); $('#xubox_iframe').css({width: wh[0], height: wh[1]}); }; //关闭layer layer.close = function(index){ var layernow = $('#xubox_layer' + index), shadenow = $('#xubox_moves, #xubox_shade' + index); if(layernow.attr('type') == that.type[1]){ if(layernow.find('#xuboxpagehtml')[0]){ layernow.remove(); }else{ layernow.find('.xubox_close,.xubox_botton,.xubox_title,.xubox_border').remove(); for(var i = 0 ; i < 3 ; i++){ layernow.find('.layer_pagecontent').unwrap().hide(); } } }else{ !-[1,] && layernow.find('#xubox_iframe').remove(); layernow.remove(); } shadenow.remove(); ready.ie6 && that.reselect(); typeof ready.config.end[index] === 'function' && ready.config.end[index](); delete ready.config.end[index]; }; //关闭加载层,仅loading私有 layer.loadclose = function(){ var parent = $('.xubox_loading').parents('.xubox_layer'), index = parent.attr('times'); layer.close(index); }; //出场内置动画 layer.shift = function(type, rate){ var config = that.config, ie6 = ready.ie6, layere = that.layere, cutwth = 0, ww = win.width(), wh = win.height(); (config.offset[1] == '50%' || config.offset[1] == '') ? cutwth = layere.outerwidth()/2 : cutwth = layere.outerwidth(); var anim = { t: {top : config.border[0]}, b: {top : wh - layere.outerheight() - config.border[0]}, cl: cutwth + config.border[0], ct: -layere.outerheight(), cr: ww - cutwth - config.border[0], fn: function(){ ie6 && that.ie6(); } }; switch(type){ case 'left-top': layere.css({left: anim.cl, top: anim.ct}).animate(anim.t, rate, anim.fn); break; case 'right-top': layere.css({left: anim.cr, top: anim.ct}).animate(anim.t, rate, anim.fn); break; case 'left-bottom': layere.css({left: anim.cl, top: wh}).animate(anim.b, rate, anim.fn); break; case 'right-bottom': layere.css({left: anim.cr, top: wh}).animate(anim.b, rate, anim.fn); break; }; }; layer.setmove = function(){ return that.move(); } }; //主入口 ready.run = function(){ $ = jquery; win = $(window); this.load(); $.layer = function(deliver){ var o = new class(deliver); return o.index; }; }; //为支持cmd规范的模块加载器 var require = '../../init/jquery'; //若采用seajs,需正确配置jquery的相对路径。未用可无视此处。 if(typeof define === 'function' && typeof seajs === 'object'){ define([require], function(require, exports, module){ ready.run(); exports.layer = [window.layer, window['$'].layer]; }); }else{ ready.run(); } }(window);