Jump to content

Coding Modal Boxes with Clickable Tiles

Recommended Posts

Hello,

I'm currently working a page of my site that features tiles of pervious cover art I've made and I want them to eventually each open a modal box when clicked to open a 'more info' piece that explains more about the respective art and artist, and then be closed by clicking an 'x' or outside of the modal itself.

I've managed to get to a certain point with ChatGPT but once I began attempting to integrate the code given to me, I am running into issues with the modal box activation even working. I have other css and html that ties into the specific tiles on screen that trigger hover animations.

I've placed the javascript in a separate code block (per the AI's direction) but am met with a "Script Disabled" in edit mode and then no activation on a separate tab after logging out on a live site.

URL: https://coloresart.com/cover-art-1

Here's the full code (including hover animations and id's):

HTML:

<!-- Tile -->
<div id="tile-1" class="tile">
    <img src="https://images.squarespace-cdn.com/content/v1/5e9aae3d05a18504e8481a60/d6693ed6-eec5-4fcc-9342-2e27051d14b0/_MG_8381_1.jpg?format=2500w" alt="Image description">
    <div class="tile-text">PILOT</div>
</div>

<!-- Modal -->
<div id="modal-1" class="modal">
    <div class="modal-content">
        <span class="close" data-target="modal-1">&times;</span>
        <h2>PILOT</h2>
        <p>More information about the PILOT project...</p>
    </div>
</div>

CSS:

/* Tile styling */
.tile {
    position: relative;
    display: inline-block;
    overflow: hidden;
    cursor: pointer;
    margin: 10px; /* Optional: Add some margin between tiles */
    text-align: center; /* Ensure text is centered horizontally */
}

.tile img {
    display: block;
    width: 100%;
    transition: transform 0.3s ease, filter 0.3s ease;
}

.tile .tile-text {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: white;
    opacity: 0;
    transition: opacity 0.3s ease;
    width: 100%; /* Ensure text block takes full width */
    text-align: center; /* Center the text horizontally */
}

.tile:hover img {
    transform: scale(0.95);
    filter: blur(2px) brightness(80%);
}

.tile:hover .tile-text {
    opacity: 1;
}

/* Modal styling */
.modal {
    display: none; /* Hidden by default */
    position: fixed;
    z-index: 1;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

.modal-content {
    background-color: #fefefe;
    margin: 15% auto; /* 15% from the top and centered */
    padding: 20px;
    border: 1px solid #888;
    width: 80%; /* Could be more or less, depending on screen size */
}

.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}

Javascript:

<script>
document.addEventListener('DOMContentLoaded', function () {
    // Get the tile
    const tile = document.getElementById('tile-1');
    // Get the modal
    const modal = document.getElementById('modal-1');
    // Get the close button inside the modal
    const closeButton = modal.querySelector('.close');

    // When the user clicks on the tile, open the modal
    if (tile) {
        tile.addEventListener('click', function() {
            modal.style.display = 'block';
        });
    }

    // When the user clicks on <span> (x), close the modal
    if (closeButton) {
        closeButton.addEventListener('click', function() {
            modal.style.display = 'none';
        });
    }

    // When the user clicks anywhere outside of the modal, close it
    window.addEventListener('click', function(event) {
        if (event.target === modal) {
            modal.style.display = 'none';
        }
    });
});
</script>


I hope there is an easy fix! I hope to implement some additional features to the respective modals that I may also need help with but I'll cross that bridge when I get there.

Link to comment
  • Replies 1
  • Views 203
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

On 7/6/2024 at 10:24 AM, ericganci10 said:

Hello,

I'm currently working a page of my site that features tiles of pervious cover art I've made and I want them to eventually each open a modal box when clicked to open a 'more info' piece that explains more about the respective art and artist, and then be closed by clicking an 'x' or outside of the modal itself.

I've managed to get to a certain point with ChatGPT but once I began attempting to integrate the code given to me, I am running into issues with the modal box activation even working. I have other css and html that ties into the specific tiles on screen that trigger hover animations.

I've placed the javascript in a separate code block (per the AI's direction) but am met with a "Script Disabled" in edit mode and then no activation on a separate tab after logging out on a live site.

URL: https://coloresart.com/cover-art-1

Here's the full code (including hover animations and id's):

HTML:

<!-- Tile -->
<div id="tile-1" class="tile">
    <img src="https://images.squarespace-cdn.com/content/v1/5e9aae3d05a18504e8481a60/d6693ed6-eec5-4fcc-9342-2e27051d14b0/_MG_8381_1.jpg?format=2500w" alt="Image description">
    <div class="tile-text">PILOT</div>
</div>

<!-- Modal -->
<div id="modal-1" class="modal">
    <div class="modal-content">
        <span class="close" data-target="modal-1">&times;</span>
        <h2>PILOT</h2>
        <p>More information about the PILOT project...</p>
    </div>
</div>

CSS:

/* Tile styling */
.tile {
    position: relative;
    display: inline-block;
    overflow: hidden;
    cursor: pointer;
    margin: 10px; /* Optional: Add some margin between tiles */
    text-align: center; /* Ensure text is centered horizontally */
}

.tile img {
    display: block;
    width: 100%;
    transition: transform 0.3s ease, filter 0.3s ease;
}

.tile .tile-text {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: white;
    opacity: 0;
    transition: opacity 0.3s ease;
    width: 100%; /* Ensure text block takes full width */
    text-align: center; /* Center the text horizontally */
}

.tile:hover img {
    transform: scale(0.95);
    filter: blur(2px) brightness(80%);
}

.tile:hover .tile-text {
    opacity: 1;
}

/* Modal styling */
.modal {
    display: none; /* Hidden by default */
    position: fixed;
    z-index: 1;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

.modal-content {
    background-color: #fefefe;
    margin: 15% auto; /* 15% from the top and centered */
    padding: 20px;
    border: 1px solid #888;
    width: 80%; /* Could be more or less, depending on screen size */
}

.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}

Javascript:

<script>
document.addEventListener('DOMContentLoaded', function () {
    // Get the tile
    const tile = document.getElementById('tile-1');
    // Get the modal
    const modal = document.getElementById('modal-1');
    // Get the close button inside the modal
    const closeButton = modal.querySelector('.close');

    // When the user clicks on the tile, open the modal
    if (tile) {
        tile.addEventListener('click', function() {
            modal.style.display = 'block';
        });
    }

    // When the user clicks on <span> (x), close the modal
    if (closeButton) {
        closeButton.addEventListener('click', function() {
            modal.style.display = 'none';
        });
    }

    // When the user clicks anywhere outside of the modal, close it
    window.addEventListener('click', function(event) {
        if (event.target === modal) {
            modal.style.display = 'none';
        }
    });
});
</script>


I hope there is an easy fix! I hope to implement some additional features to the respective modals that I may also need help with but I'll cross that bridge when I get there.

A plugin you can get reference:

https://www.sqspthemes.com/plugins/p/ultimate-squarespace-lightbox-plugin

Press 👍  or mark my comment as solution if you find my sharing useful

🆒 Squarespace pinchzoom lightbox plugin (affiliate link)

👁‍🗨 360 degree photo viewer (affiliate link)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.