layer.js 15 KB

1
  1. /*! layer-v2.1 弹层组件 License LGPL http://layer.layui.com/ By 贤心 */!function(n,p){var h,l,k={getPath:function(){var b=document.scripts,b=b[b.length-1],a=b.src;if(!b.getAttribute("merge"))return a.substring(0,a.lastIndexOf("/")+1)}(),enter:function(b){13===b.keyCode&&b.preventDefault()},config:{},end:{},btn:["\x26#x786E;\x26#x5B9A;","\x26#x53D6;\x26#x6D88;"],type:["dialog","page","iframe","loading","tips"]},e={v:"2.1",ie6:!!n.ActiveXObject&&!n.XMLHttpRequest,index:0,path:k.getPath,config:function(b,a){var d=0;return b=b||{},e.cache=k.config=h.extend(k.config,b),e.path=k.config.path||e.path,"string"==typeof b.extend&&(b.extend=[b.extend]),e.use("skin/layer.css",b.extend&&0<b.extend.length?function f(){var g=b.extend;e.use(g[g[d]?d:d-1],d<g.length?(++d,f):a)}():a),this},use:function(b,a,d){var c=h("head")[0];b=b.replace(/\s/g,"");var f=/\.css$/.test(b),g=document.createElement(f?"link":"script"),k="layui_layer_"+b.replace(/\.|\//g,"");return e.path?(f&&(g.rel="stylesheet"),g[f?"href":"src"]=/^http:\/\//.test(b)?b:e.path+b,g.id=k,h("#"+k)[0]||c.appendChild(g),function r(){if(f?1989===parseInt(h("#"+k).css("width")):e[d||k]){a&&a();try{f||c.removeChild(g)}catch(b){}}else setTimeout(r,100)}(),this):void 0},ready:function(b,a){var d="function"==typeof b;return d&&(a=b),e.config(h.extend(k.config,d?{}:{path:b}),a),this},alert:function(b,a,d){var c="function"==typeof a;return c&&(d=a),e.open(h.extend({content:b,yes:d},c?{}:a))},confirm:function(b,a,d,c){var f="function"==typeof a;return f&&(c=d,d=a),e.open(h.extend({content:b,btn:k.btn,yes:d,cancel:c},f?{}:a))},msg:function(b,a,d){var c="function"==typeof a,f=k.config.skin,f=(f?f+" "+f+"-msg":"")||"layui-layer-msg",q=g.anim.length-1;return c&&(d=a),e.open(h.extend({content:b,time:3E3,shade:!1,skin:f,title:!1,closeBtn:!1,btn:!1,end:d},c&&!k.config.skin?{skin:f+" layui-layer-hui",shift:q}:(a=a||{},(-1===a.icon||a.icon===p&&!k.config.skin)&&(a.skin=f+" "+(a.skin||"layui-layer-hui")),a)))},load:function(b,a){return e.open(h.extend({type:3,icon:b||0,shade:.01},a))},tips:function(b,a,d){return e.open(h.extend({type:4,content:[b,a],closeBtn:!1,time:3E3,maxWidth:210},d))}},m=function(b){this.index=++e.index;this.config=h.extend({},this.config,k.config,b);this.creat()};m.pt=m.prototype;var g="layui-layer .layui-layer-title .layui-layer-main .layui-layer-dialog layui-layer-iframe layui-layer-content layui-layer-btn layui-layer-close".split(" ");g.anim="layui-anim layui-anim-01 layui-anim-02 layui-anim-03 layui-anim-04 layui-anim-05 layui-anim-06".split(" ");m.pt.config={type:0,shade:.3,fix:!0,move:g[1],title:"\x26#x4FE1;\x26#x606F;",offset:"auto",area:"auto",closeBtn:1,time:0,zIndex:19891014,maxWidth:360,shift:0,icon:-1,scrollbar:!0,tips:2};m.pt.vessel=function(b,a){var d=this.index,c=this.config,f=c.zIndex+d,h="object"==typeof c.title,e=c.maxmin&&(1===c.type||2===c.type),h=c.title?'\x3cdiv class\x3d"layui-layer-title" style\x3d"'+(h?c.title[1]:"")+'"\x3e'+(h?c.title[0]:c.title)+"\x3c/div\x3e":"";return c.zIndex=f,a([c.shade?'\x3cdiv class\x3d"layui-layer-shade" id\x3d"layui-layer-shade'+d+'" times\x3d"'+d+'" style\x3d"'+("z-index:"+(f-1)+"; background-color:"+(c.shade[1]||"#000")+"; opacity:"+(c.shade[0]||c.shade)+"; filter:alpha(opacity\x3d"+(100*c.shade[0]||100*c.shade)+");")+'"\x3e\x3c/div\x3e':"",'\x3cdiv class\x3d"'+g[0]+" "+(g.anim[c.shift]||"")+(" layui-layer-"+k.type[c.type])+(0!=c.type&&2!=c.type||c.shade?"":" layui-layer-border")+" "+(c.skin||"")+'" id\x3d"'+g[0]+d+'" type\x3d"'+k.type[c.type]+'" times\x3d"'+d+'" showtime\x3d"'+c.time+'" conType\x3d"'+(b?"object":"string")+'" style\x3d"z-index: '+f+"; width:"+c.area[0]+";height:"+c.area[1]+(c.fix?"":";position:absolute;")+'"\x3e'+(b&&2!=c.type?"":h)+'\x3cdiv class\x3d"layui-layer-content'+(0==c.type&&-1!==c.icon?" layui-layer-padding":"")+(3==c.type?" layui-layer-loading"+c.icon:"")+'"\x3e'+(0==c.type&&-1!==c.icon?'\x3ci class\x3d"layui-layer-ico layui-layer-ico'+c.icon+'"\x3e\x3c/i\x3e':"")+(1==c.type&&b?"":c.content||"")+'\x3c/div\x3e\x3cspan class\x3d"layui-layer-setwin"\x3e'+function(){var a=e?'\x3ca class\x3d"layui-layer-min" href\x3d"javascript:;"\x3e\x3ccite\x3e\x3c/cite\x3e\x3c/a\x3e\x3ca class\x3d"layui-layer-ico layui-layer-max" href\x3d"javascript:;"\x3e\x3c/a\x3e':"";return c.closeBtn&&(a+='\x3ca class\x3d"layui-layer-ico '+g[7]+" "+g[7]+(c.title?c.closeBtn:4==c.type?"1":"2")+'" href\x3d"javascript:;"\x3e\x3c/a\x3e'),a}()+"\x3c/span\x3e"+(c.btn?function(){var a="";"string"==typeof c.btn&&(c.btn=[c.btn]);for(var b=0,d=c.btn.length;d>b;b++)a+='\x3ca class\x3d"'+g[6]+b+'"\x3e'+c.btn[b]+"\x3c/a\x3e";return'\x3cdiv class\x3d"'+g[6]+'"\x3e'+a+"\x3c/div\x3e"}():"")+"\x3c/div\x3e"],h),this};m.pt.creat=function(){var b=this,a=b.config,d=b.index,c=a.content,f="object"==typeof c;switch("string"==typeof a.area&&(a.area="auto"===a.area?["",""]:[a.area,""]),a.type){case 0:a.btn="btn"in a?a.btn:k.btn[0];e.closeAll("dialog");break;case 2:c=a.content=f?a.content:[a.content||"http://layer.layui.com","auto"];a.content='\x3ciframe scrolling\x3d"'+(a.content[1]||"auto")+'" allowtransparency\x3d"true" id\x3d"'+g[4]+d+'" name\x3d"'+g[4]+d+'" onload\x3d"this.className\x3d\'\';" class\x3d"layui-layer-load" frameborder\x3d"0" src\x3d"'+a.content[0]+'"\x3e\x3c/iframe\x3e';break;case 3:a.title=!1;a.closeBtn=!1;-1===a.icon&&0===a.icon;e.closeAll("loading");break;case 4:f||(a.content=[a.content,"body"]),a.follow=a.content[1],a.content=a.content[0]+'\x3ci class\x3d"layui-layer-TipsG"\x3e\x3c/i\x3e',a.title=!1,a.shade=!1,a.fix=!1,a.tips="object"==typeof a.tips?a.tips:[a.tips,!0],a.tipsMore||e.closeAll("tips")}b.vessel(f,function(e,k){h("body").append(e[0]);f?2==a.type||4==a.type?h("body").append(e[1]):c.parents("."+g[0])[0]||(c.show().addClass("layui-layer-wrap").wrap(e[1]),h("#"+g[0]+d).find("."+g[5]).before(k)):h("body").append(e[1]);b.layero=h("#"+g[0]+d);a.scrollbar||g.html.css("overflow","hidden").attr("layer-full",d)}).auto(d);2==a.type&&e.ie6&&b.layero.find("iframe").attr("src",c[0]);h(document).off("keydown",k.enter).on("keydown",k.enter);b.layero.on("keydown",function(a){h(document).off("keydown",k.enter)});4==a.type?b.tips():b.offset();a.fix&&l.on("resize",function(){b.offset();(/^\d+%$/.test(a.area[0])||/^\d+%$/.test(a.area[1]))&&b.auto(d);4==a.type&&b.tips()});0>=a.time||setTimeout(function(){e.close(b.index)},a.time);b.move().callback()};m.pt.auto=function(b){function a(a){a=c.find(a);a.height(f[1]-e-k-2*(0|parseFloat(a.css("padding"))))}var d=this.config,c=h("#"+g[0]+b);""===d.area[0]&&0<d.maxWidth&&(/MSIE 7/.test(navigator.userAgent)&&d.btn&&c.width(c.innerWidth()),c.outerWidth()>d.maxWidth&&c.width(d.maxWidth));var f=[c.innerWidth(),c.innerHeight()],e=c.find(g[1]).outerHeight()||0,k=c.find("."+g[6]).outerHeight()||0;switch(d.type){case 2:a("iframe");break;default:""===d.area[1]?d.fix&&f[1]>=l.height()&&(f[1]=l.height(),a("."+g[5])):a("."+g[5])}return this};m.pt.offset=function(){var b=this.config,a=this.layero,d=[a.outerWidth(),a.outerHeight()],c="object"==typeof b.offset;this.offsetTop=(l.height()-d[1])/2;this.offsetLeft=(l.width()-d[0])/2;c?(this.offsetTop=b.offset[0],this.offsetLeft=b.offset[1]||this.offsetLeft):"auto"!==b.offset&&(this.offsetTop=b.offset,"rb"===b.offset&&(this.offsetTop=l.height()-d[1],this.offsetLeft=l.width()-d[0]));b.fix||(this.offsetTop=/%$/.test(this.offsetTop)?l.height()*parseFloat(this.offsetTop)/100:parseFloat(this.offsetTop),this.offsetLeft=/%$/.test(this.offsetLeft)?l.width()*parseFloat(this.offsetLeft)/100:parseFloat(this.offsetLeft),this.offsetTop+=l.scrollTop(),this.offsetLeft+=l.scrollLeft());a.css({top:this.offsetTop,left:this.offsetLeft})};m.pt.tips=function(){var b=this.config,a=this.layero,d=[a.outerWidth(),a.outerHeight()],c=h(b.follow);c[0]||(c=h("body"));var f={width:c.outerWidth(),height:c.outerHeight(),top:c.offset().top,left:c.offset().left},e=a.find(".layui-layer-TipsG"),c=b.tips[0];b.tips[1]||e.remove();f.autoLeft=function(){0<f.left+d[0]-l.width()?(f.tipLeft=f.left+f.width-d[0],e.css({right:12,left:"auto"})):f.tipLeft=f.left};f.where=[function(){f.autoLeft();f.tipTop=f.top-d[1]-10;e.removeClass("layui-layer-TipsB").addClass("layui-layer-TipsT").css("border-right-color",b.tips[1])},function(){f.tipLeft=f.left+f.width+10;f.tipTop=f.top;e.removeClass("layui-layer-TipsL").addClass("layui-layer-TipsR").css("border-bottom-color",b.tips[1])},function(){f.autoLeft();f.tipTop=f.top+f.height+10;e.removeClass("layui-layer-TipsT").addClass("layui-layer-TipsB").css("border-right-color",b.tips[1])},function(){f.tipLeft=f.left-d[0]-10;f.tipTop=f.top;e.removeClass("layui-layer-TipsR").addClass("layui-layer-TipsL").css("border-bottom-color",b.tips[1])}];f.where[c-1]();1===c?0>f.top-(l.scrollTop()+d[1]+16)&&f.where[2]():2===c?0<l.width()-(f.left+f.width+d[0]+16)||f.where[3]():3===c?0<f.top-l.scrollTop()+f.height+d[1]+16-l.height()&&f.where[0]():4===c&&0<d[0]+16-f.left&&f.where[1]();a.find("."+g[5]).css({"background-color":b.tips[1],"padding-right":b.closeBtn?"30px":""});a.css({left:f.tipLeft,top:f.tipTop})};m.pt.move=function(){var b=this.config,a={setY:0,moveLayer:function(){var b=a.layero,d=parseInt(b.css("margin-left")),e=parseInt(a.move.css("left"));0===d||(e-=d);"fixed"!==b.css("position")&&(e-=b.parent().offset().left,a.setY=0);b.css({left:e,top:parseInt(a.move.css("top"))-a.setY})}},d=this.layero.find(b.move);return b.move&&d.attr("move","ok"),d.css({cursor:b.move?"move":"auto"}),h(b.move).on("mousedown",function(c){if(c.preventDefault(),"ok"===h(this).attr("move")){a.ismove=!0;a.layero=h(this).parents("."+g[0]);var d=a.layero.offset().left,e=a.layero.offset().top,k=a.layero.outerWidth()-6,m=a.layero.outerHeight()-6;h("#layui-layer-moves")[0]||h("body").append('\x3cdiv id\x3d"layui-layer-moves" class\x3d"layui-layer-moves" style\x3d"left:'+d+"px; top:"+e+"px; width:"+k+"px; height:"+m+'px; z-index:2147483584"\x3e\x3c/div\x3e');a.move=h("#layui-layer-moves");b.moveType&&a.move.css({visibility:"hidden"});a.moveX=c.pageX-a.move.position().left;a.moveY=c.pageY-a.move.position().top;"fixed"!==a.layero.css("position")||(a.setY=l.scrollTop())}}),h(document).mousemove(function(c){if(a.ismove){var d=c.pageX-a.moveX,e=c.pageY-a.moveY;if(c.preventDefault(),!b.moveOut){a.setY=l.scrollTop();c=l.width()-a.move.outerWidth();var g=a.setY;0>d&&(d=0);d>c&&(d=c);g>e&&(e=g);e>l.height()-a.move.outerHeight()+a.setY&&(e=l.height()-a.move.outerHeight()+a.setY)}a.move.css({left:d,top:e});b.moveType&&a.moveLayer()}}).mouseup(function(){try{a.ismove&&(a.moveLayer(),a.move.remove(),b.moveEnd&&b.moveEnd()),a.ismove=!1}catch(c){a.ismove=!1}}),this};m.pt.callback=function(){function b(){!1===(c.cancel&&c.cancel(a.index))||e.close(a.index)}var a=this,d=a.layero,c=a.config;a.openLayer();c.success&&(2==c.type?d.find("iframe").on("load",function(){c.success(d,a.index)}):c.success(d,a.index));e.ie6&&a.IE6(d);d.find("."+g[6]).children("a").on("click",function(){var f=h(this).index();c["btn"+(f+1)]&&c["btn"+(f+1)](a.index,d);0===f?c.yes?c.yes(a.index,d):e.close(a.index):1===f?b():c["btn"+(f+1)]||e.close(a.index)});d.find("."+g[7]).on("click",b);c.shadeClose&&h("#layui-layer-shade"+a.index).on("click",function(){e.close(a.index)});d.find(".layui-layer-min").on("click",function(){e.min(a.index,c);c.min&&c.min(d)});d.find(".layui-layer-max").on("click",function(){h(this).hasClass("layui-layer-maxmin")?(e.restore(a.index),c.restore&&c.restore(d)):(e.full(a.index,c),c.full&&c.full(d))});c.end&&(k.end[a.index]=c.end)};k.reselect=function(){h.each(h("select"),function(b,a){var d=h(this);d.parents("."+g[0])[0]||1==d.attr("layer")&&1>h("."+g[0]).length&&d.removeAttr("layer").show()})};m.pt.IE6=function(b){function a(){b.css({top:c+(d.config.fix?l.scrollTop():0)})}var d=this,c=b.offset().top;a();l.scroll(a);h("select").each(function(a,b){var c=h(this);c.parents("."+g[0])[0]||"none"===c.css("display")||c.attr({layer:"1"}).hide()})};m.pt.openLayer=function(){e.zIndex=this.config.zIndex;e.setTop=function(b){return e.zIndex=parseInt(b[0].style.zIndex),b.on("mousedown",function(){e.zIndex++;b.css("z-index",e.zIndex+1)}),e.zIndex}};k.record=function(b){var a=[b.outerWidth(),b.outerHeight(),b.position().top,b.position().left+parseFloat(b.css("margin-left"))];b.find(".layui-layer-max").addClass("layui-layer-maxmin");b.attr({area:a})};k.rescollbar=function(b){g.html.attr("layer-full")==b&&(g.html[0].style.removeProperty?g.html[0].style.removeProperty("overflow"):g.html[0].style.removeAttribute("overflow"),g.html.removeAttr("layer-full"))};n.layer=e;e.getChildFrame=function(b,a){return a=a||h("."+g[4]).attr("times"),h("#"+g[0]+a).find("iframe").contents().find(b)};e.getFrameIndex=function(b){return h("#"+b).parents("."+g[4]).attr("times")};e.iframeAuto=function(b){if(b){var a=e.getChildFrame("html",b).outerHeight();b=h("#"+g[0]+b);var d=b.find(g[1]).outerHeight()||0,c=b.find("."+g[6]).outerHeight()||0;b.css({height:a+d+c});b.find("iframe").css({height:a})}};e.iframeSrc=function(b,a){h("#"+g[0]+b).find("iframe").attr("src",a)};e.style=function(b,a){var d=h("#"+g[0]+b),c=d.attr("type"),e=d.find(g[1]).outerHeight()||0,l=d.find("."+g[6]).outerHeight()||0;c!==k.type[1]&&c!==k.type[2]||(d.css(a),c!==k.type[2]||d.find("iframe").css({height:parseFloat(a.height)-e-l}))};e.min=function(b,a){var d=h("#"+g[0]+b),c=d.find(g[1]).outerHeight()||0;k.record(d);e.style(b,{width:180,height:c,overflow:"hidden"});d.find(".layui-layer-min").hide();"page"===d.attr("type")&&d.find(g[4]).hide();k.rescollbar(b)};e.restore=function(b){var a=h("#"+g[0]+b),d=a.attr("area").split(",");a.attr("type");e.style(b,{width:parseFloat(d[0]),height:parseFloat(d[1]),top:parseFloat(d[2]),left:parseFloat(d[3]),overflow:"visible"});a.find(".layui-layer-max").removeClass("layui-layer-maxmin");a.find(".layui-layer-min").show();"page"===a.attr("type")&&a.find(g[4]).show();k.rescollbar(b)};e.full=function(b){var a,d=h("#"+g[0]+b);k.record(d);g.html.attr("layer-full")||g.html.css("overflow","hidden").attr("layer-full",b);clearTimeout(a);a=setTimeout(function(){var a="fixed"===d.css("position");e.style(b,{top:a?0:l.scrollTop(),left:a?0:l.scrollLeft(),width:l.width(),height:l.height()});d.find(".layui-layer-min").hide()},100)};e.title=function(b,a){h("#"+g[0]+(a||e.index)).find(g[1]).html(b)};e.close=function(b){var a=h("#"+g[0]+b),d=a.attr("type");if(a[0]){if(d===k.type[1]&&"object"===a.attr("conType"))for(a.children(":not(."+g[5]+")").remove(),d=0;2>d;d++)a.find(".layui-layer-wrap").unwrap().hide();else{if(d===k.type[2])try{var c=h("#"+g[4]+b)[0];c.contentWindow.document.write("");c.contentWindow.close();a.find("."+g[5])[0].removeChild(c)}catch(f){}a[0].innerHTML="";a.remove()}h("#layui-layer-moves, #layui-layer-shade"+b).remove();e.ie6&&k.reselect();k.rescollbar(b);h(document).off("keydown",k.enter);"function"==typeof k.end[b]&&k.end[b]();delete k.end[b]}};e.closeAll=function(b){h.each(h("."+g[0]),function(){var a=h(this);(b?a.attr("type")===b:1)&&e.close(a.attr("times"))})};k.run=function(){h=jQuery;l=h(n);g.html=h("html");e.open=function(b){return(new m(b)).index}};"function"==typeof define?define(function(){return k.run(),e}):(k.run(),e.use("skin/layer.css"))}(window);