1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| var imgDataPath = "/blog/photos/photos.json"; var imgPath = "https://cdn.jsdelivr.net/gh/Cenergy/images/gallery/"; var imgMaxNum = 50;
var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; if (windowWidth < 768) { var imageWidth = 145; } else { var imageWidth = 250; }
const photo = { page: 1, offset: imgMaxNum, init: function () { var that = this; $.getJSON(imgDataPath, function (data) { that.render(that.page, data); that.eventListen(data); }); }, constructHtml(options) { const { imageWidth, imageX, imageY, name, imgPath, imgName, imgNameWithPattern, } = options; const htmlEle = `<div class="card lozad" style="width:${imageWidth}px"> <div class="ImageInCard" style="height:${ (imageWidth * imageY) / imageX }px"> <a data-fancybox="gallery" href="${imgPath}${name}/${imgNameWithPattern}" data-caption="${imgName}" title="${imgName}"> <img class="lazyload" data-src="${imgPath}${name}/${imgNameWithPattern}" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" onload="lzld(this)" lazyload="auto"> </a> </div> </div>`; return htmlEle; }, render: function (page, data = []) { this.data = data; if (!data.length) return; var html, imgNameWithPattern, imgName, imageSize, imageX, imageY, li = "";
let liHtml = ""; let contentHtml = "";
data.forEach((item, index) => { const activeClass = index === 0 ? "active" : ""; liHtml += `<li class="nav-item" role="presentation"> <a class="nav-link ${activeClass} photo-tab" id="home-tab" photo-uuid="${item.name}" data-toggle="tab" href="#${item.name}" role="tab" aria-controls="${item.name}" aria-selected="true">${item.name}</a> </li>`; }); const [initData = {}] = data; const { children = [],name } = initData; children.forEach((item, index) => { imgNameWithPattern = item.split(" ")[1]; imgName = imgNameWithPattern.split(".")[0]; imageSize = item.split(" ")[0]; imageX = imageSize.split(".")[0]; imageY = imageSize.split(".")[1]; let imgOptions = { imageWidth, imageX, imageY, name, imgName, imgPath, imgNameWithPattern, }; li += this.constructHtml(imgOptions); }); contentHtml += ` <div class="tab-pane fade show active" role="tabpanel" aria-labelledby="home-tab">${li}</div>`;
const ulHtml = `<ul class="nav nav-tabs" id="myTab" role="tablist">${liHtml}</ul>`; const tabContent = `<div class="tab-content" id="myTabContent">${contentHtml}</div>`;
$("#imageTab").append(ulHtml); $(".ImageGrid").append(tabContent); this.minigrid(); }, eventListen: function (data) { let self = this; var html, imgNameWithPattern, imgName, imageSize, imageX, imageY, li = ""; $('a[data-toggle="tab"]').on("shown.bs.tab", function (e) { $(".ImageGrid").empty(); const selectId = $(e.target).attr("photo-uuid"); const selectedData = data.find((data) => data.name === selectId) || {}; const { children,name } = selectedData; let li = ""; children.forEach((item, index) => { imgNameWithPattern = item.split(" ")[1]; imgName = imgNameWithPattern.split(".")[0]; imageSize = item.split(" ")[0]; imageX = imageSize.split(".")[0]; imageY = imageSize.split(".")[1]; let imgOptions = { imageWidth, imageX, imageY, name, imgName, imgPath, imgNameWithPattern, }; li += self.constructHtml(imgOptions); }); $(".ImageGrid").append(li); self.minigrid(); }); }, minigrid: function () { var grid = new Minigrid({ container: ".ImageGrid", item: ".card", gutter: 12, }); grid.mount(); $(window).resize(function () { grid.mount(); }); }, }; photo.init();
|