I’ve searched around the internet and most lighthouse image modals or pop out image galleries use JavaScript to go up and down by 1 to a different image source (ex: img1.jpg-> img2.jpg -> img3.jpg).
I’m building an image gallery module in Hubspot using Hubl with repeating fields so these solutions won’t work.
What JavaScript could I use when the source of the image will use non linear names (in other words- each image source could vary from www.name/flower.jpg to. www.name/pizza.jpg). Would a solution be adding numbers to the div containing the repeating image source?
Here is one possible image gallery solution using Hubl repeater image fields for the image source:
const images = [...document.querySelectorAll('.gallery-image img')];
// popup
const popup = document.querySelector('.popup');
const closeBtn = document.querySelector('.close-btn');
const imageName = document.querySelector('.image-name');
const largeImage = document.querySelector('.large-image img');
const imageIndex = document.querySelector('.index');
const leftArrow = document.querySelector('.left-arrow');
const rightArrow = document.querySelector('.right-arrow');
let index = 0; // will track our current image;
images.forEach((item, i) => {
item.addEventListener('click', () => {
updateImage(i);
popup.classList.toggle('active');
})
})
const updateImage = (i) => {
let path = `img/img${i+1}.png`;
largeImage.src = path;
imageName.innerHTML = path;
imageIndex.innerHTML = `0${i+1}`;
index = i;
}
closeBtn.addEventListener('click', () => {
popup.classList.toggle('active');
})
leftArrow.addEventListener('click', () => {
if(index > 0){
updateImage(index - 1);
}
})
rightArrow.addEventListener('click', () => {
if(index < images.length - 1){
updateImage(index + 1);
}
})
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
*:focus{
outline: none;
}
body{
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background: #ff7a2d;
font-family: 'roboto', sans-serif;
}
.gallery{
width: 80%;
height: 90vh;
max-width: 1600px;
max-height: 800px;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
.gallery-image{
width: 30%;
height: calc(50% - 20px);
min-width: 300px;
min-height: 200px;
margin: 10px;
overflow: hidden;
}
.image{
width: 100%;
height: 100%;
object-fit: cover;
transition: 1s;
}
/* popup */
.popup{
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(0);
width: 80%;
max-width: 1600px;
height: 90vh;
max-height: 800px;
border-radius: 20px;
background: rgba(0, 0, 0, 0.75);
display: flex;
justify-content: center;
align-items: center;
z-index: 5;
overflow: hidden;
transition: 1s;
opacity: 0;
}
.popup.active{
transform: translate(-50%, -50%) scale(1);
opacity: 1;
}
.popup.active .close-btn,
.popup.active .image-name,
.popup.active .index,
.popup.active .large-image,
.popup.active .arrow-btn{
opacity: 1;
transition: opacity .5s;
transition-delay: 1s;
}
.top-bar{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 50px;
background: #000;
color: #fff;
text-align: center;
line-height: 50px;
font-weight: 300;
}
.image-name{
opacity: 0;
}
.close-btn{
opacity: 0;
position: absolute;
top: 15px;
right: 20px;
width: 20px;
height: 20px;
border-radius: 50%;
background: #f00;
cursor: pointer;
}
.arrow-btn{
opacity: 0;
position: absolute;
top: 50%;
transform: translateY(-50%);
padding: 10px;
border-radius: 50%;
border: none;
background: none;
cursor: pointer;
}
.left-arrow{
left: 10px;
}
.right-arrow{
right: 10px;
transform: translateY(-50%) rotate(180deg);
}
.arrow-btn:hover{
background: rgba(0, 0, 0, 0.5);
}
.index{
position: absolute;
bottom: 10px;
right: 10px;
font-size: 80px;
font-weight: 100;
color: rgba(255, 255, 255, 0.4);
opacity: 0;
}
.large-image{
margin-top: 5%;
width: 80%;
height: 80%;
object-fit: contain;
opacity: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Popup</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- popup -->
<div class="popup">
<!-- top bar -->
<div class="top-bar">
<p class="image-name">Image Name</p>
<span class="close-btn"></span>
</div>
<!-- arrows -->
<button class="arrow-btn left-arrow"><img src="img/arrow.png" alt=""></button>
<button class="arrow-btn right-arrow"><img src="img/arrow.png" alt=""></button>
<!-- image -->
{% for item in module.image_1 %}
<div class="large-image">
{% if item.src %}
{% set sizeAttrs = 'width="{{ item.width }}" height="{{ item.height }}"' %}
{% if item.size_type == 'auto' %}
{% set sizeAttrs = 'width="{{ item.width }}" height="{{ item.height }}" style="max-width: 100%; height: auto;"' %}
{% elif item.size_type == 'auto_custom_max' %}
{% set sizeAttrs = 'width="{{ item.max_width }}" height="{{ item.max_height }}" style="max-width: 100%; height: auto;"' %}
{% endif %}
{% set loadingAttr = item.loading != 'disabled' ? 'loading="{{ item.loading }}"' : '' %}
<img src="{{ item.src }}" alt="{{ item.alt }}" {{ loadingAttr }} {{ sizeAttrs }}>
{% endif %}
</div>
{% endfor %}
<!-- image-index -->
<h1 class="index">01</h1>
</div>
<div class="gallery">
{% for item in module.image_1 %}
<div class="gallery-image">
{% if item.src %}
{% set sizeAttrs = 'width="{{ item.width }}" height="{{ item.height }}"' %}
{% if item.size_type == 'auto' %}
{% set sizeAttrs = 'width="{{ item.width }}" height="{{ item.height }}" style="max-width: 100%; height: auto;"' %}
{% elif item.size_type == 'auto_custom_max' %}
{% set sizeAttrs = 'width="{{ item.max_width }}" height="{{ item.max_height }}" style="max-width: 100%; height: auto;"' %}
{% endif %}
{% set loadingAttr = item.loading != 'disabled' ? 'loading="{{ item.loading }}"' : '' %}
<img src="{{ item.src }}" alt="{{ item.alt }}" {{ loadingAttr }} {{ sizeAttrs }}>
{% endif %}
</div>
{% endfor %}
</div>
<script src="app.js"></script>
</body>
</html>
The issue is I need the pop up image to only pop up to the selected image, not every image in the repeater field. I also need a way to specify what image is being navigated to using arrow icons when the image is popped out.
I understand that this JS is based around the assumption that images will be named in sequential order from 1 and on (but it won’t work for a repeater field):
const updateImage = (i) => {
let path = `img/img${i+1}.png`;
largeImage.src = path;
imageName.innerHTML = path;
imageIndex.innerHTML = `0${i+1}`;
index = i;
}
Any help appreciated.