layer

layer不加载css版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/371378/622010/layer.js

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址