layer.js 49 KB

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