diff --git a/comics_data.py b/comics_data.py index 6ccb757..ee0c647 100644 --- a/comics_data.py +++ b/comics_data.py @@ -23,6 +23,7 @@ COMICS = [ 'number': 1, 'title': 'First Comic', 'filename': 'comic-001.jpg', + 'mobile_filename': 'comic-001-mobile.jpg', # Optional: mobile version of the comic 'date': '2025-01-01', 'alt_text': 'The very first comic', 'author_note': 'This is where your comic journey begins!', diff --git a/static/images/comics/comic-001-mobile.jpg b/static/images/comics/comic-001-mobile.jpg new file mode 100644 index 0000000..cf39d24 --- /dev/null +++ b/static/images/comics/comic-001-mobile.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:678d1af2c2ffb99ed8c9e641d38463f2e4a27118a5d316aaee4431f8815cd671 +size 62677 diff --git a/static/js/comic-nav.js b/static/js/comic-nav.js index 4802dec..322b5f6 100644 --- a/static/js/comic-nav.js +++ b/static/js/comic-nav.js @@ -67,10 +67,7 @@ // Update image and its link const comicImageDiv = document.querySelector('.comic-image'); - const img = comicImageDiv.querySelector('img'); - img.src = `/static/images/comics/${comic.filename}`; - img.alt = title; - img.title = comic.alt_text; + updateComicImage(comicImageDiv, comic, title); // Update or create/remove the link wrapper updateComicImageLink(comic.number); @@ -101,15 +98,47 @@ history.pushState({ comicId: comic.number }, '', `/comic/${comic.number}`); } + // Update or create comic image with optional mobile version + function updateComicImage(comicImageDiv, comic, title) { + // Clear all existing content + comicImageDiv.innerHTML = ''; + + // Create new image element(s) + if (comic.mobile_filename) { + // Create picture element with mobile source + const picture = document.createElement('picture'); + + const source = document.createElement('source'); + source.media = '(max-width: 768px)'; + source.srcset = `/static/images/comics/${comic.mobile_filename}`; + + const img = document.createElement('img'); + img.src = `/static/images/comics/${comic.filename}`; + img.alt = title; + img.title = comic.alt_text; + + picture.appendChild(source); + picture.appendChild(img); + comicImageDiv.appendChild(picture); + } else { + // Create regular img element + const img = document.createElement('img'); + img.src = `/static/images/comics/${comic.filename}`; + img.alt = title; + img.title = comic.alt_text; + comicImageDiv.appendChild(img); + } + } + // Update comic image link for click navigation function updateComicImageLink(currentNumber) { const comicImageDiv = document.querySelector('.comic-image'); - const img = comicImageDiv.querySelector('img'); + const imgOrPicture = comicImageDiv.querySelector('picture') || comicImageDiv.querySelector('img'); // Remove existing link if present const existingLink = comicImageDiv.querySelector('a'); if (existingLink) { - existingLink.replaceWith(img); + existingLink.replaceWith(imgOrPicture); } // Add link if there's a next comic @@ -120,8 +149,8 @@ e.preventDefault(); loadComic(currentNumber + 1); }; - img.parentNode.insertBefore(link, img); - link.appendChild(img); + imgOrPicture.parentNode.insertBefore(link, imgOrPicture); + link.appendChild(imgOrPicture); } } diff --git a/templates/comic.html b/templates/comic.html index 48930b7..e14d17a 100644 --- a/templates/comic.html +++ b/templates/comic.html @@ -16,14 +16,32 @@