Flex Panels Image Gallery Using JavaScript

Flex Panels Image Gallery Using JavaScript
Code Snippet:Flex panel gallery | JS30
Author: Lukaszewska
Published: January 9, 2024
Last Updated: January 22, 2024
Downloads: 546
License: MIT
Edit Code online: View on CodePen
Read More

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.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X