To achieve your requirement of opening a card with more information, screenshots, and a "Go to Source" button when a project is clicked, we'll modify your existing code by:
- Creating a Modal: A popup window that appears over the current page content.
- Updating Project Items: Modify each project item to trigger the modal and pass necessary data.
- Adding JavaScript Functions: To handle opening, populating, and closing the modal.
- Styling the Modal: Add CSS to make the modal look good and function properly.
Below is the step-by-step guide to implement this functionality.
1. Add Modal Structure
First, add the following modal structure just before the closing </body> tag in your HTML file:
<!-- Modal Structure -->
<div id="projectModal" class="modal">
<div class="modal-content">
<span class="close-button" onclick="closeModal()">×</span>
<h1 id="modalTitle"></h1>
<div id="modalImages" class="image-gallery"></div>
<p id="modalDescription"></p>
<button id="goToSourceButton" onclick="">Go to Source</button>
</div>
</div>
2. Add Modal CSS Styles
Add the following CSS styles to your stylesheet to style the modal:
/* Modal styles */
.modal {
display: none;
position: fixed;
z-index: 1000;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto; /* Enable scroll if needed */
background-color: rgba(0,0,0,0.5); /* Black background with opacity */
}
.modal-content {
background-color: #fefefe;
margin: 5% auto; /* Centered */
padding: 20px;
border: 1px solid #888;
width: 80%; /* Adjust as needed */
max-width: 800px; /* Max width */
position: relative; /* For positioning close button */
}
.close-button {
color: #aaa;
position: absolute;
right: 20px;
top: 10px;
font-size: 30px;
font-weight: bold;
cursor: pointer;
}
.image-gallery {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-bottom: 20px;
}
.image-gallery img {
max-width: 100%;
height: auto;
object-fit: cover;
flex: 1 1 calc(33% - 10px); /* Adjust for number of images per row */
box-sizing: border-box;
}
#goToSourceButton {
background-color: #007BFF;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
}
#goToSourceButton:hover {
background-color: #0056b3;
}
3. Update Project Items
Modify each project item to remove the existing onclick="openURL('...')" and replace it with onclick="openModal(this)". Also, add data-* attributes to pass the project's data to the modal.
Here's an example for one project:
<div class="info-point-group-large" onclick="openModal(this)"
data-title="Prism"
data-description="iOS 17+ Customization & Sideloading."
data-images="prism_screenshot1.png, prism_screenshot2.png"
data-source-url="https://github.com/Broco8Dev/Prism">
<div class="info-point-large">
<div class="hstack">
<img class="material-symbols-rounded info-point-large-icon" src="prismicon.png" alt="Prism Icon">
<div class="vstack">
<h1>Prism</h1>
<h2 class="point-text">iOS 17+ Customization & Sideloading.</h2>
</div>
</div>
</div>
<span class="material-symbols-rounded info-point-large-chevron">arrow_forward_ios</span>
</div>
Repeat this for all your project items, updating the data-* attributes accordingly.
4. Add JavaScript Functions
Include the following JavaScript code in a <script> tag at the end of your HTML file, or in a separate JavaScript file:
// Function to open the modal and populate it
function openModal(element) {
// Get data from the clicked element
var title = element.getAttribute('data-title');
var description = element.getAttribute('data-description');
var images = element.getAttribute('data-images').split(',');
var sourceUrl = element.getAttribute('data-source-url');
// Set data in the modal
document.getElementById('modalTitle').innerText = title;
document.getElementById('modalDescription').innerText = description;
// Populate images
var imageGallery = document.getElementById('modalImages');
imageGallery.innerHTML = ''; // Clear previous images
images.forEach(function(imageSrc) {
var imgElement = document.createElement('img');
imgElement.src = imageSrc.trim();
imgElement.alt = title + ' Screenshot';
imageGallery.appendChild(imgElement);
});
// Set "Go to Source" button link
document.getElementById('goToSourceButton').onclick = function() {
window.open(sourceUrl, '_blank');
};
// Display the modal
document.getElementById('projectModal').style.display = 'block';
}
// Function to close the modal
function closeModal() {
document.getElementById('projectModal').style.display = 'none';
}
// Close the modal when clicking outside of the modal content
window.onclick = function(event) {
var modal = document.getElementById('projectModal');
if (event.target == modal) {
closeModal();
}
}
5. Full Updated HTML
Here's how your HTML structure would look after incorporating all the changes:
<!-- Projects Section -->
<div class="info-points">
<h1>My Projects</h1>
<div class="gradient-border-bg"></div>
<div class="info-point-groups-large">
<!-- Project Item Example -->
<div class="info-point-group-large" onclick="openModal(this)"
data-title="Prism"
data-description="iOS 17+ Customization & Sideloading."
data-images="prism_screenshot1.png, prism_screenshot2.png"
data-source-url="https://github.com/Broco8Dev/Prism">
<div class="info-point-large">
<div class="hstack">
<img class="material-symbols-rounded info-point-large-icon" src="prismicon.png" alt="Prism Icon">
<div class="vstack">
<h1>Prism</h1>
<h2 class="point-text">iOS 17+ Customization & Sideloading.</h2>
</div>
</div>
</div>
<span class="material-symbols-rounded info-point-large-chevron">arrow_forward_ios</span>
</div>
<!-- Repeat for other projects... -->
</div>
<h2 style="margin-top: 15px; margin-bottom: 0;">Other Projects on Itch.io or GitHub</h2>
</div>
<!-- Back to Top Button -->
<h3 class="context-button" style="margin-top: 250px; font-size: 14px; z-index: 10001;" onclick="scrollToTop()">Back To Top</h3>
<!-- Modal Structure -->
<div id="projectModal" class="modal">
<div class="modal-content">
<span class="close-button" onclick="closeModal()">×</span>
<h1 id="modalTitle"></h1>
<div id="modalImages" class="image-gallery"></div>
<p id="modalDescription"></p>
<button id="goToSourceButton">Go to Source</button>
</div>
</div>
<!-- JavaScript Functions -->
<script>
// Include the JavaScript functions here (as provided above)
</script>
6. Ensure All Resources are Correctly Linked
- Images: Make sure all image paths in the
src attributes and data-images are correct.
- Icons: If you're using any icon fonts or libraries (like Material Symbols), ensure they are properly included in your project.
- CSS and JavaScript: If you've placed CSS and JS in separate files, verify that they are correctly linked in your HTML
<head> and before the closing </body> tag, respectively.
7. Testing
After implementing the changes:
- Click on Project Items: Verify that clicking on different project items opens the modal with the correct information and images.
- Close Modal: Test closing the modal by clicking the close button (
×) and by clicking outside the modal content.
- Go to Source Button: Ensure that the "Go to Source" button opens the project link in a new tab.
8. Additional Enhancements
- Responsive Design: Adjust the modal and image gallery styles to ensure they look good on all screen sizes.
- Image Gallery Features: Consider adding features like image sliders or lightboxes for a better user experience if you have multiple screenshots per project.
- Accessibility: Add
aria labels and consider keyboard navigation to improve accessibility.
Troubleshooting
- Modal Not Showing: Check for any JavaScript errors in the console that might prevent functions from executing.
- Images Not Displaying: Verify the image paths and ensure they are correctly loaded.
- Styles Not Applying: Ensure the CSS is properly linked and there are no conflicting styles.
Conclusion
By following these steps, you've transformed your project section to provide an interactive experience. Users can now click on projects to view detailed information, browse screenshots, and navigate to the project's source.
Feel free to reach out if you need further assistance or have any questions!