You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1327 lines
39 KiB

  1. /**
  2. @Name:layer v3.1.4 Web弹层组件
  3. @Author:贤心
  4. @Site:http://layer.layui.com
  5. @License:MIT
  6. */
  7. ;!function(window, undefined){
  8. "use strict";
  9. var isLayui = window.layui && layui.define, $, win, ready = {
  10. getPath: function(){
  11. var jsPath = document.currentScript ? document.currentScript.src : function(){
  12. var js = document.scripts
  13. ,last = js.length - 1
  14. ,src;
  15. for(var i = last; i > 0; i--){
  16. if(js[i].readyState === 'interactive'){
  17. src = js[i].src;
  18. break;
  19. }
  20. }
  21. return src || js[last].src;
  22. }();
  23. return jsPath.substring(0, jsPath.lastIndexOf('/') + 1);
  24. }(),
  25. config: {}, end: {}, minIndex: 0, minLeft: [],
  26. btn: ['确定', '取消'],
  27. //五种原始层模式
  28. type: ['dialog', 'page', 'iframe', 'loading', 'tips'],
  29. //获取节点的style属性值
  30. getStyle: function(node, name){
  31. var style = node.currentStyle ? node.currentStyle : window.getComputedStyle(node, null);
  32. return style[style.getPropertyValue ? 'getPropertyValue' : 'getAttribute'](name);
  33. },
  34. //载入CSS配件
  35. link: function(href, fn, cssname){
  36. //未设置路径,则不主动加载css
  37. if(!layer.path) return;
  38. var head = document.getElementsByTagName("head")[0], link = document.createElement('link');
  39. if(typeof fn === 'string') cssname = fn;
  40. var app = (cssname || href).replace(/\.|\//g, '');
  41. var id = 'layuicss-'+ app, timeout = 0;
  42. link.rel = 'stylesheet';
  43. link.href = layer.path + href;
  44. link.id = id;
  45. if(!document.getElementById(id)){
  46. head.appendChild(link);
  47. }
  48. if(typeof fn !== 'function') return;
  49. //轮询css是否加载完毕
  50. (function poll() {
  51. if(++timeout > 8 * 1000 / 100){
  52. return window.console && console.error('layer.css: Invalid');
  53. };
  54. parseInt(ready.getStyle(document.getElementById(id), 'width')) === 1989 ? fn() : setTimeout(poll, 100);
  55. }());
  56. }
  57. };
  58. //默认内置方法。
  59. var layer = {
  60. v: '3.1.1',
  61. ie: function(){ //ie版本
  62. var agent = navigator.userAgent.toLowerCase();
  63. return (!!window.ActiveXObject || "ActiveXObject" in window) ? (
  64. (agent.match(/msie\s(\d+)/) || [])[1] || '11' //由于ie11并没有msie的标识
  65. ) : false;
  66. }(),
  67. index: (window.layer && window.layer.v) ? 100000 : 0,
  68. path: ready.getPath,
  69. config: function(options, fn){
  70. options = options || {};
  71. cache = layer.cache = ready.config = $.extend({}, ready.config, options);
  72. layer.path = ready.config.path || layer.path;
  73. typeof options.extend === 'string' && (options.extend = [options.extend]);
  74. if(ready.config.path) layer.ready();
  75. if(!options.extend) return this;
  76. isLayui
  77. ? layui.addcss('modules/layer/' + options.extend)
  78. : ready.link('theme/' + options.extend);
  79. return this;
  80. },
  81. //主体CSS等待事件
  82. ready: function(callback){
  83. var cssname = 'layer', ver = ''
  84. ,path = (isLayui ? 'modules/layer/' : 'theme/') + 'default/layer.css?v='+ layer.v + ver;
  85. isLayui ? layui.addcss(path, callback, cssname) : ready.link(path, callback, cssname);
  86. return this;
  87. },
  88. //各种快捷引用
  89. alert: function(content, options, yes){
  90. var type = typeof options === 'function';
  91. if(type) yes = options;
  92. return layer.open($.extend({
  93. content: content,
  94. yes: yes
  95. }, type ? {} : options));
  96. },
  97. confirm: function(content, options, yes, cancel){
  98. var type = typeof options === 'function';
  99. if(type){
  100. cancel = yes;
  101. yes = options;
  102. }
  103. return layer.open($.extend({
  104. content: content,
  105. btn: ready.btn,
  106. yes: yes,
  107. btn2: cancel
  108. }, type ? {} : options));
  109. },
  110. msg: function(content, options, end){ //最常用提示层
  111. var type = typeof options === 'function', rskin = ready.config.skin;
  112. var skin = (rskin ? rskin + ' ' + rskin + '-msg' : '')||'layui-layer-msg';
  113. var anim = doms.anim.length - 1;
  114. if(type) end = options;
  115. return layer.open($.extend({
  116. content: content,
  117. time: 3000,
  118. shade: false,
  119. skin: skin,
  120. title: false,
  121. closeBtn: false,
  122. btn: false,
  123. resize: false,
  124. end: end
  125. }, (type && !ready.config.skin) ? {
  126. skin: skin + ' layui-layer-hui',
  127. anim: anim
  128. } : function(){
  129. options = options || {};
  130. if(options.icon === -1 || options.icon === undefined){
  131. options.skin = skin + ' ' + (options.skin||'layui-layer-hui');
  132. }
  133. return options;
  134. }()));
  135. },
  136. load: function(icon, options){
  137. return layer.open($.extend({
  138. type: 3,
  139. icon: icon || 0,
  140. resize: false,
  141. shade: 0.01
  142. }, options));
  143. },
  144. tips: function(content, follow, options){
  145. return layer.open($.extend({
  146. type: 4,
  147. content: [content, follow],
  148. closeBtn: false,
  149. time: 3000,
  150. shade: false,
  151. resize: false,
  152. fixed: false,
  153. maxWidth: 210
  154. }, options));
  155. }
  156. };
  157. var Class = function(setings){
  158. var that = this;
  159. that.index = ++layer.index;
  160. that.config = $.extend({}, that.config, ready.config, setings);
  161. document.body ? that.creat() : setTimeout(function(){
  162. that.creat();
  163. }, 30);
  164. };
  165. Class.pt = Class.prototype;
  166. //缓存常用字符
  167. var doms = ['layui-layer', '.layui-layer-title', '.layui-layer-main', '.layui-layer-dialog', 'layui-layer-iframe', 'layui-layer-content', 'layui-layer-btn', 'layui-layer-close'];
  168. doms.anim = ['layer-anim-00', 'layer-anim-01', 'layer-anim-02', 'layer-anim-03', 'layer-anim-04', 'layer-anim-05', 'layer-anim-06'];
  169. //默认配置
  170. Class.pt.config = {
  171. type: 0,
  172. shade: 0.3,
  173. fixed: true,
  174. move: doms[1],
  175. title: '信息',
  176. offset: 'auto',
  177. area: 'auto',
  178. closeBtn: 1,
  179. time: 0, //0表示不自动关闭
  180. zIndex: 19891014,
  181. maxWidth: 360,
  182. anim: 0,
  183. isOutAnim: true,
  184. focusBtn: 0,
  185. icon: -1,
  186. moveType: 1,
  187. resize: true,
  188. scrollbar: true, //是否允许浏览器滚动条
  189. tips: 2
  190. };
  191. //容器
  192. Class.pt.vessel = function(conType, callback){
  193. var that = this, times = that.index, config = that.config;
  194. var zIndex = config.zIndex + times, titype = typeof config.title === 'object';
  195. var ismax = config.maxmin && (config.type === 1 || config.type === 2);
  196. var titleHTML = (config.title ? '<div class="layui-layer-title" style="'+ (titype ? config.title[1] : '') +'">'
  197. + (titype ? config.title[0] : config.title)
  198. + '</div>' : '');
  199. config.zIndex = zIndex;
  200. callback([
  201. //遮罩
  202. config.shade ? ('<div class="layui-layer-shade" id="layui-layer-shade'+ times +'" times="'+ times +'" style="'+ ('z-index:'+ (zIndex-1) +'; ') +'"></div>') : '',
  203. //主体
  204. '<div class="'+ doms[0] + (' layui-layer-'+ready.type[config.type]) + (((config.type == 0 || config.type == 2) && !config.shade) ? ' layui-layer-border' : '') + ' ' + (config.skin||'') +'" id="'+ doms[0] + times +'" type="'+ ready.type[config.type] +'" times="'+ times +'" showtime="'+ config.time +'" conType="'+ (conType ? 'object' : 'string') +'" style="z-index: '+ zIndex +'; width:'+ config.area[0] + ';height:' + config.area[1] + (config.fixed ? '' : ';position:absolute;') +'">'
  205. + (conType && config.type != 2 ? '' : titleHTML)
  206. + '<div id="'+ (config.id||'') +'" class="layui-layer-content'+ ((config.type == 0 && config.icon !== -1) ? ' layui-layer-padding' :'') + (config.type == 3 ? ' layui-layer-loading'+config.icon : '') +'">'
  207. + (config.type == 0 && config.icon !== -1 ? '<i class="layui-layer-ico layui-layer-ico'+ config.icon +'"></i>' : '')
  208. + (config.type == 1 && conType ? '' : (config.content||''))
  209. + '</div>'
  210. + '<span class="layui-layer-setwin">'+ function(){
  211. var closebtn = ismax ? '<a class="layui-layer-min" href="javascript:;"><cite></cite></a><a class="layui-layer-ico layui-layer-max" href="javascript:;"></a>' : '';
  212. config.closeBtn && (closebtn += '<a class="layui-layer-ico '+ doms[7] +' '+ doms[7] + (config.title ? config.closeBtn : (config.type == 4 ? '1' : '2')) +'" href="javascript:;"></a>');
  213. return closebtn;
  214. }() + '</span>'
  215. + (config.btn ? function(){
  216. var button = '';
  217. typeof config.btn === 'string' && (config.btn = [config.btn]);
  218. for(var i = 0, len = config.btn.length; i < len; i++){
  219. button += '<a class="'+ doms[6] +''+ i +'" href="javascript:;">'+ config.btn[i] +'</a>'
  220. }
  221. return '<div class="'+ doms[6] +' layui-layer-btn-'+ (config.btnAlign||'') +'">'+ button +'</div>'
  222. }() : '')
  223. + (config.resize ? '<span class="layui-layer-resize"></span>' : '')
  224. + '</div>'
  225. ], titleHTML, $('<div class="layui-layer-move"></div>'));
  226. return that;
  227. };
  228. //创建骨架
  229. Class.pt.creat = function(){
  230. var that = this
  231. ,config = that.config
  232. ,times = that.index, nodeIndex
  233. ,content = config.content
  234. ,conType = typeof content === 'object'
  235. ,body = $('body');
  236. if(config.id && $('#'+config.id)[0]) return;
  237. if(typeof config.area === 'string'){
  238. config.area = config.area === 'auto' ? ['', ''] : [config.area, ''];
  239. }
  240. //anim兼容旧版shift
  241. if(config.shift){
  242. config.anim = config.shift;
  243. }
  244. if(layer.ie == 6){
  245. config.fixed = false;
  246. }
  247. switch(config.type){
  248. case 0:
  249. config.btn = ('btn' in config) ? config.btn : ready.btn[0];
  250. layer.closeAll('dialog');
  251. break;
  252. case 2:
  253. var content = config.content = conType ? config.content : [config.content||'http://layer.layui.com', 'auto'];
  254. config.content = '<iframe scrolling="'+ (config.content[1]||'auto') +'" allowtransparency="true" id="'+ doms[4] +''+ times +'" name="'+ doms[4] +''+ times +'" onload="this.className=\'\';" class="layui-layer-load" frameborder="0" src="' + config.content[0] + '"></iframe>';
  255. break;
  256. case 3:
  257. delete config.title;
  258. delete config.closeBtn;
  259. config.icon === -1 && (config.icon === 0);
  260. layer.closeAll('loading');
  261. break;
  262. case 4:
  263. conType || (config.content = [config.content, 'body']);
  264. config.follow = config.content[1];
  265. config.content = config.content[0] + '<i class="layui-layer-TipsG"></i>';
  266. delete config.title;
  267. config.tips = typeof config.tips === 'object' ? config.tips : [config.tips, true];
  268. config.tipsMore || layer.closeAll('tips');
  269. break;
  270. }
  271. //建立容器
  272. that.vessel(conType, function(html, titleHTML, moveElem){
  273. body.append(html[0]);
  274. conType ? function(){
  275. (config.type == 2 || config.type == 4) ? function(){
  276. $('body').append(html[1]);
  277. }() : function(){
  278. if(!content.parents('.'+doms[0])[0]){
  279. content.data('display', content.css('display')).show().addClass('layui-layer-wrap').wrap(html[1]);
  280. $('#'+ doms[0] + times).find('.'+doms[5]).before(titleHTML);
  281. }
  282. }();
  283. }() : body.append(html[1]);
  284. $('.layui-layer-move')[0] || body.append(ready.moveElem = moveElem);
  285. that.layero = $('#'+ doms[0] + times);
  286. config.scrollbar || doms.html.css('overflow', 'hidden').attr('layer-full', times);
  287. }).auto(times);
  288. //遮罩
  289. $('#layui-layer-shade'+ that.index).css({
  290. 'background-color': config.shade[1] || '#000'
  291. ,'opacity': config.shade[0]||config.shade
  292. });
  293. config.type == 2 && layer.ie == 6 && that.layero.find('iframe').attr('src', content[0]);
  294. //坐标自适应浏览器窗口尺寸
  295. config.type == 4 ? that.tips() : that.offset();
  296. if(config.fixed){
  297. win.on('resize', function(){
  298. that.offset();
  299. (/^\d+%$/.test(config.area[0]) || /^\d+%$/.test(config.area[1])) && that.auto(times);
  300. config.type == 4 && that.tips();
  301. });
  302. }
  303. config.time <= 0 || setTimeout(function(){
  304. layer.close(that.index)
  305. }, config.time);
  306. that.move().callback();
  307. //为兼容jQuery3.0的css动画影响元素尺寸计算
  308. if(doms.anim[config.anim]){
  309. var animClass = 'layer-anim '+ doms.anim[config.anim];
  310. that.layero.addClass(animClass).one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
  311. $(this).removeClass(animClass);
  312. });
  313. };
  314. //记录关闭动画
  315. if(config.isOutAnim){
  316. that.layero.data('isOutAnim', true);
  317. }
  318. };
  319. //自适应
  320. Class.pt.auto = function(index){
  321. var that = this, config = that.config, layero = $('#'+ doms[0] + index);
  322. if(config.area[0] === '' && config.maxWidth > 0){
  323. //为了修复IE7下一个让人难以理解的bug
  324. if(layer.ie && layer.ie < 8 && config.btn){
  325. layero.width(layero.innerWidth());
  326. }
  327. layero.outerWidth() > config.maxWidth && layero.width(config.maxWidth);
  328. }
  329. var area = [layero.innerWidth(), layero.innerHeight()]
  330. ,titHeight = layero.find(doms[1]).outerHeight() || 0
  331. ,btnHeight = layero.find('.'+doms[6]).outerHeight() || 0
  332. ,setHeight = function(elem){
  333. elem = layero.find(elem);
  334. elem.height(area[1] - titHeight - btnHeight - 2*(parseFloat(elem.css('padding-top'))|0));
  335. };
  336. switch(config.type){
  337. case 2:
  338. setHeight('iframe');
  339. break;
  340. default:
  341. if(config.area[1] === ''){
  342. if(config.maxHeight > 0 && layero.outerHeight() > config.maxHeight){
  343. area[1] = config.maxHeight;
  344. setHeight('.'+doms[5]);
  345. } else if(config.fixed && area[1] >= win.height()){
  346. area[1] = win.height();
  347. setHeight('.'+doms[5]);
  348. }
  349. } else {
  350. setHeight('.'+doms[5]);
  351. }
  352. break;
  353. };
  354. return that;
  355. };
  356. //计算坐标
  357. Class.pt.offset = function(){
  358. var that = this, config = that.config, layero = that.layero;
  359. var area = [layero.outerWidth(), layero.outerHeight()];
  360. var type = typeof config.offset === 'object';
  361. that.offsetTop = (win.height() - area[1])/2;
  362. that.offsetLeft = (win.width() - area[0])/2;
  363. if(type){
  364. that.offsetTop = config.offset[0];
  365. that.offsetLeft = config.offset[1]||that.offsetLeft;
  366. } else if(config.offset !== 'auto'){
  367. if(config.offset === 't'){ //上
  368. that.offsetTop = 0;
  369. } else if(config.offset === 'r'){ //右
  370. that.offsetLeft = win.width() - area[0];
  371. } else if(config.offset === 'b'){ //下
  372. that.offsetTop = win.height() - area[1];
  373. } else if(config.offset === 'l'){ //左
  374. that.offsetLeft = 0;
  375. } else if(config.offset === 'lt'){ //左上角
  376. that.offsetTop = 0;
  377. that.offsetLeft = 0;
  378. } else if(config.offset === 'lb'){ //左下角
  379. that.offsetTop = win.height() - area[1];
  380. that.offsetLeft = 0;
  381. } else if(config.offset === 'rt'){ //右上角
  382. that.offsetTop = 0;
  383. that.offsetLeft = win.width() - area[0];
  384. } else if(config.offset === 'rb'){ //右下角
  385. that.offsetTop = win.height() - area[1];
  386. that.offsetLeft = win.width() - area[0];
  387. } else {
  388. that.offsetTop = config.offset;
  389. }
  390. }
  391. if(!config.fixed){
  392. that.offsetTop = /%$/.test(that.offsetTop) ?
  393. win.height()*parseFloat(that.offsetTop)/100
  394. : parseFloat(that.offsetTop);
  395. that.offsetLeft = /%$/.test(that.offsetLeft) ?
  396. win.width()*parseFloat(that.offsetLeft)/100
  397. : parseFloat(that.offsetLeft);
  398. that.offsetTop += win.scrollTop();
  399. that.offsetLeft += win.scrollLeft();
  400. }
  401. if(layero.attr('minLeft')){
  402. that.offsetTop = win.height() - (layero.find(doms[1]).outerHeight() || 0);
  403. that.offsetLeft = layero.css('left');
  404. }
  405. layero.css({top: that.offsetTop, left: that.offsetLeft});
  406. };
  407. //Tips
  408. Class.pt.tips = function(){
  409. var that = this, config = that.config, layero = that.layero;
  410. var layArea = [layero.outerWidth(), layero.outerHeight()], follow = $(config.follow);
  411. if(!follow[0]) follow = $('body');
  412. var goal = {
  413. width: follow.outerWidth(),
  414. height: follow.outerHeight(),
  415. top: follow.offset().top,
  416. left: follow.offset().left
  417. }, tipsG = layero.find('.layui-layer-TipsG');
  418. var guide = config.tips[0];
  419. config.tips[1] || tipsG.remove();
  420. goal.autoLeft = function(){
  421. if(goal.left + layArea[0] - win.width() > 0){
  422. goal.tipLeft = goal.left + goal.width - layArea[0];
  423. tipsG.css({right: 12, left: 'auto'});
  424. } else {
  425. goal.tipLeft = goal.left;
  426. };
  427. };
  428. //辨别tips的方位
  429. goal.where = [function(){ //上
  430. goal.autoLeft();
  431. goal.tipTop = goal.top - layArea[1] - 10;
  432. tipsG.removeClass('layui-layer-TipsB').addClass('layui-layer-TipsT').css('border-right-color', config.tips[1]);
  433. }, function(){ //右
  434. goal.tipLeft = goal.left + goal.width + 10;
  435. goal.tipTop = goal.top;
  436. tipsG.removeClass('layui-layer-TipsL').addClass('layui-layer-TipsR').css('border-bottom-color', config.tips[1]);
  437. }, function(){ //下
  438. goal.autoLeft();
  439. goal.tipTop = goal.top + goal.height + 10;
  440. tipsG.removeClass('layui-layer-TipsT').addClass('layui-layer-TipsB').css('border-right-color', config.tips[1]);
  441. }, function(){ //左
  442. goal.tipLeft = goal.left - layArea[0] - 10;
  443. goal.tipTop = goal.top;
  444. tipsG.removeClass('layui-layer-TipsR').addClass('layui-layer-TipsL').css('border-bottom-color', config.tips[1]);
  445. }];
  446. goal.where[guide-1]();
  447. /* 8*2为小三角形占据的空间 */
  448. if(guide === 1){
  449. goal.top - (win.scrollTop() + layArea[1] + 8*2) < 0 && goal.where[2]();
  450. } else if(guide === 2){
  451. win.width() - (goal.left + goal.width + layArea[0] + 8*2) > 0 || goal.where[3]()
  452. } else if(guide === 3){
  453. (goal.top - win.scrollTop() + goal.height + layArea[1] + 8*2) - win.height() > 0 && goal.where[0]();
  454. } else if(guide === 4){
  455. layArea[0] + 8*2 - goal.left > 0 && goal.where[1]()
  456. }
  457. layero.find('.'+doms[5]).css({
  458. 'background-color': config.tips[1],
  459. 'padding-right': (config.closeBtn ? '30px' : '')
  460. });
  461. layero.css({
  462. left: goal.tipLeft - (config.fixed ? win.scrollLeft() : 0),
  463. top: goal.tipTop - (config.fixed ? win.scrollTop() : 0)
  464. });
  465. }
  466. //拖拽层
  467. Class.pt.move = function(){
  468. var that = this
  469. ,config = that.config
  470. ,_DOC = $(document)
  471. ,layero = that.layero
  472. ,moveElem = layero.find(config.move)
  473. ,resizeElem = layero.find('.layui-layer-resize')
  474. ,dict = {};
  475. if(config.move){
  476. moveElem.css('cursor', 'move');
  477. }
  478. moveElem.on('mousedown', function(e){
  479. e.preventDefault();
  480. if(config.move){
  481. dict.moveStart = true;
  482. dict.offset = [
  483. e.clientX - parseFloat(layero.css('left'))
  484. ,e.clientY - parseFloat(layero.css('top'))
  485. ];
  486. ready.moveElem.css('cursor', 'move').show();
  487. }
  488. });
  489. resizeElem.on('mousedown', function(e){
  490. e.preventDefault();
  491. dict.resizeStart = true;
  492. dict.offset = [e.clientX, e.clientY];
  493. dict.area = [
  494. layero.outerWidth()
  495. ,layero.outerHeight()
  496. ];
  497. ready.moveElem.css('cursor', 'se-resize').show();
  498. });
  499. _DOC.on('mousemove', function(e){
  500. //拖拽移动
  501. if(dict.moveStart){
  502. var X = e.clientX - dict.offset[0]
  503. ,Y = e.clientY - dict.offset[1]
  504. ,fixed = layero.css('position') === 'fixed';
  505. e.preventDefault();
  506. dict.stX = fixed ? 0 : win.scrollLeft();
  507. dict.stY = fixed ? 0 : win.scrollTop();
  508. //控制元素不被拖出窗口外
  509. if(!config.moveOut){
  510. var setRig = win.width() - layero.outerWidth() + dict.stX
  511. ,setBot = win.height() - layero.outerHeight() + dict.stY;
  512. X < dict.stX && (X = dict.stX);
  513. X > setRig && (X = setRig);
  514. Y < dict.stY && (Y = dict.stY);
  515. Y > setBot && (Y = setBot);
  516. }
  517. layero.css({
  518. left: X
  519. ,top: Y
  520. });
  521. }
  522. //Resize
  523. if(config.resize && dict.resizeStart){
  524. var X = e.clientX - dict.offset[0]
  525. ,Y = e.clientY - dict.offset[1];
  526. e.preventDefault();
  527. layer.style(that.index, {
  528. width: dict.area[0] + X
  529. ,height: dict.area[1] + Y
  530. })
  531. dict.isResize = true;
  532. config.resizing && config.resizing(layero);
  533. }
  534. }).on('mouseup', function(e){
  535. if(dict.moveStart){
  536. delete dict.moveStart;
  537. ready.moveElem.hide();
  538. config.moveEnd && config.moveEnd(layero);
  539. }
  540. if(dict.resizeStart){
  541. delete dict.resizeStart;
  542. ready.moveElem.hide();
  543. }
  544. });
  545. return that;
  546. };
  547. Class.pt.callback = function(){
  548. var that = this, layero = that.layero, config = that.config;
  549. that.openLayer();
  550. if(config.success){
  551. if(config.type == 2){
  552. layero.find('iframe').on('load', function(){
  553. config.success(layero, that.index);
  554. });
  555. } else {
  556. config.success(layero, that.index);
  557. }
  558. }
  559. layer.ie == 6 && that.IE6(layero);
  560. //按钮
  561. layero.find('.'+ doms[6]).children('a').on('click', function(){
  562. var index = $(this).index();
  563. if(index === 0){
  564. if(config.yes){
  565. config.yes(that.index, layero)
  566. } else if(config['btn1']){
  567. config['btn1'](that.index, layero)
  568. } else {
  569. layer.close(that.index);
  570. }
  571. } else {
  572. var close = config['btn'+(index+1)] && config['btn'+(index+1)](that.index, layero);
  573. close === false || layer.close(that.index);
  574. }
  575. });
  576. //按钮焦点
  577. if (typeof config.focusBtn === 'number') {
  578. var focusBtn = layero.find('.' + doms[6]).children('a').eq(config.focusBtn);
  579. if (focusBtn.size() > 0) {
  580. var position = focusBtn.position();
  581. var style = {
  582. width: focusBtn.outerWidth(), height: focusBtn.outerHeight(), left: position.left, top: position.top,
  583. marginTop: focusBtn.css("marginTop"),
  584. marginLeft: focusBtn.css("marginLeft"),
  585. };
  586. var confirmBtn = $("<button class='layui-layer-confirm'></button>").css(style);
  587. layero.find('.' + doms[6]).after(confirmBtn);
  588. confirmBtn.focus().click(function () {
  589. focusBtn.trigger("click");
  590. return false;
  591. }).on("focus blur", function (e) {
  592. focusBtn.toggleClass("focus", e.type === "focus");
  593. });
  594. }
  595. }
  596. //取消
  597. function cancel(){
  598. var close = config.cancel && config.cancel(that.index, layero);
  599. close === false || layer.close(that.index);
  600. }
  601. //右上角关闭回调
  602. layero.find('.'+ doms[7]).on('click', cancel);
  603. //点遮罩关闭
  604. if(config.shadeClose){
  605. $('#layui-layer-shade'+ that.index).on('click', function(){
  606. layer.close(that.index);
  607. });
  608. }
  609. //最小化
  610. layero.find('.layui-layer-min').on('click', function(){
  611. var min = config.min && config.min(layero);
  612. min === false || layer.min(that.index, config);
  613. });
  614. //全屏/还原
  615. layero.find('.layui-layer-max').on('click', function(){
  616. if($(this).hasClass('layui-layer-maxmin')){
  617. layer.restore(that.index);
  618. config.restore && config.restore(layero);
  619. } else {
  620. layer.full(that.index, config);
  621. setTimeout(function(){
  622. config.full && config.full(layero);
  623. }, 100);
  624. }
  625. });
  626. config.end && (ready.end[that.index] = config.end);
  627. };
  628. //for ie6 恢复select
  629. ready.reselect = function(){
  630. $.each($('select'), function(index , value){
  631. var sthis = $(this);
  632. if(!sthis.parents('.'+doms[0])[0]){
  633. (sthis.attr('layer') == 1 && $('.'+doms[0]).length < 1) && sthis.removeAttr('layer').show();
  634. }
  635. sthis = null;
  636. });
  637. };
  638. Class.pt.IE6 = function(layero){
  639. //隐藏select
  640. $('select').each(function(index , value){
  641. var sthis = $(this);
  642. if(!sthis.parents('.'+doms[0])[0]){
  643. sthis.css('display') === 'none' || sthis.attr({'layer' : '1'}).hide();
  644. }
  645. sthis = null;
  646. });
  647. };
  648. //需依赖原型的对外方法
  649. Class.pt.openLayer = function(){
  650. var that = this;
  651. //置顶当前窗口
  652. layer.zIndex = that.config.zIndex;
  653. layer.setTop = function(layero){
  654. var setZindex = function(){
  655. layer.zIndex++;
  656. layero.css('z-index', layer.zIndex + 1);
  657. };
  658. layer.zIndex = parseInt(layero[0].style.zIndex);
  659. layero.on('mousedown', setZindex);
  660. return layer.zIndex;
  661. };
  662. };
  663. ready.record = function(layero){
  664. var area = [
  665. layero.width(),
  666. layero.height(),
  667. layero.position().top,
  668. layero.position().left + parseFloat(layero.css('margin-left'))
  669. ];
  670. layero.find('.layui-layer-max').addClass('layui-layer-maxmin');
  671. layero.attr({area: area});
  672. };
  673. ready.rescollbar = function(index){
  674. if(doms.html.attr('layer-full') == index){
  675. if(doms.html[0].style.removeProperty){
  676. doms.html[0].style.removeProperty('overflow');
  677. } else {
  678. doms.html[0].style.removeAttribute('overflow');
  679. }
  680. doms.html.removeAttr('layer-full');
  681. }
  682. };
  683. /** 内置成员 */
  684. window.layer = layer;
  685. //获取子iframe的DOM
  686. layer.getChildFrame = function(selector, index){
  687. index = index || $('.'+doms[4]).attr('times');
  688. return $('#'+ doms[0] + index).find('iframe').contents().find(selector);
  689. };
  690. //得到当前iframe层的索引,子iframe时使用
  691. layer.getFrameIndex = function(name){
  692. return $('#'+ name).parents('.'+doms[4]).attr('times');
  693. };
  694. //iframe层自适应宽高
  695. layer.iframeAuto = function(index){
  696. if(!index) return;
  697. var heg = layer.getChildFrame('html', index).outerHeight();
  698. var layero = $('#'+ doms[0] + index);
  699. var titHeight = layero.find(doms[1]).outerHeight() || 0;
  700. var btnHeight = layero.find('.'+doms[6]).outerHeight() || 0;
  701. layero.css({height: heg + titHeight + btnHeight});
  702. layero.find('iframe').css({height: heg});
  703. };
  704. //重置iframe url
  705. layer.iframeSrc = function(index, url){
  706. $('#'+ doms[0] + index).find('iframe').attr('src', url);
  707. };
  708. //设定层的样式
  709. layer.style = function(index, options, limit){
  710. var layero = $('#'+ doms[0] + index)
  711. ,contElem = layero.find('.layui-layer-content')
  712. ,type = layero.attr('type')
  713. ,titHeight = layero.find(doms[1]).outerHeight() || 0
  714. ,btnHeight = layero.find('.'+doms[6]).outerHeight() || 0
  715. ,minLeft = layero.attr('minLeft');
  716. if(type === ready.type[3] || type === ready.type[4]){
  717. return;
  718. }
  719. if(!limit){
  720. if(parseFloat(options.width) <= 260){
  721. options.width = 260;
  722. };
  723. if(parseFloat(options.height) - titHeight - btnHeight <= 64){
  724. options.height = 64 + titHeight + btnHeight;
  725. };
  726. }
  727. layero.css(options);
  728. btnHeight = layero.find('.'+doms[6]).outerHeight();
  729. if(type === ready.type[2]){
  730. layero.find('iframe').css({
  731. height: parseFloat(options.height) - titHeight - btnHeight
  732. });
  733. } else {
  734. contElem.css({
  735. height: parseFloat(options.height) - titHeight - btnHeight
  736. - parseFloat(contElem.css('padding-top'))
  737. - parseFloat(contElem.css('padding-bottom'))
  738. })
  739. }
  740. };
  741. //最小化
  742. layer.min = function(index, options){
  743. var layero = $('#'+ doms[0] + index)
  744. ,titHeight = layero.find(doms[1]).outerHeight() || 0
  745. ,left = layero.attr('minLeft') || (181*ready.minIndex)+'px'
  746. ,position = layero.css('position');
  747. ready.record(layero);
  748. if(ready.minLeft[0]){
  749. left = ready.minLeft[0];
  750. ready.minLeft.shift();
  751. }
  752. layero.attr('position', position);
  753. layer.style(index, {
  754. width: 180
  755. ,height: titHeight
  756. ,left: left
  757. ,top: win.height() - titHeight
  758. ,position: 'fixed'
  759. ,overflow: 'hidden'
  760. }, true);
  761. layero.find('.layui-layer-min').hide();
  762. layero.attr('type') === 'page' && layero.find(doms[4]).hide();
  763. ready.rescollbar(index);
  764. if(!layero.attr('minLeft')){
  765. ready.minIndex++;
  766. }
  767. layero.attr('minLeft', left);
  768. };
  769. //还原
  770. layer.restore = function(index){
  771. var layero = $('#'+ doms[0] + index), area = layero.attr('area').split(',');
  772. var type = layero.attr('type');
  773. layer.style(index, {
  774. width: parseFloat(area[0]),
  775. height: parseFloat(area[1]),
  776. top: parseFloat(area[2]),
  777. left: parseFloat(area[3]),
  778. position: layero.attr('position'),
  779. overflow: 'visible'
  780. }, true);
  781. layero.find('.layui-layer-max').removeClass('layui-layer-maxmin');
  782. layero.find('.layui-layer-min').show();
  783. layero.attr('type') === 'page' && layero.find(doms[4]).show();
  784. ready.rescollbar(index);
  785. };
  786. //全屏
  787. layer.full = function(index){
  788. var layero = $('#'+ doms[0] + index), timer;
  789. ready.record(layero);
  790. if(!doms.html.attr('layer-full')){
  791. doms.html.css('overflow','hidden').attr('layer-full', index);
  792. }
  793. clearTimeout(timer);
  794. timer = setTimeout(function(){
  795. var isfix = layero.css('position') === 'fixed';
  796. layer.style(index, {
  797. top: isfix ? 0 : win.scrollTop(),
  798. left: isfix ? 0 : win.scrollLeft(),
  799. width: win.width(),
  800. height: win.height()
  801. }, true);
  802. layero.find('.layui-layer-min').hide();
  803. }, 100);
  804. };
  805. //改变title
  806. layer.title = function(name, index){
  807. var title = $('#'+ doms[0] + (index||layer.index)).find(doms[1]);
  808. title.html(name);
  809. };
  810. //关闭layer总方法
  811. layer.close = function(index){
  812. var layero = $('#'+ doms[0] + index), type = layero.attr('type'), closeAnim = 'layer-anim-close';
  813. if(!layero[0]) return;
  814. var WRAP = 'layui-layer-wrap', remove = function(){
  815. if(type === ready.type[1] && layero.attr('conType') === 'object'){
  816. layero.children(':not(.'+ doms[5] +')').remove();
  817. var wrap = layero.find('.'+WRAP);
  818. for(var i = 0; i < 2; i++){
  819. wrap.unwrap();
  820. }
  821. wrap.css('display', wrap.data('display')).removeClass(WRAP);
  822. } else {
  823. //低版本IE 回收 iframe
  824. if(type === ready.type[2]){
  825. try {
  826. var iframe = $('#'+doms[4]+index)[0];
  827. iframe.contentWindow.document.write('');
  828. iframe.contentWindow.close();
  829. layero.find('.'+doms[5])[0].removeChild(iframe);
  830. } catch(e){}
  831. }
  832. layero[0].innerHTML = '';
  833. layero.remove();
  834. }
  835. typeof ready.end[index] === 'function' && ready.end[index]();
  836. delete ready.end[index];
  837. };
  838. if(layero.data('isOutAnim')){
  839. layero.addClass('layer-anim '+ closeAnim);
  840. }
  841. $('#layui-layer-moves, #layui-layer-shade' + index).remove();
  842. layer.ie == 6 && ready.reselect();
  843. ready.rescollbar(index);
  844. if(layero.attr('minLeft')){
  845. ready.minIndex--;
  846. ready.minLeft.push(layero.attr('minLeft'));
  847. }
  848. if((layer.ie && layer.ie < 10) || !layero.data('isOutAnim')){
  849. remove()
  850. } else {
  851. setTimeout(function(){
  852. remove();
  853. }, 200);
  854. }
  855. };
  856. //关闭所有层
  857. layer.closeAll = function(type){
  858. $.each($('.'+doms[0]), function(){
  859. var othis = $(this);
  860. var is = type ? (othis.attr('type') === type) : 1;
  861. is && layer.close(othis.attr('times'));
  862. is = null;
  863. });
  864. };
  865. /**
  866. 拓展模块,layui开始合并在一起
  867. */
  868. var cache = layer.cache||{}, skin = function(type){
  869. return (cache.skin ? (' ' + cache.skin + ' ' + cache.skin + '-'+type) : '');
  870. };
  871. //仿系统prompt
  872. layer.prompt = function(options, yes){
  873. var style = '';
  874. options = options || {};
  875. if(typeof options === 'function') yes = options;
  876. if(options.area){
  877. var area = options.area;
  878. style = 'style="width: '+ area[0] +'; height: '+ area[1] + ';"';
  879. delete options.area;
  880. }
  881. var prompt, content = options.formType == 2 ? '<textarea class="layui-layer-input"' + style +'>' + (options.value||'') +'</textarea>' : function(){
  882. return '<input type="'+ (options.formType == 1 ? 'password' : 'text') +'" class="layui-layer-input" value="'+ (options.value||'') +'">';
  883. }();
  884. var success = options.success;
  885. delete options.success;
  886. return layer.open($.extend({
  887. type: 1
  888. ,btn: ['&#x786E;&#x5B9A;','&#x53D6;&#x6D88;']
  889. ,content: content
  890. ,skin: 'layui-layer-prompt' + skin('prompt')
  891. ,maxWidth: win.width()
  892. ,success: function(layero){
  893. prompt = layero.find('.layui-layer-input');
  894. prompt.focus();
  895. typeof success === 'function' && success(layero);
  896. }
  897. ,resize: false
  898. ,yes: function(index){
  899. var value = prompt.val();
  900. if(value === ''){
  901. prompt.focus();
  902. } else if(value.length > (options.maxlength||500)) {
  903. layer.tips('&#x6700;&#x591A;&#x8F93;&#x5165;'+ (options.maxlength || 500) +'&#x4E2A;&#x5B57;&#x6570;', prompt, {tips: 1});
  904. } else {
  905. yes && yes(value, index, prompt);
  906. }
  907. }
  908. }, options));
  909. };
  910. //tab层
  911. layer.tab = function(options){
  912. options = options || {};
  913. var tab = options.tab || {}
  914. ,THIS = 'layui-this'
  915. ,success = options.success;
  916. delete options.success;
  917. return layer.open($.extend({
  918. type: 1,
  919. skin: 'layui-layer-tab' + skin('tab'),
  920. resize: false,
  921. title: function(){
  922. var len = tab.length, ii = 1, str = '';
  923. if(len > 0){
  924. str = '<span class="'+ THIS +'">'+ tab[0].title +'</span>';
  925. for(; ii < len; ii++){
  926. str += '<span>'+ tab[ii].title +'</span>';
  927. }
  928. }
  929. return str;
  930. }(),
  931. content: '<ul class="layui-layer-tabmain">'+ function(){
  932. var len = tab.length, ii = 1, str = '';
  933. if(len > 0){
  934. str = '<li class="layui-layer-tabli '+ THIS +'">'+ (tab[0].content || 'no content') +'</li>';
  935. for(; ii < len; ii++){
  936. str += '<li class="layui-layer-tabli">'+ (tab[ii].content || 'no content') +'</li>';
  937. }
  938. }
  939. return str;
  940. }() +'</ul>',
  941. success: function(layero){
  942. var btn = layero.find('.layui-layer-title').children();
  943. var main = layero.find('.layui-layer-tabmain').children();
  944. btn.on('mousedown', function(e){
  945. e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true;
  946. var othis = $(this), index = othis.index();
  947. othis.addClass(THIS).siblings().removeClass(THIS);
  948. main.eq(index).show().siblings().hide();
  949. typeof options.change === 'function' && options.change(index);
  950. });
  951. typeof success === 'function' && success(layero);
  952. }
  953. }, options));
  954. };
  955. //相册层
  956. layer.photos = function(options, loop, key){
  957. var dict = {};
  958. options = options || {};
  959. if(!options.photos) return;
  960. var type = options.photos.constructor === Object;
  961. var photos = type ? options.photos : {}, data = photos.data || [];
  962. var start = photos.start || 0;
  963. dict.imgIndex = (start|0) + 1;
  964. options.img = options.img || 'img';
  965. var success = options.success;
  966. delete options.success;
  967. if(!type){ //页面直接获取
  968. var parent = $(options.photos), pushData = function(){
  969. data = [];
  970. parent.find(options.img).each(function(index){
  971. var othis = $(this);
  972. othis.attr('layer-index', index);
  973. data.push({
  974. alt: othis.attr('alt'),
  975. pid: othis.attr('layer-pid'),
  976. src: othis.attr('layer-src') || othis.attr('src'),
  977. thumb: othis.attr('src')
  978. });
  979. })
  980. };
  981. pushData();
  982. if (data.length === 0) return;
  983. loop || parent.on('click', options.img, function(){
  984. var othis = $(this), index = othis.attr('layer-index');
  985. layer.photos($.extend(options, {
  986. photos: {
  987. start: index,
  988. data: data,
  989. tab: options.tab
  990. },
  991. full: options.full
  992. }), true);
  993. pushData();
  994. })
  995. //不直接弹出
  996. if(!loop) return;
  997. } else if (data.length === 0){
  998. return layer.msg('&#x6CA1;&#x6709;&#x56FE;&#x7247;');
  999. }
  1000. //上一张
  1001. dict.imgprev = function(key){
  1002. dict.imgIndex--;
  1003. if(dict.imgIndex < 1){
  1004. dict.imgIndex = data.length;
  1005. }
  1006. dict.tabimg(key);
  1007. };
  1008. //下一张
  1009. dict.imgnext = function(key,errorMsg){
  1010. dict.imgIndex++;
  1011. if(dict.imgIndex > data.length){
  1012. dict.imgIndex = 1;
  1013. if (errorMsg) {return};
  1014. }
  1015. dict.tabimg(key)
  1016. };
  1017. //方向键
  1018. dict.keyup = function(event){
  1019. if(!dict.end){
  1020. var code = event.keyCode;
  1021. event.preventDefault();
  1022. if(code === 37){
  1023. dict.imgprev(true);
  1024. } else if(code === 39) {
  1025. dict.imgnext(true);
  1026. } else if(code === 27) {
  1027. layer.close(dict.index);
  1028. }
  1029. }
  1030. }
  1031. //切换
  1032. dict.tabimg = function(key){
  1033. if(data.length <= 1) return;
  1034. photos.start = dict.imgIndex - 1;
  1035. layer.close(dict.index);
  1036. return layer.photos(options, true, key);
  1037. setTimeout(function(){
  1038. layer.photos(options, true, key);
  1039. }, 200);
  1040. }
  1041. //一些动作
  1042. dict.event = function(){
  1043. dict.bigimg.hover(function(){
  1044. dict.imgsee.show();
  1045. }, function(){
  1046. dict.imgsee.hide();
  1047. });
  1048. dict.bigimg.find('.layui-layer-imgprev').on('click', function(event){
  1049. event.preventDefault();
  1050. dict.imgprev();
  1051. });
  1052. dict.bigimg.find('.layui-layer-imgnext').on('click', function(event){
  1053. event.preventDefault();
  1054. dict.imgnext();
  1055. });
  1056. $(document).on('keyup', dict.keyup);
  1057. };
  1058. //图片预加载
  1059. function loadImage(url, callback, error) {
  1060. var img = new Image();
  1061. img.src = url;
  1062. if(img.complete){
  1063. return callback(img);
  1064. }
  1065. img.onload = function(){
  1066. img.onload = null;
  1067. callback(img);
  1068. };
  1069. img.onerror = function(e){
  1070. img.onerror = null;
  1071. error(e);
  1072. };
  1073. };
  1074. dict.loadi = layer.load(1, {
  1075. shade: 'shade' in options ? options["shade"] : 0.9,
  1076. scrollbar: 'scrollbar' in options ? options["scrollbar"] : false,
  1077. });
  1078. loadImage(data[start].src, function(img){
  1079. layer.close(dict.loadi);
  1080. dict.index = layer.open($.extend({
  1081. type: 1,
  1082. id: 'layui-layer-photos',
  1083. area: function(){
  1084. var imgarea = [img.width, img.height];
  1085. var winarea = [$(window).width() - 100, $(window).height() - 100];
  1086. //如果 实际图片的宽或者高比 屏幕大(那么进行缩放)
  1087. if(!options.full && (imgarea[0]>winarea[0]||imgarea[1]>winarea[1])){
  1088. var wh = [imgarea[0]/winarea[0],imgarea[1]/winarea[1]];//取宽度缩放比例、高度缩放比例
  1089. if(wh[0] > wh[1]){//取缩放比例最大的进行缩放
  1090. imgarea[0] = imgarea[0]/wh[0];
  1091. imgarea[1] = imgarea[1]/wh[0];
  1092. } else if(wh[0] < wh[1]){
  1093. imgarea[0] = imgarea[0]/wh[1];
  1094. imgarea[1] = imgarea[1]/wh[1];
  1095. }
  1096. }
  1097. return [imgarea[0]+'px', imgarea[1]+'px'];
  1098. }(),
  1099. title: false,
  1100. shade: 0.9,
  1101. shadeClose: true,
  1102. closeBtn: false,
  1103. move: '.layui-layer-phimg img',
  1104. moveType: 1,
  1105. scrollbar: false,
  1106. moveOut: true,
  1107. //anim: Math.random()*5|0,
  1108. isOutAnim: false,
  1109. skin: 'layui-layer-photos' + skin('photos'),
  1110. content: '<div class="layui-layer-phimg">'
  1111. +'<img src="'+ data[start].src +'" alt="'+ (data[start].alt||'') +'" layer-pid="'+ data[start].pid +'">'
  1112. +'<div class="layui-layer-imgsee">'
  1113. +(data.length > 1 ? '<span class="layui-layer-imguide"><a href="javascript:;" class="layui-layer-iconext layui-layer-imgprev"></a><a href="javascript:;" class="layui-layer-iconext layui-layer-imgnext"></a></span>' : '')
  1114. +'<div class="layui-layer-imgbar" style="display:'+ (key ? 'block' : '') +'"><span class="layui-layer-imgtit"><a href="javascript:;">'+ (data[start].alt||'') +'</a><em>'+ dict.imgIndex +'/'+ data.length +'</em></span></div>'
  1115. +'</div>'
  1116. +'</div>',
  1117. success: function(layero, index){
  1118. dict.bigimg = layero.find('.layui-layer-phimg');
  1119. dict.imgsee = layero.find('.layui-layer-imguide,.layui-layer-imgbar');
  1120. dict.event(layero);
  1121. options.tab && options.tab(data[start], layero);
  1122. typeof success === 'function' && success(layero);
  1123. }, end: function(){
  1124. dict.end = true;
  1125. $(document).off('keyup', dict.keyup);
  1126. }
  1127. }, options));
  1128. }, function(){
  1129. layer.close(dict.loadi);
  1130. layer.msg('&#x5F53;&#x524D;&#x56FE;&#x7247;&#x5730;&#x5740;&#x5F02;&#x5E38;<br>&#x662F;&#x5426;&#x7EE7;&#x7EED;&#x67E5;&#x770B;&#x4E0B;&#x4E00;&#x5F20;&#xFF1F;', {
  1131. time: 30000,
  1132. btn: ['&#x4E0B;&#x4E00;&#x5F20;', '&#x4E0D;&#x770B;&#x4E86;'],
  1133. yes: function(){
  1134. data.length > 1 && dict.imgnext(true,true);
  1135. }
  1136. });
  1137. });
  1138. };
  1139. //主入口
  1140. ready.run = function(_$){
  1141. $ = _$;
  1142. win = $(window);
  1143. doms.html = $('html');
  1144. layer.open = function(deliver){
  1145. var o = new Class(deliver);
  1146. return o.index;
  1147. };
  1148. };
  1149. //加载方式
  1150. window.layui && layui.define ? (
  1151. layer.ready()
  1152. ,layui.define('jquery', function(exports){ //layui加载
  1153. layer.path = layui.cache.dir;
  1154. ready.run(layui.$);
  1155. //暴露模块
  1156. window.layer = layer;
  1157. exports('layer', layer);
  1158. })
  1159. ) : (
  1160. (typeof define === 'function' && define.amd) ? define(['jquery'], function(){ //requirejs加载
  1161. ready.run(window.jQuery);
  1162. return layer;
  1163. }) : function(){ //普通script标签加载
  1164. ready.run(window.jQuery);
  1165. layer.ready();
  1166. }()
  1167. );
  1168. }(window);