PhotoSwipe
PhotoSwipe copied to clipboard
Can we unset the data-size and get the height and width of image automatically?
for (var i = 0; i < numNodes; i++) {
figureEl = thumbElements[i];
if (figureEl.nodeType !== 1) {
continue;
}
linkEl = figureEl.children[0];
---
var img = new Image();
img.src = linkEl.getAttribute('href');
linkEl.setAttribute('data-size', img.naturalWidth + 'x' + img.naturalHeight);
//i try to get the width and height when photoSwipe the problem is the first time i click the pic and its width and height are both '0' due to the loading problem..
---
size = linkEl.getAttribute("data-size").split("x");
console.log(size)
item = {
src: linkEl.getAttribute("href"),
w: parseInt(size[0], 10),
h: parseInt(size[1], 10)
};
+1 👍
Try this way instead:
---
linkEl = figureEl.children[0]; // <a> element
let img = linkEl.querySelector("img");
linkEl.setAttribute('data-size', img.naturalWidth + 'x' + img.naturalHeight);
// create slide object
item = {
src: linkEl.getAttribute('href'),
w: parseInt(img.naturalWidth, 10),
h: parseInt(img.naturalHeight, 10)
};
---