PhotoSwipe icon indicating copy to clipboard operation
PhotoSwipe copied to clipboard

Can we unset the data-size and get the height and width of image automatically?

Open bennyli519 opened this issue 6 years ago • 2 comments

  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)
          };

bennyli519 avatar May 25 '18 04:05 bennyli519

+1 👍

notrealdev avatar Aug 23 '18 08:08 notrealdev

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)
	};
---

developman-akl avatar Nov 11 '20 02:11 developman-akl