A pure JS question.
When the page loads, small thumbnail images are loaded (webp or jpg). When one clicks on an image (picture element) - two things happen: picture element changes class to large (and css resizes it) and srcset string of the small file is edited to point to a larger file ('-large.webp' is the end of string). And once the image is clicked on again the srcset string should be again edited to point to the small file ('.webp' should be the end of string). However this last thing does not happen, rather the srcset string get another '-large.webp' now pointing to a non existing file.
A page has a lots of images and I need to load big file only on click. Lazy loading doesn't help because scrolling is not the event I want to trigger them.
I suppose my logic in the if else loop is faulty ... but it may be something else?
Any help appreciated.
function toggleImages() {
var images = document.querySelectorAll('figure picture');
for(var i =0; i< images.length; i++){
images[i].onclick = function() {
// resize the container
this.classList.toggle('thumb');
this.classList.toggle('large');
// img and source DOM elements
var imgtag = this.querySelector('img');
var sourcetag = this.querySelector('source');
// get urls of small image files
var thumbsourceJpg = imgtag.getAttribute('src');
var thumbsourceWebp = sourcetag.getAttribute('srcset');
// strip the last 4 chars (extension and dot)
var noJpg = thumbsourceJpg.slice(0,-4);
var noWebp = thumbsourceWebp.slice(0,-5);
// change ending of urls to point to larger image
var largesourceJpg = noJpg + '-large.jpg';
var largesourceWebp = noWebp + '-large.webp';
// if srcset points to small file
if (sourcetag.srcset == thumbsourceWebp) {
// make it point to a large one
sourcetag.srcset = largesourceWebp;
} else {
// otherwise point to the small file
sourcetag.srcset = thumbsourceWebp;
}
// do the same for jpgs (this doesnt work at all)
if (imgtag.src == thumbsourceJpg) {
imgtag.src = largesourceJpg;
} else {
imgtag.src = thumbsourceJpg;
}
}
}
}
window.onload = function() {
toggleImages();
}
figure picture {background: gray;}
figure picture.large{
width: 608px;
-moz-transition: all .5s ease;
-webkit-transition: all .5s ease;
-o-transition: all .5s ease;
transition: all .5s ease;
}
figure picture.thumb {
width: 180px;
cursor:pointer;
}
<!DOCTYPE html>
<html>
<body>
<figure>
<picture class="thumb">
<source type="image/webp" srcset="https://fokusgrupa.net/files/file1.webp">
<img src="https://fokusgrupa.net/files/file1.jpg" alt="description">
</picture>
<picture class="thumb">
<source type="image/webp" srcset="https://fokusgrupa.net/files/file2.webp">
<img src="https://fokusgrupa.net/files/file2.jpg" alt="description">
</picture>
</figure>
</body>