Expanding Image Carousel in JavaScript

Expanding Image Carousel in JavaScript
Code Snippet:Flex Panel Gallery
Author: Bishal Peter Dores
Published: January 9, 2024
Last Updated: January 22, 2024
Downloads: 765
License: MIT
Edit Code online: View on CodePen
Read More

This code creates an image carousel with expanding panels. Clicking on a panel opens it, revealing a full-screen image. The code uses HTML, CSS, and JavaScript to achieve this effect. It’s a visually appealing way to showcase images or content in a dynamic manner.

It’s great for showcasing products, portfolio items, or featured content in a visually appealing way.

How to Create an Expanding Image Carousel in JavaScript

1. Begin by setting up the HTML structure. Inside your HTML file, create a <div> with the class “panels” and individual panels with the class “panel.” Each panel will contain the content you want to showcase. Make sure to include images in the panel backgrounds.

<div class="panels">
    <div class="panel panel1">
        <p>Whatever</p>
        <p>It</p>
        <p>Takes</p>
    </div>
    <div class="panel panel2">
        <p>Cause</p>
        <p>I</p>
        <p>Love</p>
    </div>
    <div class="panel panel3">
        <p>The</p>
        <p>Adrenaline</p>
        <p>In</p>
    </div>
    <div class="panel panel4">
        <p>My</p>
        <p>Veins</p>
        <p>I</p>
    </div>
    <div class="panel panel5">
        <p>Do</p>
        <p>What It</p>
        <p>Takes !!!</p>
    </div>
</div>

2. Style your panels and define their appearance in the CSS section. You can customize the backgrounds, fonts, and other visual elements to match your website’s design. Add the following CSS code to your project:

html {
    box-sizing: border-box;
    background: #ffc600;
    font-family: 'helvetica neue';
    font-size: 20px;
    font-weight: 200;
}

body {
    margin: 0;
}

*,
*:before,
*:after {
    box-sizing: inherit;
}
.panels {
    height: 100vh;
    overflow: hidden;
    display: flex;
}
.panel{
    box-shadow: inset 0 0 0 5px rgba(255, 255, 255, 0.1);
    color: darkorange;
    text-align: center;
    align-items: center;
    transition:
            font-size 0.7s cubic-bezier(0.61, -0.19, 0.7, -0.11),
            flex 0.7s cubic-bezier(0.61, -0.19, 0.7, -0.11),
            background 0.2s;
    font-size: 20px;
    background: #6B0F9C center;
    background-size: cover;
    display: flex;
    flex-direction: column;
    justify-content: center;
    flex: 1;
}
.panel1{
    background-image: url(https://images.unsplash.com/photo-1526506118085-60ce8714f8c5?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjF9);
}
.panel2{
    background-image: url(https://images.unsplash.com/photo-1517836357463-d25dfeac3438?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjF9);
}
.panel3{
    background-image: url(https://images.unsplash.com/photo-1532384748853-8f54a8f476e2?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjF9);
}
.panel4{
    background-image: url(https://images.unsplash.com/photo-1434596922112-19c563067271?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjF9);
}
.panel5{
    background-image: url(https://images.unsplash.com/photo-1506197061617-7f5c0b093236?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjF9);
}
/*Flex Children*/
.panel>* {
    margin: 0;
    width: 100%;
    transition: transform 0.5s;
    display: flex;
    flex: 1 0 auto;
    justify-content: center;
    align-items: center;
}
.panel>*:first-child{
    transform: translateY(-100%);
}
.panel.open-active>*:first-child{
    transform: translateY(0);
}
.panel>*:last-child{
    transform: translateY(100%);
}
.panel.open-active>*:last-child{
    transform: translateY(0);
}
.panel p {
    text-transform: uppercase;
    font-family: 'Amatic SC', cursive;
    text-shadow: 0 0 4px rgba(0, 0, 0, 0.72), 0 0 14px rgba(0, 0, 0, 0.45);
    font-size: 2em;
}
.panel p:nth-child(2) {
    font-size: 4em;
}

.panel.open {
    font-size: 40px;
    flex: 5;
}

3. To make the panels expand and contract, we’ll use JavaScript. Include the following JavaScript code in your HTML file. It adds interactivity to the panels, making them expand when clicked.

const panels = document.querySelectorAll('.panel');
    function toggleOpen() {
        this.classList.toggle('open')
    }
    function toggleActive(e){
        if(e.propertyName.includes('flex')){
            this.classList.toggle('open-active')
        }
    }

    panels.forEach((panel)=>panel.addEventListener('click',toggleOpen));
    panels.forEach((panel)=>panel.addEventListener('transitionend',toggleActive));

That’s all! hopefully, you have successfully created an Expanding Image Carousel in 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