/**
@name:layer v3.1.2 web弹层组件
@author:贤心
@license:mit
*/
; !function (window, undefined) {
"use strict";
var islayui = window.layui && layui.define, $, win, ready = {
getpath: function () {
var jspath = document.currentscript ? document.currentscript.src : function () {
var js = document.scripts
, last = js.length - 1
, src;
for (var i = last; i > 0; i--) {
if (js[i].readystate === 'interactive') {
src = js[i].src;
break;
}
}
return src || js[last].src;
}();
return jspath.substring(0, jspath.lastindexof('/') + 1);
}(),
config: {}, end: {}, minindex: 0, minleft: [],
btn: ['确定', '取消'],
//五种原始层模式
type: ['dialog', 'page', 'iframe', 'loading', 'tips'],
//获取节点的style属性值
getstyle: function (node, name) {
var style = node.currentstyle ? node.currentstyle : window.getcomputedstyle(node, null);
return style[style.getpropertyvalue ? 'getpropertyvalue' : 'getattribute'](name);
},
//载入css配件
link: function (href, fn, cssname) {
//未设置路径,则不主动加载css
if (!layer.path) return;
var head = document.getelementsbytagname("head")[0], link = document.createelement('link');
if (typeof fn === 'string') cssname = fn;
var app = (cssname || href).replace(/\.|\//g, '');
var id = 'layuicss-' + app, timeout = 0;
link.rel = 'stylesheet';
link.href = layer.path + href;
link.id = id;
if (!document.getelementbyid(id)) {
head.appendchild(link);
}
if (typeof fn !== 'function') return;
//轮询css是否加载完毕
(function poll() {
if (++timeout > 8 * 1000 / 100) {
return window.console && console.error('layer.css: invalid');
};
parseint(ready.getstyle(document.getelementbyid(id), 'width')) === 1989 ? fn() : settimeout(poll, 100);
}());
}
};
//默认内置方法。
var layer = {
v: '3.1.1',
ie: function () { //ie版本
var agent = navigator.useragent.tolowercase();
return (!!window.activexobject || "activexobject" in window) ? (
(agent.match(/msie\s(\d+)/) || [])[1] || '11' //由于ie11并没有msie的标识
) : false;
}(),
index: (window.layer && window.layer.v) ? 100000 : 0,
path: ready.getpath,
config: function (options, fn) {
options = options || {};
layer.cache = ready.config = $.extend({}, ready.config, options);
layer.path = ready.config.path || layer.path;
typeof options.extend === 'string' && (options.extend = [options.extend]);
if (ready.config.path) layer.ready();
if (!options.extend) return this;
islayui
? layui.addcss('modules/layer/' + options.extend)
: ready.link('theme/' + options.extend);
return this;
},
//主体css等待事件
ready: function (callback) {
//var cssname = 'layer', ver = ''
//, path = (islayui ? 'modules/layer/' : 'theme/') + 'default/layer.css?v=' + layer.v + ver;
//islayui ? layui.addcss(path, callback, cssname) : ready.link(path, callback, cssname);
return this;
},
//各种快捷引用
alert: function (content, options, yes) {
var type = typeof options === 'function';
if (type) yes = options;
var index = layer.open($.extend({
content: content,
yes: yes
}, type ? {} : options));
top.window.focus();
$(top.window).unbind("keydown").keydown(function (e) {
if (e.keycode == 13 || e.keycode == 32) {
//模拟触发onclick事件
var check = $("#layui-layer" + index + " .layui-layer-btn0");
if (check[0].fireevent) {
check[0].fireevent('onclick');
}
else {
var evt = document.createevent("mouseevents");
evt.initmouseevent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
check[0].dispatchevent(evt);
}
return false;
}
else if (e.keycode == 27) {
layer.close(index);
return false;
}
})
return index;
},
confirm: function (content, options, yes, cancel) {
var type = typeof options === 'function';
if (type) {
cancel = yes;
yes = options;
}
var index = layer.open($.extend({
content: content,
btn: ready.btn,
yes: yes,
btn2: cancel
}, type ? {} : options));
top.window.focus();
$(top.window).unbind("keydown").keydown(function (e) {
if (e.keycode == 13 || e.keycode == 32) {
//模拟触发onclick事件
var check = $("#layui-layer" + index + " .layui-layer-btn0");
if (check[0].fireevent) {
check[0].fireevent('onclick');
}
else {
var evt = document.createevent("mouseevents");
evt.initmouseevent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
check[0].dispatchevent(evt);
}
return false;
}
else if (e.keycode == 27) {
//模拟触发onclick事件
var check = $("#layui-layer" + index + " .layui-layer-btn1");
if (check[0].fireevent) {
check[0].fireevent('onclick');
}
else {
var evt = document.createevent("mouseevents");
evt.initmouseevent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
check[0].dispatchevent(evt);
}
return false;
}
})
return index;
},
msg: function (content, options, end) { //最常用提示层
var type = typeof options === 'function', rskin = ready.config.skin;
var skin = (rskin ? rskin + ' ' + rskin + '-msg' : '') || 'layui-layer-msg';
var anim = doms.anim.length - 1;
if (type) end = options;
return layer.open($.extend({
content: content,
time: 3000,
shade: false,
skin: skin,
title: false,
closebtn: false,
btn: false,
resize: false,
end: end
}, (type && !ready.config.skin) ? {
skin: skin + ' layui-layer-hui',
anim: anim
} : function () {
options = options || {};
if (options.icon === -1 || options.icon === undefined && !ready.config.skin) {
options.skin = skin + ' ' + (options.skin || 'layui-layer-hui');
}
return options;
}()));
},
load: function (icon, options) {
return layer.open($.extend({
type: 3,
icon: icon || 0,
resize: false,
shade: 0.01
}, options));
},
tips: function (content, follow, options) {
return layer.open($.extend({
type: 4,
content: [content, follow],
closebtn: false,
time: 3000,
shade: false,
resize: false,
fixed: false,
maxwidth: 210
}, options));
}
};
var class = function (setings) {
var that = this;
that.index = ++layer.index;
that.config = $.extend({}, that.config, ready.config, setings);
document.body ? that.creat() : settimeout(function () {
that.creat();
}, 30);
};
class.pt = class.prototype;
//缓存常用字符
var doms = ['layui-layer', '.layui-layer-title', '.layui-layer-main', '.layui-layer-dialog', 'layui-layer-iframe', 'layui-layer-content', 'layui-layer-btn', 'layui-layer-close'];
doms.anim = ['layer-anim-00', 'layer-anim-01', 'layer-anim-02', 'layer-anim-03', 'layer-anim-04', 'layer-anim-05', 'layer-anim-06'];
//默认配置
class.pt.config = {
type: 0,
shade: 0.3,
fixed: true,
move: doms[1],
title: '信息',
offset: 'auto',
area: 'auto',
closebtn: 1,
time: 0, //0表示不自动关闭
zindex: 19891014,
maxwidth: 360,
anim: 0,
isoutanim: true,
icon: -1,
movetype: 1,
resize: true,
scrollbar: true, //是否允许浏览器滚动条
tips: 2
};
//容器
class.pt.vessel = function (contype, callback) {
var that = this, times = that.index, config = that.config;
var zindex = config.zindex + times, titype = typeof config.title === 'object';
var ismax = config.maxmin && (config.type === 1 || config.type === 2);
var titlehtml = (config.title ? '
'
+ (titype ? config.title[0] : config.title)
+ '
' : '');
config.zindex = zindex;
callback([
//遮罩
config.shade ? ('') : '',
//主体
''
+ (contype && config.type != 2 ? '' : titlehtml)
+ '
'
+ (config.type == 0 && config.icon !== -1 ? '' : '')
+ (config.type == 1 && contype ? '' : (config.content || ''))
+ '
'
+ '
' + function () {
var closebtn = ismax ? '' : '';
config.closebtn && (closebtn += '');
return closebtn;
}() + ''
+ (config.btn ? function () {
var button = '';
typeof config.btn === 'string' && (config.btn = [config.btn]);
for (var i = 0, len = config.btn.length; i < len; i++) {
button += '
' + config.btn[i] + ''
}
return '
' + button + '
'
}() : '')
+ (config.resize ? '
' : '')
+ '
'
], titlehtml, $(''));
return that;
};
//创建骨架
class.pt.creat = function () {
var that = this
, config = that.config
, times = that.index, nodeindex
, content = config.content
, contype = typeof content === 'object'
, body = $('body');
if (config.id && $('#' + config.id)[0]) return;
if (typeof config.area === 'string') {
config.area = config.area === 'auto' ? ['', ''] : [config.area, ''];
}
//anim兼容旧版shift
if (config.shift) {
config.anim = config.shift;
}
if (layer.ie == 6) {
config.fixed = false;
}
switch (config.type) {
case 0:
config.btn = ('btn' in config) ? config.btn : ready.btn[0];
layer.closeall('loading');
layer.closeall('dialog');
break;
case 2:
var content = config.content = contype ? config.content : [config.content, 'auto'];
config.content = '';
break;
case 3:
delete config.title;
delete config.closebtn;
config.icon === -1 && (config.icon === 0);
//layer.closeall('loading');
//layer.closeall('dialog');
break;
case 4:
contype || (config.content = [config.content, 'body']);
config.follow = config.content[1];
config.content = config.content[0] + '';
delete config.title;
config.tips = typeof config.tips === 'object' ? config.tips : [config.tips, true];
config.tipsmore || layer.closeall('tips');
break;
}
//建立容器
that.vessel(contype, function (html, titlehtml, moveelem) {
body.append(html[0]);
contype ? function () {
(config.type == 2 || config.type == 4) ? function () {
$('body').append(html[1]);
}() : function () {
if (!content.parents('.' + doms[0])[0]) {
content.data('display', content.css('display')).show().addclass('layui-layer-wrap').wrap(html[1]);
$('#' + doms[0] + times).find('.' + doms[5]).before(titlehtml);
}
}();
}() : body.append(html[1]);
$('.layui-layer-move')[0] || body.append(ready.moveelem = moveelem);
that.layero = $('#' + doms[0] + times);
config.scrollbar || doms.html.css('overflow', 'hidden').attr('layer-full', times);
}).auto(times);
//遮罩
$('#layui-layer-shade' + that.index).css({
'background-color': config.shade[1] || '#000'
, 'opacity': config.shade[0] || config.shade
});
config.type == 2 && layer.ie == 6 && that.layero.find('iframe').attr('src', content[0]);
//坐标自适应浏览器窗口尺寸
config.type == 4 ? that.tips() : that.offset();
if (config.fixed) {
win.on('resize', function () {
that.offset();
(/^\d+%$/.test(config.area[0]) || /^\d+%$/.test(config.area[1])) && that.auto(times);
config.type == 4 && that.tips();
});
}
config.time <= 0 || settimeout(function () {
layer.close(that.index)
}, config.time);
that.move().callback();
//为兼容jquery3.0的css动画影响元素尺寸计算
if (doms.anim[config.anim]) {
var animclass = 'layer-anim ' + doms.anim[config.anim];
that.layero.addclass(animclass).one('webkitanimationend mozanimationend msanimationend oanimationend animationend', function () {
$(this).removeclass(animclass);
});
};
//记录关闭动画
if (config.isoutanim) {
that.layero.data('isoutanim', true);
}
};
//自适应
class.pt.auto = function (index) {
var that = this, config = that.config, layero = $('#' + doms[0] + index);
if (config.area[0] === '' && config.maxwidth > 0) {
//为了修复ie7下一个让人难以理解的bug
if (layer.ie && layer.ie < 8 && config.btn) {
layero.width(layero.innerwidth());
}
layero.outerwidth() > config.maxwidth && layero.width(config.maxwidth);
}
var area = [layero.innerwidth(), layero.innerheight()]
, titheight = layero.find(doms[1]).outerheight() || 0
, btnheight = layero.find('.' + doms[6]).outerheight() || 0
, setheight = function (elem) {
elem = layero.find(elem);
elem.height(area[1] - titheight - btnheight - 2 * (parsefloat(elem.css('padding-top')) | 0));
};
switch (config.type) {
case 2:
setheight('iframe');
break;
default:
if (config.area[1] === '') {
if (config.maxheight > 0 && layero.outerheight() > config.maxheight) {
area[1] = config.maxheight;
setheight('.' + doms[5]);
} else if (config.fixed && area[1] >= win.height()) {
area[1] = win.height();
setheight('.' + doms[5]);
}
} else {
setheight('.' + doms[5]);
}
break;
};
return that;
};
//计算坐标
class.pt.offset = function () {
var that = this, config = that.config, layero = that.layero;
var area = [layero.outerwidth(), layero.outerheight()];
var type = typeof config.offset === 'object';
that.offsettop = (win.height() - area[1]) / 2;
that.offsetleft = (win.width() - area[0]) / 2;
if (type) {
that.offsettop = config.offset[0];
that.offsetleft = config.offset[1] || that.offsetleft;
} else if (config.offset !== 'auto') {
if (config.offset === 't') { //上
that.offsettop = 0;
} else if (config.offset === 'r') { //右
that.offsetleft = win.width() - area[0];
} else if (config.offset === 'b') { //下
that.offsettop = win.height() - area[1];
} else if (config.offset === 'l') { //左
that.offsetleft = 0;
} else if (config.offset === 'lt') { //左上角
that.offsettop = 0;
that.offsetleft = 0;
} else if (config.offset === 'lb') { //左下角
that.offsettop = win.height() - area[1];
that.offsetleft = 0;
} else if (config.offset === 'rt') { //右上角
that.offsettop = 0;
that.offsetleft = win.width() - area[0];
} else if (config.offset === 'rb') { //右下角
that.offsettop = win.height() - area[1];
that.offsetleft = win.width() - area[0];
} else {
that.offsettop = config.offset;
}
}
if (!config.fixed) {
that.offsettop = /%$/.test(that.offsettop) ?
win.height() * parsefloat(that.offsettop) / 100
: parsefloat(that.offsettop);
that.offsetleft = /%$/.test(that.offsetleft) ?
win.width() * parsefloat(that.offsetleft) / 100
: parsefloat(that.offsetleft);
that.offsettop += win.scrolltop();
that.offsetleft += win.scrollleft();
}
if (layero.attr('minleft')) {
that.offsettop = win.height() - (layero.find(doms[1]).outerheight() || 0);
that.offsetleft = layero.css('left');
}
layero.css({ top: that.offsettop, left: that.offsetleft });
};
//tips
class.pt.tips = function () {
var that = this, config = that.config, layero = that.layero;
var layarea = [layero.outerwidth(), layero.outerheight()], follow = $(config.follow);
if (!follow[0]) follow = $('body');
var goal = {
width: follow.outerwidth(),
height: follow.outerheight(),
top: follow.offset().top,
left: follow.offset().left
}, tipsg = layero.find('.layui-layer-tipsg');
var guide = config.tips[0];
config.tips[1] || tipsg.remove();
goal.autoleft = function () {
if (goal.left + layarea[0] - win.width() > 0) {
goal.tipleft = goal.left + goal.width - layarea[0];
tipsg.css({ right: 12, left: 'auto' });
} else {
goal.tipleft = goal.left;
};
};
//辨别tips的方位
goal.where = [function () { //上
goal.autoleft();
goal.tiptop = goal.top - layarea[1] - 10;
tipsg.removeclass('layui-layer-tipsb').addclass('layui-layer-tipst').css('border-right-color', config.tips[1]);
}, function () { //右
goal.tipleft = goal.left + goal.width + 10;
goal.tiptop = goal.top;
tipsg.removeclass('layui-layer-tipsl').addclass('layui-layer-tipsr').css('border-bottom-color', config.tips[1]);
}, function () { //下
goal.autoleft();
goal.tiptop = goal.top + goal.height + 10;
tipsg.removeclass('layui-layer-tipst').addclass('layui-layer-tipsb').css('border-right-color', config.tips[1]);
}, function () { //左
goal.tipleft = goal.left - layarea[0] - 10;
goal.tiptop = goal.top;
tipsg.removeclass('layui-layer-tipsr').addclass('layui-layer-tipsl').css('border-bottom-color', config.tips[1]);
}];
goal.where[guide - 1]();
/* 8*2为小三角形占据的空间 */
if (guide === 1) {
goal.top - (win.scrolltop() + layarea[1] + 8 * 2) < 0 && goal.where[2]();
} else if (guide === 2) {
win.width() - (goal.left + goal.width + layarea[0] + 8 * 2) > 0 || goal.where[3]()
} else if (guide === 3) {
(goal.top - win.scrolltop() + goal.height + layarea[1] + 8 * 2) - win.height() > 0 && goal.where[0]();
} else if (guide === 4) {
layarea[0] + 8 * 2 - goal.left > 0 && goal.where[1]()
}
layero.find('.' + doms[5]).css({
'background-color': config.tips[1],
'padding-right': (config.closebtn ? '30px' : '')
});
layero.css({
left: goal.tipleft - (config.fixed ? win.scrollleft() : 0),
top: goal.tiptop - (config.fixed ? win.scrolltop() : 0)
});
}
//拖拽层
class.pt.move = function () {
var that = this
, config = that.config
, _doc = $(document)
, layero = that.layero
, moveelem = layero.find(config.move)
, resizeelem = layero.find('.layui-layer-resize')
, dict = {};
if (config.move) {
moveelem.css('cursor', 'move');
}
moveelem.on('mousedown', function (e) {
e.preventdefault();
if (config.move) {
dict.movestart = true;
dict.offset = [
e.clientx - parsefloat(layero.css('left'))
, e.clienty - parsefloat(layero.css('top'))
];
ready.moveelem.css('cursor', 'move').show();
}
});
resizeelem.on('mousedown', function (e) {
e.preventdefault();
dict.resizestart = true;
dict.offset = [e.clientx, e.clienty];
dict.area = [
layero.outerwidth()
, layero.outerheight()
];
ready.moveelem.css('cursor', 'se-resize').show();
});
_doc.on('mousemove', function (e) {
//拖拽移动
if (dict.movestart) {
var x = e.clientx - dict.offset[0]
, y = e.clienty - dict.offset[1]
, fixed = layero.css('position') === 'fixed';
e.preventdefault();
dict.stx = fixed ? 0 : win.scrollleft();
dict.sty = fixed ? 0 : win.scrolltop();
//控制元素不被拖出窗口外
if (!config.moveout) {
var setrig = win.width() - layero.outerwidth() + dict.stx
, setbot = win.height() - layero.outerheight() + dict.sty;
x < dict.stx && (x = dict.stx);
x > setrig && (x = setrig);
y < dict.sty && (y = dict.sty);
y > setbot && (y = setbot);
}
layero.css({
left: x
, top: y
});
}
//resize
if (config.resize && dict.resizestart) {
var x = e.clientx - dict.offset[0]
, y = e.clienty - dict.offset[1];
e.preventdefault();
layer.style(that.index, {
width: dict.area[0] + x
, height: dict.area[1] + y
})
dict.isresize = true;
config.resizing && config.resizing(layero);
}
}).on('mouseup', function (e) {
if (dict.movestart) {
delete dict.movestart;
ready.moveelem.hide();
config.moveend && config.moveend(layero);
}
if (dict.resizestart) {
delete dict.resizestart;
ready.moveelem.hide();
}
});
return that;
};
class.pt.callback = function () {
var that = this, layero = that.layero, config = that.config;
that.openlayer();
if (config.success) {
if (config.type == 2) {
layero.find('iframe').on('load', function () {
config.success(layero, that.index);
});
} else {
config.success(layero, that.index);
}
}
layer.ie == 6 && that.ie6(layero);
//按钮
layero.find('.' + doms[6]).children('a').on('click', function () {
var index = $(this).index();
if (index === 0) {
if (config.yes) {
var flag = config.yes(that.index, layero);
if (flag != false) layer.close(that.index);
} else if (config['btn1']) {
config['btn1'](that.index, layero);
} else {
layer.close(that.index);
}
} else {
var close = config['btn' + (index + 1)] && config['btn' + (index + 1)](that.index, layero);
close === false || layer.close(that.index);
}
});
//取消
function cancel() {
var close = config.cancel && config.cancel(that.index, layero);
close === false || layer.close(that.index);
}
//右上角关闭回调
layero.find('.' + doms[7]).on('click', cancel);
//点遮罩关闭
if (config.shadeclose) {
$('#layui-layer-shade' + that.index).on('click', function () {
layer.close(that.index);
});
}
//最小化
layero.find('.layui-layer-min').on('click', function () {
var min = config.min && config.min(layero);
min === false || layer.min(that.index, config);
});
//全屏/还原
layero.find('.layui-layer-max').on('click', function () {
if ($(this).hasclass('layui-layer-maxmin')) {
layer.restore(that.index);
config.restore && config.restore(layero);
} else {
layer.full(that.index, config);
settimeout(function () {
config.full && config.full(layero);
}, 100);
}
});
config.end && (ready.end[that.index] = config.end);
};
//for ie6 恢复select
ready.reselect = function () {
$.each($('select'), function (index, value) {
var sthis = $(this);
if (!sthis.parents('.' + doms[0])[0]) {
(sthis.attr('layer') == 1 && $('.' + doms[0]).length < 1) && sthis.removeattr('layer').show();
}
sthis = null;
});
};
class.pt.ie6 = function (layero) {
//隐藏select
$('select').each(function (index, value) {
var sthis = $(this);
if (!sthis.parents('.' + doms[0])[0]) {
sthis.css('display') === 'none' || sthis.attr({ 'layer': '1' }).hide();
}
sthis = null;
});
};
//需依赖原型的对外方法
class.pt.openlayer = function () {
var that = this;
//置顶当前窗口
layer.zindex = that.config.zindex;
layer.settop = function (layero) {
var setzindex = function () {
layer.zindex++;
layero.css('z-index', layer.zindex + 1);
};
layer.zindex = parseint(layero[0].style.zindex);
layero.on('mousedown', setzindex);
return layer.zindex;
};
};
ready.record = function (layero) {
var area = [
layero.width(),
layero.height(),
layero.position().top,
layero.position().left + parsefloat(layero.css('margin-left'))
];
layero.find('.layui-layer-max').addclass('layui-layer-maxmin');
layero.attr({ area: area });
};
ready.rescollbar = function (index) {
if (doms.html.attr('layer-full') == index) {
if (doms.html[0].style.removeproperty) {
doms.html[0].style.removeproperty('overflow');
} else {
doms.html[0].style.removeattribute('overflow');
}
doms.html.removeattr('layer-full');
}
};
/** 内置成员 */
window.layer = layer;
//获取子iframe的dom
layer.getchildframe = function (selector, index) {
index = index || $('.' + doms[4]).attr('times');
return $('#' + doms[0] + index).find('iframe').contents().find(selector);
};
//得到当前iframe层的索引,子iframe时使用
layer.getframeindex = function (name) {
return $('#' + name).parents('.' + doms[4]).attr('times');
};
//iframe层自适应宽高
layer.iframeauto = function (index) {
if (!index) return;
var heg = layer.getchildframe('html', index).outerheight();
var layero = $('#' + doms[0] + index);
var titheight = layero.find(doms[1]).outerheight() || 0;
var btnheight = layero.find('.' + doms[6]).outerheight() || 0;
layero.css({ height: heg + titheight + btnheight });
layero.find('iframe').css({ height: heg });
};
//重置iframe url
layer.iframesrc = function (index, url) {
$('#' + doms[0] + index).find('iframe').attr('src', url);
};
//设定层的样式
layer.style = function (index, options, limit) {
var layero = $('#' + doms[0] + index)
, contelem = layero.find('.layui-layer-content')
, type = layero.attr('type')
, titheight = layero.find(doms[1]).outerheight() || 0
, btnheight = layero.find('.' + doms[6]).outerheight() || 0
, minleft = layero.attr('minleft');
if (type === ready.type[3] || type === ready.type[4]) {
return;
}
if (!limit) {
if (parsefloat(options.width) <= 260) {
options.width = 260;
};
if (parsefloat(options.height) - titheight - btnheight <= 64) {
options.height = 64 + titheight + btnheight;
};
}
layero.css(options);
btnheight = layero.find('.' + doms[6]).outerheight();
if (type === ready.type[2]) {
layero.find('iframe').css({
height: parsefloat(options.height) - titheight - btnheight
});
} else {
contelem.css({
height: parsefloat(options.height) - titheight - btnheight
- parsefloat(contelem.css('padding-top'))
- parsefloat(contelem.css('padding-bottom'))
})
}
};
//最小化
layer.min = function (index, options) {
var layero = $('#' + doms[0] + index)
, titheight = layero.find(doms[1]).outerheight() || 0
, left = layero.attr('minleft') || (181 * ready.minindex) + 'px'
, position = layero.css('position');
ready.record(layero);
if (ready.minleft[0]) {
left = ready.minleft[0];
ready.minleft.shift();
}
layero.attr('position', position);
layer.style(index, {
width: 180
, height: titheight
, left: left
, top: win.height() - titheight
, position: 'fixed'
, overflow: 'hidden'
}, true);
layero.find('.layui-layer-min').hide();
layero.attr('type') === 'page' && layero.find(doms[4]).hide();
ready.rescollbar(index);
if (!layero.attr('minleft')) {
ready.minindex++;
}
layero.attr('minleft', left);
};
//还原
layer.restore = function (index) {
var layero = $('#' + doms[0] + index), area = layero.attr('area').split(',');
var type = layero.attr('type');
layer.style(index, {
width: parsefloat(area[0]),
height: parsefloat(area[1]),
top: parsefloat(area[2]),
left: parsefloat(area[3]),
position: layero.attr('position'),
overflow: 'visible'
}, true);
layero.find('.layui-layer-max').removeclass('layui-layer-maxmin');
layero.find('.layui-layer-min').show();
layero.attr('type') === 'page' && layero.find(doms[4]).show();
ready.rescollbar(index);
};
//全屏
layer.full = function (index) {
var layero = $('#' + doms[0] + index), timer;
ready.record(layero);
if (!doms.html.attr('layer-full')) {
doms.html.css('overflow', 'hidden').attr('layer-full', index);
}
cleartimeout(timer);
timer = settimeout(function () {
var isfix = layero.css('position') === 'fixed';
layer.style(index, {
top: isfix ? 0 : win.scrolltop(),
left: isfix ? 0 : win.scrollleft(),
width: win.width(),
height: win.height()
}, true);
layero.find('.layui-layer-min').hide();
}, 100);
};
//改变title
layer.title = function (name, index) {
var title = $('#' + doms[0] + (index || layer.index)).find(doms[1]);
title.html(name);
};
//关闭layer总方法
layer.close = function (index) {
var layero = $('#' + doms[0] + index), type = layero.attr('type'), closeanim = 'layer-anim-close';
if (!layero[0]) return;
var wrap = 'layui-layer-wrap', remove = function () {
if (type === ready.type[1] && layero.attr('contype') === 'object') {
layero.children(':not(.' + doms[5] + ')').remove();
var wrap = layero.find('.' + wrap);
for (var i = 0; i < 2; i++) {
wrap.unwrap();
}
wrap.css('display', wrap.data('display')).removeclass(wrap);
} else {
//低版本ie 回收 iframe
if (type === ready.type[2]) {
try {
var iframe = $('#' + doms[4] + index)[0];
iframe.contentwindow.document.write('');
iframe.contentwindow.close();
layero.find('.' + doms[5])[0].removechild(iframe);
} catch (e) { }
}
layero[0].innerhtml = '';
layero.remove();
}
typeof ready.end[index] === 'function' && ready.end[index]();
delete ready.end[index];
};
if (layero.data('isoutanim')) {
layero.addclass('layer-anim ' + closeanim);
}
$('#layui-layer-moves, #layui-layer-shade' + index).remove();
layer.ie == 6 && ready.reselect();
ready.rescollbar(index);
if (layero.attr('minleft')) {
ready.minindex--;
ready.minleft.push(layero.attr('minleft'));
}
if ((layer.ie && layer.ie < 10) || !layero.data('isoutanim')) {
remove()
} else {
settimeout(function () {
remove();
}, 200);
}
};
//关闭所有层
layer.closeall = function (type) {
$.each($('.' + doms[0]), function () {
var othis = $(this);
var is = type ? (othis.attr('type') === type) : 1;
is && layer.close(othis.attr('times'));
is = null;
});
};
/**
拓展模块,layui开始合并在一起
*/
var cache = layer.cache || {}, skin = function (type) {
return (cache.skin ? (' ' + cache.skin + ' ' + cache.skin + '-' + type) : '');
};
//仿系统prompt
layer.prompt = function (options, yes) {
var style = '';
options = options || {};
if (typeof options === 'function') yes = options;
if (options.area) {
var area = options.area;
style = 'style="width: ' + area[0] + '; height: ' + area[1] + ';"';
delete options.area;
}
var prompt, content = options.formtype == 2 ? '' : function () {
return '';
}();
var success = options.success;
delete options.success;
var index = layer.open($.extend({
type: 1
, btn: ['确定', '取消']
, content: content
, skin: 'layui-layer-prompt' + skin('prompt')
, maxwidth: win.width()
, success: function (layero) {
prompt = layero.find('.layui-layer-input');
prompt.val(options.value || '').focus();
typeof success === 'function' && success(layero);
}
, resize: false
, yes: function (index) {
var value = prompt.val();
if (value === '') {
prompt.focus();
} else if (value.length > (options.maxlength || 500)) {
layer.tips('最多输入' + (options.maxlength || 500) + '个字数', prompt, { tips: 1 });
} else {
yes && yes(value, index, prompt);
}
return false;
}
}, options));
top.window.focus();
$(top.window).unbind("keydown").keydown(function (e) {
if (e.keycode == 13) {
//模拟触发onclick事件
var check = $("#layui-layer" + index + " .layui-layer-btn0");
if (check[0].fireevent) {
check[0].fireevent('onclick');
}
else {
var evt = document.createevent("mouseevents");
evt.initmouseevent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
check[0].dispatchevent(evt);
}
return false;
}
else if (e.keycode == 27) {
//模拟触发onclick事件
var check = $("#layui-layer" + index + " .layui-layer-btn1");
if (check[0].fireevent) {
check[0].fireevent('onclick');
}
else {
var evt = document.createevent("mouseevents");
evt.initmouseevent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
check[0].dispatchevent(evt);
}
return false;
}
})
return index;
};
//tab层
layer.tab = function (options) {
options = options || {};
var tab = options.tab || {}
, this = 'layui-this'
, success = options.success;
delete options.success;
return layer.open($.extend({
type: 1,
skin: 'layui-layer-tab' + skin('tab'),
resize: false,
title: function () {
var len = tab.length, ii = 1, str = '';
if (len > 0) {
str = '' + tab[0].title + '';
for (; ii < len; ii++) {
str += '' + tab[ii].title + '';
}
}
return str;
}(),
content: '' + function () {
var len = tab.length, ii = 1, str = '';
if (len > 0) {
str = '- ' + (tab[0].content || 'no content') + '
';
for (; ii < len; ii++) {
str += '- ' + (tab[ii].content || 'no content') + '
';
}
}
return str;
}() + '
',
success: function (layero) {
var btn = layero.find('.layui-layer-title').children();
var main = layero.find('.layui-layer-tabmain').children();
btn.on('mousedown', function (e) {
e.stoppropagation ? e.stoppropagation() : e.cancelbubble = true;
var othis = $(this), index = othis.index();
othis.addclass(this).siblings().removeclass(this);
main.eq(index).show().siblings().hide();
typeof options.change === 'function' && options.change(index);
});
typeof success === 'function' && success(layero);
}
}, options));
};
//相册层
layer.photos = function (options, loop, key) {
var dict = {};
options = options || {};
if (!options.photos) return;
var type = options.photos.constructor === object;
var photos = type ? options.photos : {}, data = photos.data || [];
var start = photos.start || 0;
dict.imgindex = (start | 0) + 1;
options.img = options.img || 'img';
var success = options.success;
delete options.success;
if (!type) { //页面直接获取
var parent = $(options.photos), pushdata = function () {
data = [];
parent.find(options.img).each(function (index) {
var othis = $(this);
othis.attr('layer-index', index);
data.push({
alt: othis.attr('alt'),
pid: othis.attr('layer-pid'),
src: othis.attr('layer-src') || othis.attr('src'),
thumb: othis.attr('src')
});
})
};
pushdata();
if (data.length === 0) return;
loop || parent.on('click', options.img, function () {
var othis = $(this), index = othis.attr('layer-index');
layer.photos($.extend(options, {
photos: {
start: index,
data: data,
tab: options.tab
},
full: options.full
}), true);
pushdata();
})
//不直接弹出
if (!loop) return;
} else if (data.length === 0) {
return layer.msg('没有图片');
}
//上一张
dict.imgprev = function (key) {
dict.imgindex--;
if (dict.imgindex < 1) {
dict.imgindex = data.length;
}
dict.tabimg(key);
};
//下一张
dict.imgnext = function (key, errormsg) {
dict.imgindex++;
if (dict.imgindex > data.length) {
dict.imgindex = 1;
if (errormsg) { return };
}
dict.tabimg(key)
};
//方向键
dict.keyup = function (event) {
if (!dict.end) {
var code = event.keycode;
event.preventdefault();
if (code === 37) {
dict.imgprev(true);
} else if (code === 39) {
dict.imgnext(true);
} else if (code === 27) {
layer.close(dict.index);
}
}
}
//切换
dict.tabimg = function (key) {
if (data.length <= 1) return;
photos.start = dict.imgindex - 1;
layer.close(dict.index);
return layer.photos(options, true, key);
settimeout(function () {
layer.photos(options, true, key);
}, 200);
}
//一些)\" />")
dict.event = function () {
dict.bigimg.hover(function () {
dict.imgsee.show();
}, function () {
dict.imgsee.hide();
});
dict.bigimg.find('.layui-layer-imgprev').on('click', function (event) {
event.preventdefault();
dict.imgprev();
});
dict.bigimg.find('.layui-layer-imgnext').on('click', function (event) {
event.preventdefault();
dict.imgnext();
});
$(document).on('keyup', dict.keyup);
};
//图片预加载
function loadimage(url, callback, error) {
var img = new image();
img.src = url;
if (img.complete) {
return callback(img);
}
img.onload = function () {
img.onload = null;
callback(img);
};
img.onerror = function (e) {
img.onerror = null;
error(e);
};
};
dict.loadi = layer.load(1, {
shade: 'shade' in options ? false : 0.9,
scrollbar: false
});
loadimage(data[start].src, function (img) {
layer.close(dict.loadi);
dict.index = layer.open($.extend({
type: 1,
id: 'layui-layer-photos',
area: function () {
var imgarea = [img.width, img.height];
var winarea = [$(window).width() - 100, $(window).height() - 100];
//如果 实际图片的宽或者高比 屏幕大(那么进行缩放)
if (!options.full && (imgarea[0] > winarea[0] || imgarea[1] > winarea[1])) {
var wh = [imgarea[0] / winarea[0], imgarea[1] / winarea[1]];//取宽度缩放比例、高度缩放比例
if (wh[0] > wh[1]) {//取缩放比例最大的进行缩放
imgarea[0] = imgarea[0] / wh[0];
imgarea[1] = imgarea[1] / wh[0];
} else if (wh[0] < wh[1]) {
imgarea[0] = imgarea[0] / wh[1];
imgarea[1] = imgarea[1] / wh[1];
}
}
return [imgarea[0] + 'px', imgarea[1] + 'px'];
}(),
title: false,
shade: 0.9,
shadeclose: true,
closebtn: false,
move: '.layui-layer-phimg img',
movetype: 1,
scrollbar: false,
moveout: true,
//anim: math.random()*5|0,
isoutanim: false,
skin: 'layui-layer-photos' + skin('photos'),
content: ''
+ '
'
+ '
'
+ (data.length > 1 ? '
' : '')
+ '
'
+ '
'
+ '
',
success: function (layero, index) {
dict.bigimg = layero.find('.layui-layer-phimg');
dict.imgsee = layero.find('.layui-layer-imguide,.layui-layer-imgbar');
dict.event(layero);
options.tab && options.tab(data[start], layero);
typeof success === 'function' && success(layero);
}, end: function () {
dict.end = true;
$(document).off('keyup', dict.keyup);
}
}, options));
}, function () {
layer.close(dict.loadi);
layer.msg('当前图片地址异常
是否继续查看下一张?', {
time: 30000,
btn: ['下一张', '不看了'],
yes: function () {
data.length > 1 && dict.imgnext(true, true);
}
});
});
};
//主入口
ready.run = function (_$) {
$ = _$;
win = $(window);
doms.html = $('html');
layer.open = function (deliver) {
var o = new class(deliver);
return o.index;
};
};
//加载方式
window.layui && layui.define ? (
layer.ready()
, layui.define('jquery', function (exports) { //layui加载
layer.path = layui.cache.dir;
ready.run(layui.$);
//暴露模块
window.layer = layer;
exports('layer', layer);
})
) : (
(typeof define === 'function' && define.amd) ? define(['jquery'], function () { //requirejs加载
ready.run(window.jquery);
return layer;
}) : function () { //普通script标签加载
ready.run(window.jquery);
layer.ready();
}()
);
}(window);