上次针对layer.photos进行了小图显示不出的修正,现在需要进一步显示pdf文件
需配合pdfobject.js,该文件请自行网上查找
r.photos = function (t, n, a) {
function o(e, t, i) {
var tsrc=e;
var n = new Image;
return n.src = e,
n.complete ? t(n) : (n.onload = function () {
n.onload = null,
t(n)
}, void(n.onerror = function (e) {
//>>Add Mod by Shenjiujun 20220507
if(tsrc.substring(tsrc.lastIndexOf('.') + 1).toLowerCase() =="pdf")
{
t(n);
}
else
{
n.onerror = null,
i(e)
}
//<<Add Mod by Shenjiujun 20220507
}))
}
var l = {};
if (t = t || {}, t.photos) {
var s = t.photos.constructor === Object,
f = s ? t.photos : {},
d = f.data || [],
u = f.start || 0;
if (l.imgIndex = (0 | u) + 1, t.img = t.img || "img", s) {
if (0 === d.length)
return r.msg("没有图片")
} else {
var y = i(t.photos),
p = function () {
d = [],
y.find(t.img).each(function (e) {
var t = i(this);
t.attr("layer-index", e),
d.push({
alt: t.attr("alt"),
pid: t.attr("layer-pid"),
src: t.attr("layer-src") || t.attr("src"),
thumb: t.attr("src")
})
})
};
if (p(), 0 === d.length)
return;
if (n || y.on("click", t.img, function () {
var e = i(this),
n = e.attr("layer-index");
r.photos(i.extend(t, {
photos: {
start: n,
data: d,
tab: t.tab
},
full: t.full
}), !0),
p()
}), !n)
return
}
l.imgprev = function (e) {
l.imgIndex--,
l.imgIndex < 1 && (l.imgIndex = d.length),
l.tabimg(e)
},
l.imgnext = function (e, t) {
l.imgIndex++,
l.imgIndex > d.length && (l.imgIndex = 1, t) || l.tabimg(e)
},
l.keyup = function (e) {
if (!l.end) {
var t = e.keyCode;
e.preventDefault(),
37 === t ? l.imgprev(!0) : 39 === t ? l.imgnext(!0) : 27 === t && r.close(l.index)
}
},
l.tabimg = function (e) {
d.length <= 1 || (f.start = l.imgIndex - 1, r.close(l.index), r.photos(t, !0, e))
},
l.event = function () {
l.bigimg.hover(function () {
l.imgsee.show()
}, function () {
l.imgsee.hide()
}),
l.bigimg.find(".layui-layer-imgprev").on("click", function (e) {
e.preventDefault(),
l.imgprev()
}),
l.bigimg.find(".layui-layer-imgnext").on("click", function (e) {
e.preventDefault(),
l.imgnext()
}),
i(document).on("keyup", l.keyup)
},
l.loadi = r.load(1, {
shade: !("shade" in t) && .9,
scrollbar: !1
}),
o(d[u].src, function (n) {
//>>Add Mod by Shenjiujun 20220507
var imgHeight=n.height<200?200:n.height;//Mod by Shenjiujun 20220426 NewAdd
var lcontent;
var ispdf;
if(d[u].src.substring(d[u].src.lastIndexOf('.') + 1).toLowerCase() =="pdf")
{
$("#showpdf").remove();
lcontent='<div class="layui-layer-phimg" style="height:800px"><div id="showpdf" src="' + d[u].src + '11" style="width:1400px; height:800px;"></div><div class="layui-layer-imgsee">' + (d.length > 1 ? '<span class="layui-layer-imguide"><a href="javascript:;" class="layui-layer-iconext layui-layer-imgprev"></a><a href="javascript:;" class="layui-layer-iconext layui-layer-imgnext"></a></span>' : "") + '<div class="layui-layer-imgbar" style="display:' + (a ? "block" : "") + '"><span class="layui-layer-imgtit"><a href="javascript:;">' + (d[u].alt || "") + "</a><em>" + l.imgIndex + "/" + d.length + "</em></span></div></div></div>";
ispdf=true;
imgHeight=800;
}
else
{
lcontent='<div class="layui-layer-phimg" style="height:'+imgHeight+'px"><img src="' + d[u].src + '" alt="' + (d[u].alt || "") + '" layer-pid="' + d[u].pid + '"><div class="layui-layer-imgsee">' + (d.length > 1 ? '<span class="layui-layer-imguide"><a href="javascript:;" class="layui-layer-iconext layui-layer-imgprev"></a><a href="javascript:;" class="layui-layer-iconext layui-layer-imgnext"></a></span>' : "") + '<div class="layui-layer-imgbar" style="display:' + (a ? "block" : "") + '"><span class="layui-layer-imgtit"><a href="javascript:;">' + (d[u].alt || "") + "</a><em>" + l.imgIndex + "/" + d.length + "</em></span></div></div></div>";
ispdf=false;
}
//<<Add Mod by Shenjiujun 20220507
r.close(l.loadi),
l.index = r.open(i.extend({
type: 1,
area: function () {
//>>Mod by Shenjiujun 20220507 NewAdd
if(ispdf)
return [ "1400px","800px"];
else
{
var a = [n.width, n.height],
o = [i(e).width() - 100, i(e).height() - 100];
if (!t.full && (a[0] > o[0] || a[1] > o[1])) {
var r = [a[0] / o[0], a[1] / o[1]];
r[0] > r[1] ? (a[0] = a[0] / r[0], a[1] = a[1] / r[0]) : r[0] < r[1] && (a[0] = a[0] / r[1], a[1] = a[1] / r[1])
}
var a0=a[0]<200?200:a[0];//Mod by Shenjiujun 20220426 NewAdd
var a1=a[1]<200?200:a[1];//Mod by Shenjiujun 20220426 NewAdd
return [a0 + "px", a1 + "px"];
}
//return [a[0] + "px", a[1] + "px"];//Moded
//<<Mod by Shenjiujun 20220507 Moded
}
(),
title: !1,
shade: .9,
shadeClose: !0,
closeBtn: !1,
move: ".layui-layer-phimg img",
moveType: 1,
scrollbar: !1,
moveOut: !0,
anim: 5 * Math.random() | 0,
skin: "layui-layer-photos" + c("photos"),
content: lcontent,
success: function (e, i) {
//>>Add by Shenjiujun 20220507 NewAdd
if(ispdf)
{
var pdf = PDFObject.embed(
d[u].src,
"#showpdf"
);
}
//<<Add by Shenjiujun 20220507 NewAdd
l.bigimg = e.find(".layui-layer-phimg"),
l.imgsee = e.find(".layui-layer-imguide,.layui-layer-imgbar"),
l.event(e),
t.tab && t.tab(d[u], e)
},
end: function () {
l.end = !0,
i(document).off("keyup", l.keyup)
}
}, t))
}, function () {
r.close(l.loadi),
r.msg("当前图片地址异常<br>是否继续查看下一张?", {
time: 3e4,
btn: ["下一张", "不看了"],
yes: function () {
d.length > 1 && l.imgnext(!0, !0)
}
})
})
}
},
以上用的是固定宽高,如果有其他显示比例需求,请自行修改