var initphotoswipefromdom = function (galleryselector) { var parsethumbnailelements = function (el) { var thumbelements = el.childnodes, numnodes = thumbelements.length, items = [], figureel, linkel, size, item; var imgw, imgh; for (var i = 0; i < numnodes; i++) { if (window.cp.shouldstopexecution(1)) { break; } figureel = thumbelements[i]; if (figureel.nodetype !== 1) { continue; } linkel = figureel.children[0]; //size = linkel.getattribute('data-size').split('x'); getimagewidth(linkel.getattribute('href'), function (w, h) { imgw = w; imgh = h; }); item = { src: linkel.getattribute('href'), w: parseint(imgw, 10), h: parseint(imgh, 10) }; if (figureel.children.length > 1) { item.title = figureel.children[1].innerhtml; } if (linkel.children.length > 0) { item.msrc = linkel.children[0].getattribute('src'); } item.el = figureel; items.push(item); } window.cp.exitedloop(1); return items; }; var closest = function closest(el, fn) { return el && (fn(el) ? el : closest(el.parentnode, fn)); }; var onthumbnailsclick = function (e) { e = e || window.event; e.preventdefault ? e.preventdefault() : e.returnvalue = false; var etarget = e.target || e.srcelement; var clickedlistitem = closest(etarget, function (el) { return (el.tagname && el.tagname.touppercase() === 'div'); }); if (!clickedlistitem) { return; } var clickedgallery = clickedlistitem.parentnode, childnodes = clickedlistitem.parentnode.childnodes, numchildnodes = childnodes.length, nodeindex = 0, index; for (var i = 0; i < numchildnodes; i++) { if (window.cp.shouldstopexecution(2)) { break; } if (childnodes[i].nodetype !== 1) { continue; } if (childnodes[i] === clickedlistitem) { index = nodeindex; break; } nodeindex++; } window.cp.exitedloop(2); if (index >= 0) { openphotoswipe(index, clickedgallery); } return false; }; var photoswipeparsehash = function () { var hash = window.location.hash.substring(1) , params = {}; if (hash.length < 5) { return params; } var vars = hash.split('&'); for (var i = 0; i < vars.length; i++) { if (window.cp.shouldstopexecution(3)) { break; } if (!vars[i]) { continue; } var pair = vars[i].split('='); if (pair.length < 2) { continue; } params[pair[0]] = pair[1]; } window.cp.exitedloop(3); if (params.gid) { params.gid = parseint(params.gid, 10); } return params; }; var openphotoswipe = function (index, galleryelement, disableanimation, fromurl) { var pswpelement = document.queryselectorall('.pswp')[0], gallery, options, items; items = parsethumbnailelements(galleryelement); options = { galleryuid: galleryelement.getattribute('data-pswp-uid'), getthumbboundsfn: function (index) { var thumbnail = items[index].el.getelementsbytagname('img')[0] pageyscroll = window.pageyoffset || document.documentelement.scrolltop , rect = thumbnail.getboundingclientrect(); return { x: rect.left, y: rect.top + pageyscroll, w: rect.width }; } }; if (fromurl) { if (options.gallerypids) { for (var j = 0; j < items.length; j++) { if (window.cp.shouldstopexecution(4)) { break; } if (items[j].pid == index) { options.index = j; break; } } window.cp.exitedloop(4); } else { options.index = parseint(index, 10) - 1; } } else { options.index = parseint(index, 10); } if (isnan(options.index)) { return; } if (disableanimation) { options.showanimationduration = 0; } gallery = new photoswipe(pswpelement, photoswipeui_default, items, options); gallery.init(); }; var galleryelements = document.queryselectorall(galleryselector); for (var i = 0, l = galleryelements.length; i < l; i++) { if (window.cp.shouldstopexecution(5)) { break; } galleryelements[i].setattribute('data-pswp-uid', i + 1); galleryelements[i].onclick = onthumbnailsclick; } window.cp.exitedloop(5); var hashdata = photoswipeparsehash(); if (hashdata.pid && hashdata.gid) { openphotoswipe(hashdata.pid, galleryelements[hashdata.gid - 1], true, true); } }; initphotoswipefromdom('.my-gallery'); function getimagewidth(url, callback) { var img = new image(); img.src = url; // ���ͼƭ�����棬��ֱ�ӷ��ػ������� if (img.complete) { callback(img.width, img.height); } else { // ��ȫ������ϵ��¼� img.onload = function () { callback(img.width, img.height); } } }