layerjs-gm-with-css

layer.js 带有css样式

2021-10-27 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

بۇ قوليازمىنى بىۋاسىتە قاچىلاشقا بولمايدۇ. بۇ باشقا قوليازمىلارنىڭ ئىشلىتىشى ئۈچۈن تەمىنلەنگەن ئامبار بولۇپ، ئىشلىتىش ئۈچۈن مېتا كۆرسەتمىسىگە قىستۇرىدىغان كود: // @require https://update.greatest.deepsurf.us/scripts/434540/982891/layerjs-gm-with-css.js

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