mobile images

This commit is contained in:
mi
2025-11-12 14:15:47 +10:00
parent 83ea55adc3
commit ddf20d0f7f
5 changed files with 71 additions and 11 deletions

View File

@@ -23,6 +23,7 @@ COMICS = [
'number': 1, 'number': 1,
'title': 'First Comic', 'title': 'First Comic',
'filename': 'comic-001.jpg', 'filename': 'comic-001.jpg',
'mobile_filename': 'comic-001-mobile.jpg', # Optional: mobile version of the comic
'date': '2025-01-01', 'date': '2025-01-01',
'alt_text': 'The very first comic', 'alt_text': 'The very first comic',
'author_note': 'This is where your comic journey begins!', 'author_note': 'This is where your comic journey begins!',

Binary file not shown.

View File

@@ -67,10 +67,7 @@
// Update image and its link // Update image and its link
const comicImageDiv = document.querySelector('.comic-image'); const comicImageDiv = document.querySelector('.comic-image');
const img = comicImageDiv.querySelector('img'); updateComicImage(comicImageDiv, comic, title);
img.src = `/static/images/comics/${comic.filename}`;
img.alt = title;
img.title = comic.alt_text;
// Update or create/remove the link wrapper // Update or create/remove the link wrapper
updateComicImageLink(comic.number); updateComicImageLink(comic.number);
@@ -101,15 +98,47 @@
history.pushState({ comicId: comic.number }, '', `/comic/${comic.number}`); 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 // Update comic image link for click navigation
function updateComicImageLink(currentNumber) { function updateComicImageLink(currentNumber) {
const comicImageDiv = document.querySelector('.comic-image'); const comicImageDiv = document.querySelector('.comic-image');
const img = comicImageDiv.querySelector('img'); const imgOrPicture = comicImageDiv.querySelector('picture') || comicImageDiv.querySelector('img');
// Remove existing link if present // Remove existing link if present
const existingLink = comicImageDiv.querySelector('a'); const existingLink = comicImageDiv.querySelector('a');
if (existingLink) { if (existingLink) {
existingLink.replaceWith(img); existingLink.replaceWith(imgOrPicture);
} }
// Add link if there's a next comic // Add link if there's a next comic
@@ -120,8 +149,8 @@
e.preventDefault(); e.preventDefault();
loadComic(currentNumber + 1); loadComic(currentNumber + 1);
}; };
img.parentNode.insertBefore(link, img); imgOrPicture.parentNode.insertBefore(link, imgOrPicture);
link.appendChild(img); link.appendChild(imgOrPicture);
} }
} }

View File

@@ -16,14 +16,32 @@
<div class="comic-image"> <div class="comic-image">
{% if comic.number < total_comics %} {% if comic.number < total_comics %}
<a href="{{ url_for('comic', comic_id=comic.number + 1) }}"> <a href="{{ url_for('comic', comic_id=comic.number + 1) }}">
{% if comic.mobile_filename %}
<picture>
<source media="(max-width: 768px)" srcset="{{ url_for('static', filename='images/comics/' + comic.mobile_filename) }}">
<img src="{{ url_for('static', filename='images/comics/' + comic.filename) }}"
alt="{{ comic.title if comic.title else '#' ~ comic.number }}"
title="{{ comic.alt_text }}">
</picture>
{% else %}
<img src="{{ url_for('static', filename='images/comics/' + comic.filename) }}" <img src="{{ url_for('static', filename='images/comics/' + comic.filename) }}"
alt="{{ comic.title if comic.title else '#' ~ comic.number }}" alt="{{ comic.title if comic.title else '#' ~ comic.number }}"
title="{{ comic.alt_text }}"> title="{{ comic.alt_text }}">
{% endif %}
</a> </a>
{% else %} {% else %}
<img src="{{ url_for('static', filename='images/comics/' + comic.filename) }}" {% if comic.mobile_filename %}
alt="{{ comic.title if comic.title else '#' ~ comic.number }}" <picture>
title="{{ comic.alt_text }}"> <source media="(max-width: 768px)" srcset="{{ url_for('static', filename='images/comics/' + comic.mobile_filename) }}">
<img src="{{ url_for('static', filename='images/comics/' + comic.filename) }}"
alt="{{ comic.title if comic.title else '#' ~ comic.number }}"
title="{{ comic.alt_text }}">
</picture>
{% else %}
<img src="{{ url_for('static', filename='images/comics/' + comic.filename) }}"
alt="{{ comic.title if comic.title else '#' ~ comic.number }}"
title="{{ comic.alt_text }}">
{% endif %}
{% endif %} {% endif %}
</div> </div>

View File

@@ -14,9 +14,18 @@
</div> </div>
<div class="comic-image"> <div class="comic-image">
{% if comic.mobile_filename %}
<picture>
<source media="(max-width: 768px)" srcset="{{ url_for('static', filename='images/comics/' + comic.mobile_filename) }}">
<img src="{{ url_for('static', filename='images/comics/' + comic.filename) }}"
alt="{{ comic.title if comic.title else '#' ~ comic.number }}"
title="{{ comic.alt_text }}">
</picture>
{% else %}
<img src="{{ url_for('static', filename='images/comics/' + comic.filename) }}" <img src="{{ url_for('static', filename='images/comics/' + comic.filename) }}"
alt="{{ comic.title if comic.title else '#' ~ comic.number }}" alt="{{ comic.title if comic.title else '#' ~ comic.number }}"
title="{{ comic.alt_text }}"> title="{{ comic.alt_text }}">
{% endif %}
</div> </div>
<div class="comic-navigation"> <div class="comic-navigation">