Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <p>Read <a href="https://getbutterfly.com/how-i-created-my-homepage-javascript-post-carousel/">How I Created My Homepage JavaScript Post Carousel</a></p>

<div id="carousel-wrapper">
    <div id="menu">
        <div id="current-option">
            <a id="current-option-permalink" href=""><span id="current-option-text1" data-previous-text="" data-next-text=""></span></a>
            <span id="current-option-text2" data-previous-text="" data-next-text=""></span>
        </div>
        <div id="image"></div>
    </div>

    <button id="previous-option"></button>
    <button id="next-option"></button>
</div>
              
            
!

CSS

              
                /* DEMO STYLES ONLY */
body {
    font-family: "Space Mono";
    padding: 4em;
}
/* END DEMO STYLES */

#carousel-wrapper,
#carousel-wrapper #menu {
    width: 100%;

    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
}

#carousel-wrapper {
    position: relative;
    height: auto;
}

#carousel-wrapper #menu {
    height: 380px;
    overflow: hidden;
    vertical-align: middle;
    box-shadow: 0px 0px 12px 0px rgb(0 0 0 / 20%);
    border-radius: 5px;

    transition: all 0.6s ease-in-out;
}

#carousel-wrapper #menu #current-option {
    position: relative;
    width: 50%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    overflow: hidden;
}

#carousel-wrapper #menu #current-option a {
    color: var(--primary_colour);
}

#carousel-wrapper #menu #current-option #current-option-text1,
#carousel-wrapper #menu #current-option #current-option-text2 {
    width: 240px;

    display: flex;
    flex-direction: column;
    align-items: flex-start;
}

#carousel-wrapper #menu #current-option #current-option-text1 {
    font-size: 24px;
    font-weight: 700;
    line-height: 1.35;
    height: 200px;
    justify-content: flex-start;
}

#carousel-wrapper #menu #current-option #current-option-text2 {
    font-size: 0.8rem;
    height: 40px;
    justify-content: flex-end;
}

#carousel-wrapper #menu #current-option #current-option-text1::before,
#carousel-wrapper #menu #current-option #current-option-text1::after {
    position: absolute;
    width: 240px;
    height: 200px;

    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-start;
}

#carousel-wrapper #menu #current-option #current-option-text1::before {
    content: attr(data-next-text);
    transform: translate(0%, 380px);
}

#carousel-wrapper #menu #current-option #current-option-text1::after {
    content: attr(data-previous-text);
    transform: translate(0%, -380px);
}

#carousel-wrapper #menu #current-option #current-option-text2::before,
#carousel-wrapper #menu #current-option #current-option-text2::after {
    position: absolute;
    width: 240px;
    height: 40px;

    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-end;
}

#carousel-wrapper #menu #current-option #current-option-text2::before {
    content: attr(data-next-text);
    transform: translate(0%, 380px);
}

#carousel-wrapper #menu #current-option #current-option-text2::after {
    content: attr(data-previous-text);
    transform: translate(0%, -380px);
}

#carousel-wrapper #previous-option,
#carousel-wrapper #next-option {
    width: 1.5rem;
    height: 1.5rem;
    border: none;
    display: block;
    cursor: pointer;
    background: url("data:image/svg+xml,%3Csvg version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' viewBox='0 0 256 256'%3E%3Cpolygon points='225.813,48.907 128,146.72 30.187,48.907 0,79.093 128,207.093 256,79.093' fill='%23333'%3E%3C/polygon%3E%3C/svg%3E");
    background-size: cover;
    position: absolute;
    right: 0;
}

#carousel-wrapper #previous-option {
    transform: scale(1.2) translate(10px, 24px);
}

#carousel-wrapper #next-option {
    transform: scale(1.2) translate(10px, -24px) rotate(180deg);
}

#carousel-wrapper #image {
    height: 240px;
    width: 240px;

    border-radius: 5px;

    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
}

#carousel-wrapper.anim-previous,
#carousel-wrapper.anim-next {
    pointer-events: none;
}

#carousel-wrapper.anim-next #current-option-text1,
#carousel-wrapper.anim-next #current-option-text2 {
    animation: next-text 0.65s 0.085s;
}

#carousel-wrapper.anim-previous #image,
#carousel-wrapper.anim-next #image {
    animation: previous-next-image 0.65s 0.085s;
}

#carousel-wrapper.anim-previous #current-option-text1,
#carousel-wrapper.anim-previous #current-option-text2 {
    animation: previous-text 0.65s 0.085s;
}

@keyframes previous-text {

    50%,
    55% {
        transform: translate(0%, 390px);
    }

    to {
        transform: translate(0%, 380px);
    }
}

@keyframes previous-next-image {
    0% {
        transform: scale(1);
        opacity: 1;
    }

    70% {
        transform: scale(1.1);
        opacity: 0;
    }

    100% {
        transform: scale(1);
        opacity: 1;
    }
}

@keyframes next-text {

    50%,
    55% {
        transform: translate(0%, -390px);
    }

    to {
        transform: translate(0%, -380px);
    }
}

@media only screen and (max-width: 480px) {
    #carousel-wrapper #menu {
        height: 430px;
    }

    #carousel-wrapper,
    #carousel-wrapper #menu {
        flex-direction: column;
    }

    #carousel-wrapper #menu #current-option {
        width: 240px;
    }
}
              
            
!

JS

              
                const text1_options = [
    'Lorem ipsum dolor sit amet',
    'Lorem ipsum dolor sit amet',
    'Lorem ipsum dolor sit amet',
    'Lorem ipsum dolor sit amet',
    'Lorem ipsum dolor sit amet',
];
const text2_options = [
    'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, nunc ut aliquam ultricies, nunc nisl aliquam nisl, eget aliquam nisl nisl sit amet lorem.',
    'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, nunc ut aliquam ultricies, nunc nisl aliquam nisl, eget aliquam nisl nisl sit amet lorem.',
    'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, nunc ut aliquam ultricies, nunc nisl aliquam nisl, eget aliquam nisl nisl sit amet lorem.',
    'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, nunc ut aliquam ultricies, nunc nisl aliquam nisl, eget aliquam nisl nisl sit amet lorem.',
    'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, nunc ut aliquam ultricies, nunc nisl aliquam nisl, eget aliquam nisl nisl sit amet lorem.',
];
const color_options = ['#FFC312', '#F79F1F', '#C4E538', '#A3CB38', '#12CBC4'];
const image_options = [
    'https://images.unsplash.com/photo-1534214526114-0ea4d47b04f2?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D',
    'https://images.unsplash.com/photo-1533050487297-09b450131914?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D',
    'https://images.unsplash.com/photo-1493515322954-4fa727e97985?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D',
    'https://images.unsplash.com/photo-1517541166507-dface5dd8b2b?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D',
    'https://images.unsplash.com/photo-1700055065100-b4eda9bc1ef6?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D',
];
const permalink_options = [
    '#',
    '#',
    '#',
    '#',
    '#',
];

var i = 0;
const currentOptionText1 = document.getElementById("current-option-text1");
const currentOptionText2 = document.getElementById("current-option-text2");
const currentOptionImage = document.getElementById("image");
const currentOptionPermalink = document.getElementById("current-option-permalink");

const carousel = document.getElementById("carousel-wrapper");
const mainMenu = document.getElementById("menu");
const optionPrevious = document.getElementById("previous-option");
const optionNext = document.getElementById("next-option");

currentOptionText1.innerText = text1_options[i];
currentOptionText2.innerText = text2_options[i];
currentOptionImage.style.backgroundImage = "url(" + image_options[i] + ")";
currentOptionPermalink.href = permalink_options[i];
mainMenu.style.background = color_options[i];

optionNext.onclick = function () {
    i = i + 1;
    i = i % text1_options.length;
    currentOptionText1.dataset.nextText = text1_options[i];
    currentOptionText2.dataset.nextText = text2_options[i];

    mainMenu.style.background = color_options[i];
    carousel.classList.add("anim-next");
  
    setTimeout(() => {
        currentOptionImage.style.backgroundImage = "url(" + image_options[i] + ")";
    }, 455);
  
    setTimeout(() => {
        currentOptionText1.innerText = text1_options[i];
        currentOptionText2.innerText = text2_options[i];
        currentOptionPermalink.href = permalink_options[i];
        carousel.classList.remove("anim-next");
    }, 650);
};

optionPrevious.onclick = function () {
    if (i === 0) {
        i = text1_options.length;
    }
    i = i - 1;
    currentOptionText1.dataset.previousText = text1_options[i];
    currentOptionText2.dataset.previousText = text2_options[i];

    mainMenu.style.background = color_options[i];
    carousel.classList.add("anim-previous");

    setTimeout(() => {
        currentOptionImage.style.backgroundImage = "url(" + image_options[i] + ")";
    }, 455);
  
    setTimeout(() => {
        currentOptionText1.innerText = text1_options[i];
        currentOptionText2.innerText = text2_options[i];
        currentOptionPermalink.href = permalink_options[i];
        carousel.classList.remove("anim-previous");
    }, 650);
};
              
            
!
999px

Console