I actually just made this kinda viewer for my new gallery maker! You're welcome to use the code from it ^^

Its very simple, when you click an image, it will stop the new tab from opening, then display the image on top of your gallery with next and back buttons - if you click outside the image or press esc, it will close the viewer!
(It depends on every image being inside an <a> with a href link to the image and all the <a>s being next to each other; but that seems to be what you have!)
The only change you need to make is add id="photos" to your image-gallery section, add the CSS, then link this script at the bottom of your page
CSS:
#js-viewer {
position: fixed;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.612);
display: flex;
justify-content: center;
}
#js-viewer div {
position: absolute;
top: 50%;
transform: translateY(-50%);
text-align: center;
}
#js-viewer div img {
width: 95%;
}
Script:
// +++ Melon's Micro Gallery Viewer v0.1 +++
let al = {};
// Inject viewer code to the end of the body!
document.body.insertAdjacentHTML("beforeend", '<div id="js-viewer"><div><img id="js-viewer-img" src="" /><span><button id="js-viewer-prev" type="button">Previous</button> <button id="js-viewer-next" type="button">Next</button></span></div></div>');
al.html = {};
al.html.viewer = document.getElementById("js-viewer");
al.html.viewer.style.display = "none";
al.html.viewerImg = document.getElementById("js-viewer-img");
al.html.viewerPrev = document.getElementById("js-viewer-prev");
al.html.viewerNext = document.getElementById("js-viewer-next");
al.selectedPhoto = undefined;
document.addEventListener("click", (e) => {
// Photo Viewing Event!
if (e.target.parentElement.tagName == "A" && e.target.parentElement.parentElement.id == "photos") {
e.preventDefault();
viewPhoto(e.target.parentElement);
}
// Viewer Hide!
if (e.target.id == "js-viewer") {
al.html.viewer.style.display = "none";
}
// Prev Button Event
if (e.target.id == "js-viewer-prev") {
viewPhoto(al.selectedPhoto.previousElementSibling);
}
// Next Button Event
if (e.target.id == "js-viewer-next") {
viewPhoto(al.selectedPhoto.nextElementSibling);
}
});
document.addEventListener("keyup", (e) => {
// Close the viewer is Esc is pressed!
if (e.key === "Escape") {
al.html.viewer.style.display = "none";
}
});
// Open the viewer and display a photo!
function viewPhoto(photo) {
al.selectedPhoto = photo;
al.html.viewerImg.src = al.selectedPhoto.href;
al.html.viewer.style.display = "";
al.html.viewerPrev.style.display = "";
al.html.viewerNext.style.display = "";
if (al.selectedPhoto.previousElementSibling == undefined) al.html.viewerPrev.style.display = "none";
if (al.selectedPhoto.nextElementSibling == undefined) al.html.viewerNext.style.display = "none";
}