Use navigable link elements.
This commit is contained in:
parent
5774ef7779
commit
b57ca2c47a
@ -25,6 +25,7 @@
|
|||||||
padding: 20px;
|
padding: 20px;
|
||||||
user-select: none;
|
user-select: none;
|
||||||
transition: color 0.2s;
|
transition: color 0.2s;
|
||||||
|
text-decoration: none;
|
||||||
}
|
}
|
||||||
.chevron:hover { color: rgba(255, 255, 255, 0.9); }
|
.chevron:hover { color: rgba(255, 255, 255, 0.9); }
|
||||||
.chevron.left { left: 10px; }
|
.chevron.left { left: 10px; }
|
||||||
@ -33,23 +34,30 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="container">
|
<div id="container">
|
||||||
<img id="img" alt="Random image">
|
<a href="#" id="img-link"><img id="img" alt="Random image"></a>
|
||||||
<div class="chevron left" id="prev-btn">‹</div>
|
<a href="#" class="chevron left" id="prev-btn">‹</a>
|
||||||
<div class="chevron right" id="next-btn">›</div>
|
<a href="#" class="chevron right" id="next-btn">›</a>
|
||||||
</div>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
let currentData = null;
|
let currentData = null;
|
||||||
|
|
||||||
function loadImageSrc(hash) {
|
function loadImageSrc(hash) {
|
||||||
document.getElementById('img').src = '/' + hash + '/data';
|
document.getElementById('img').src = '/' + hash + '/data';
|
||||||
|
document.getElementById('img-link').href = '#';
|
||||||
history.replaceState(null, '', '#' + hash);
|
history.replaceState(null, '', '#' + hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadInfo(hash) {
|
async function loadInfo(hash) {
|
||||||
const response = await fetch('/' + hash);
|
const response = await fetch('/' + hash);
|
||||||
|
if (!response.ok) {
|
||||||
|
loadRandom();
|
||||||
|
return;
|
||||||
|
}
|
||||||
currentData = await response.json();
|
currentData = await response.json();
|
||||||
loadImageSrc(currentData.img);
|
loadImageSrc(currentData.img);
|
||||||
document.getElementById('img').title = currentData.filename || '';
|
document.getElementById('img').title = currentData.filename || '';
|
||||||
|
document.getElementById('prev-btn').href = '#' + currentData.previous;
|
||||||
|
document.getElementById('next-btn').href = '#' + currentData.next;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadRandom() {
|
async function loadRandom() {
|
||||||
@ -58,24 +66,30 @@
|
|||||||
await loadInfo(data.img);
|
await loadInfo(data.img);
|
||||||
}
|
}
|
||||||
|
|
||||||
document.getElementById('img').addEventListener('click', loadRandom);
|
window.addEventListener('hashchange', function() {
|
||||||
document.getElementById('prev-btn').addEventListener('click', function() {
|
const hash = window.location.hash.slice(1);
|
||||||
if (currentData && currentData.previous) loadInfo(currentData.previous);
|
if (hash) {
|
||||||
|
loadInfo(hash);
|
||||||
|
} else {
|
||||||
|
loadRandom();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
document.getElementById('next-btn').addEventListener('click', function() {
|
|
||||||
if (currentData && currentData.next) loadInfo(currentData.next);
|
window.addEventListener('hashchange', function() {
|
||||||
|
const hash = window.location.hash.slice(1);
|
||||||
|
if (hash) loadInfo(hash);
|
||||||
});
|
});
|
||||||
|
|
||||||
document.addEventListener('keydown', function(e) {
|
document.addEventListener('keydown', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
if (e.code === 'Space') {
|
if (e.code === 'Space') {
|
||||||
e.preventDefault();
|
window.location.hash = '';
|
||||||
loadRandom();
|
} else if (e.code === 'ArrowLeft') {
|
||||||
} else if (e.code === 'ArrowLeft' && currentData && currentData.previous) {
|
document.getElementById('prev-btn').click();
|
||||||
e.preventDefault();
|
} else if (e.code === 'ArrowRight') {
|
||||||
loadInfo(currentData.previous);
|
document.getElementById('next-btn').click();
|
||||||
} else if (e.code === 'ArrowRight' && currentData && currentData.next) {
|
} else {
|
||||||
e.preventDefault();
|
return;
|
||||||
loadInfo(currentData.next);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user