This code creates a Flex Panels Image Gallery using JavaScript. It enables the user to click on panels, which then expand and reveal their content. The code uses JavaScript to toggle classes for opening and activating the panels when they are clicked. This interactive gallery is helpful for creating engaging and dynamic web page content.
You can use this code to enhance your website by creating an interactive image gallery.
How to Create Flex Panels Image Gallery Using JavaScript
1. First, create the necessary HTML structure for your Flex Panels Gallery. In the following code, a div with the id "parts"
contains multiple div elements with the class “part.” Each “part” represents a panel in the gallery. Customize the content within each panel to your liking.
<div id="parts"> <div class="part part1"> <p>Hey</p> <p>Let's</p> <p>Have Fun</p> </div> <div class="part part2"> <p>Relax,</p> <p>Take</p> <p>It Easy</p> </div> <div class="part part3"> <p>Make</p> <p>It</p> <p>True</p> </div> <div class="part part4"> <p>Give</p> <p>All</p> <p>The Best</p> </div> <div class="part part5"> <p>Live</p> <p>In</p> <p>The Moment</p> </div> </div>
2۔ Style your gallery with CSS. The following CSS code sets the background colors, fonts, and layout. You can modify these styles to match your website’s design. The essential CSS properties to note include "display: flex"
for the container and transitions for smooth animations when panels open.
* { box-sizing: border-box; margin: 0; padding: 0; } body { background-color:#1C0602; font-family: 'Merienda One', cursive; color: #FFEACE; font-size: 20px; } #parts { min-height: 100vh; overflow: hidden; display: flex; /*flex container*/ } .part { text-align: center; flex: 1; /*flex item, każdy zajmuje tyle samo miejsca czyli 1*/ display: flex; /*flex container dla el. p*/ justify-content: center; flex-direction: column; /*ustawia w kolumnach flex-items czyli tutaj p*/ transition: font-size .7s, flex .7s; } .part.open { font-size: 2em; flex: 5; /*kiedy panel jest otwarty zajmuje 5*więcej miejsca niż panel zamkniety*/ } .part p { width: 100%; flex: 1 0 auto; display: flex; /*flex container dla p*/ justify-content: center; align-items: center; transition: transform .5s; } .part p:nth-child(2) { font-size: 2em; text-transform: uppercase; } .part p:first-child { transform: translateY(-100%); /*znikają górne napisy*/ } .part.open-active p:first-child { transform: translateY(0); /*pojawiają się górne napisy*/ } .part p:last-child { transform: translateY(100%); /*znikają dolne napisy*/ } .part.open-active p:last-child { transform: translateY(0); /*pojawiają się dolne napisy*/ } .part1 { background-color: #FF992C; } .part2 { background-color: #F27823; } .part3 { background-color: #F26320; } .part4 { background-color: #BF3E18; } .part5 { background-color: #8C1D0C; } @media screen and (max-width: 1000px) { body { font-size: 10px; } .part.open { font-size: 1.8em; flex: 3; } .part p:nth-child(2) { font-size: 2em; } } @media screen and (max-width: 530px) { body { font-size: 6px; } }
3. The JavaScript code in the example makes the gallery interactive. It toggles classes to open and activate the panels. To add this functionality to your gallery, create a JavaScript file or include the code in a script tag within your HTML document.
const parts = document.querySelectorAll('.part'); function toggleOpen() { this.classList.toggle('open'); } function toggleActive(e) { if(e.propertyName.includes('flex')) { this.classList.toggle('open-active'); } } parts.forEach(part => part.addEventListener('click', toggleOpen)); parts.forEach(part => part.addEventListener('transitionend', toggleActive));
That’s all! hopefully, you have successfully created a Flex Panels Image Gallery Using HTML, CSS, and JavaScript. If you have any questions or suggestions, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.