Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

Save Automatically?

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 Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

              
                <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GSAP Slider #2</title>
    <link rel="stylesheet" href="/css/main.css">
</head>

<body>

    <div class="slider-holder">
        <div class="slider">
            <div class="slider-item" data-url="https://picsum.photos/id/126/1200/800">
                <div class="sl_title">
                    <div class="sl_main_title"><span class="span_title">Slide No.1</span></div>
                    <div class="sl_sub_title"><span class="span_title">Slide No.1 Subtitle</span></div>
                    <div class="slider-btn" onclick="buttonClick()">
                        <div class="slider-label">Learn More</div>
                        <svg version="1.0" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="200" height="50" viewBox="0 0 205 55" style="enable-background:new 0 0 205 55;" xml:space="preserve">
                            <style type="text/css">
                                .st0{fill:rgba(0,0,0,0.5);fill-opacity:0;stroke:#8BC34A;stroke-width:2;stroke-miterlimit:10;}
                            </style>
                            <path id="XMLID_93_" class="st0" d="M202.5,52.5H2.5v-50h200V52.5z"/>
                        </svg>

                    </div>
                </div>
            </div>
            <div class="slider-item" data-url="https://picsum.photos/id/166/1200/800">
                <div class="sl_title">
                    <div class="sl_main_title"><span class="span_title">Slide No.2</span></div>
                    <div class="sl_sub_title"><span class="span_title">Slide No.2 Subtitle</span></div>
                    <div class="slider-btn">
                        <div class="slider-label">Learn More</div>
                        <svg version="1.0" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="200" height="50" viewBox="0 0 205 55" style="enable-background:new 0 0 205 55;" xml:space="preserve">
                            <style type="text/css">
                                .st0{fill:rgba(0,0,0,0.5);fill-opacity:0;stroke:#8BC34A;stroke-width:2;stroke-miterlimit:10;}
                            </style>
                            <path id="XMLID_93_" class="st0" d="M202.5,52.5H2.5v-50h200V52.5z"/>
                        </svg>
                    </div>
                </div>
            </div>
            <div class="slider-item" data-url="https://picsum.photos/id/107/1200/800">
                <div class="sl_title">
                    <div class="sl_main_title"><span class="span_title">Slide No.3</span></div>
                    <div class="sl_sub_title"><span class="span_title">Slide No.3 Subtitle</span></div>
                    <div class="slider-btn">
                        <div class="slider-label">Learn More</div>
                        <svg version="1.0" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="200" height="50" viewBox="0 0 205 55" style="enable-background:new 0 0 205 55;" xml:space="preserve">
                            <style type="text/css">
                                .st0{fill:rgba(0,0,0,0.5);fill-opacity:0;stroke:#8BC34A;stroke-width:2;stroke-miterlimit:10;}
                            </style>
                            <path id="XMLID_93_" class="st0" d="M202.5,52.5H2.5v-50h200V52.5z"/>
                        </svg>
                    </div>
                </div>
            </div>
            <div class="slider-item" data-url="https://picsum.photos/id/118/1200/800">
                <div class="sl_title">
                    <div class="sl_main_title"><span class="span_title">Slide No.4</span></div>
                    <div class="sl_sub_title"><span class="span_title">Slide No.4 Subtitle</span></div>
                </div>
            </div>
            <div class="slider-item" data-url="https://picsum.photos/id/219/1200/800">
                <div class="sl_title">
                    <div class="sl_main_title"><span class="span_title">Slide No.5</span></div>
                    <div class="sl_sub_title"><span class="span_title">Slide No.5 Subtitle</span></div>
                </div>
            </div>
            <div class="slider-item" data-url="https://picsum.photos/id/390/1200/800">
                <div class="sl_title">
                    <div class="sl_main_title"><span class="span_title">Slide No.6</span></div>
                    <div class="sl_sub_title"><span class="span_title">Slide No.6 Subtitle</span></div>
                </div>
            </div>
        </div>

        <div class="slide-bullet-wrapper"></div>

        <div class="slider-nav">
            <div class="slider-nav-btn go-prev"></div>
            <div class="slider-nav-btn go-next"></div>
        </div>
    </div>

</body>
<script>
    function buttonClick() {
        alert("Button Clicked");
    }
</script>
<script src="/js/anime.min.js"></script>
<script src="/js/gsap.min.js"></script>
<script src="/js/gsap-slider.js"></script>


</html>
              
            
!

CSS

              
                * {
    margin: 0;
    padding: 0;
}

.slider {
    position: relative;
    width: 100vw;
    height: 100vh;
    background-color: #000;
    overflow: hidden;
}

.slider-item {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
}

.sl_title {
    position: absolute;
    left: 30vw;
    top: 10vh;
    color: #8BC34A;
    height: auto;
}

.sl_main_title,
.sl_sub_title {
    display: flex;
    position: relative;
    margin: 1vh 0;
    overflow: hidden;
    background-color: rgba(0,0,0,0.5);
    white-space: nowrap;
}

.sl_main_title span {
    font-size: 12vw;
    padding: .75vw;
}

.sl_sub_title span {
    font-size: 6vw;
    padding: .75vw;
}

.slider-btn {
    position: relative;
    display: none;
    justify-content: center;
    align-items: center;
    padding-top: 2vh;
    cursor: pointer;
    overflow: hidden;
}

.slider-label {
    position: absolute;
    color: #8BC34A;
}


/* Navigation */

.slider-nav-btn {
    cursor: pointer;
}

.go-prev {
    position: absolute;
    left: 0;
    bottom: 50vh;
    margin-left: 5vw;
    background-image: url("https://drive.google.com/uc?id=14DNCaPw93zLzYApGMBaTlmgjnMgyPAhh");
    background-repeat: no-repeat;
    background-position: left;
    width: 10vh;
    height: 10vh;
}

.go-prev:hover {
    background-image: url("https://drive.google.com/uc?id=1LNlfizA-2kbUSZlX8JfEPfFKvVHxtZ-I");
}

.go-next {
    position: absolute;
    right: 0;
    bottom: 50vh;
    margin-right: 5vw;
    background-image: url("https://drive.google.com/uc?id=1OmzvQdoIrNW9XR_eM3twHhQ79JTmMPxb");
    background-repeat: no-repeat;
    background-position: right;
    width: 10vh;
    height: 10vh;
}

.go-next:hover {
    background-image: url("https://drive.google.com/uc?id=1tqX3Nlh9pG1wkA1IbtzDhh_d6wsDXlyc");
}

.slide-bullet-wrapper {
    position: absolute;
    bottom: 5vh;
    display: flex;
    width: 100vw;
    justify-content: center;
}

.slide-bullet {
    margin-left: 1vw;
    width: 10vw;
    height: 5vw;
    background-color: rgba(0,0,0,0.5);
    transform: skew(40deg);
    overflow: hidden;
}

.slide-bullet-fill {
    width: 0%;
    height: 100%;
    background-color: #8BC34A;
}

.progress-wrapper {
    position: absolute;
    height: 0.65vh;
    display: flex;  
    bottom: 1vh;
    overflow: hidden;
    background-color: rgba(0,0,0,0.5);
    border-radius: 10px;
    width: 60vw;
    margin: 0 20vw 0 20vw;
}

.progress {
    background-color: #8BC34A;
    white-space: nowrap;
    border-radius: 10px;
}

@media only screen and (min-width: 1024px) {

    .sl_title {
        left: 50vw;
    }

    .sl_main_title span {
        font-size: 5vw;
        padding: .75vw;
    }
    
    .sl_sub_title span {
        font-size: 2vw;
        padding: .75vw;
    }    

    .slide-bullet {
        width: 2vw;
        height: 1vw;
    }

}
              
            
!

JS

              
                var slider = document.querySelector(".slider");
var slides = document.querySelectorAll(".slider-item");
var navPrev = document.querySelector(".go-prev");
var navNext = document.querySelector(".go-next");
var slidesNum = slides.length;
var allButtons = document.querySelectorAll(".slider-btn");
var prevSlideID = null;
var currentSlideID = 0;
var isAnimating = false;
var tl;
var startingSlide = true;
var delayTime = 6;

function init () {
    gsap.set(slides, {
        xPercent: -100
    });

    /* Set background images */
    for (i = 0; i < slides.length; i++) {
        slides[i].style.backgroundImage = `url(${slides[i].dataset.url}`;
    }

    /* Create slide bullets */
    for (i = 0; i < slides.length; i++) {
        var slideBullet = document.createElement('div');
        var slideBulletFill = document.createElement('div');
        slideBullet.setAttribute('class', 'slide-bullet');
        slideBulletFill.setAttribute('class', 'slide-bullet-fill');
        var wrapper = document.querySelector('.slide-bullet-wrapper');
        wrapper.appendChild(slideBullet);
        slideBullet.appendChild(slideBulletFill);
    }

    /* Add mouse events to buttons */

    for (let index = 0; index < allButtons.length; index++) {
        allButtons[index].addEventListener("mouseenter", hoverEnterBtn);
        allButtons[index].addEventListener("mouseleave", hoverLeaveBtn);
    }

    navPrev.onclick = function () {
        gotoPrevSlide();
    };
    navNext.onclick = function () {
        gotoNextSlide();
    };
    /* Goto first slide */
    gotoSlide(0, 0);
}

function gotoPrevSlide () {
    var slideToGo = currentSlideID - 1;
    if (slideToGo <= -1) {
        slideToGo = slidesNum - 1;
    }

    /* Unfill the current bullet before going back */
    var allBullets = document.querySelectorAll('.slide-bullet-fill');
    gsap.set(allBullets[currentSlideID], { width: "0%" });

    /* Fill all previous bullets if first bullet is actively animating */
    if (currentSlideID == 0) {
        for (let index = 0; index <= slidesNum - 2; index++) {
            gsap.to(allBullets[index], { width: "100%" });
        }
    }

    gotoSlide(slideToGo, 1, "prev");
}

function gotoNextSlide () {
    var slideToGo = currentSlideID + 1;
    if (slideToGo >= slidesNum) {
        slideToGo = 0;
    }

    var allBullets = document.querySelectorAll('.slide-bullet-fill');

    /* When all slides are complete clear out the bullets */
    if (currentSlideID >= slidesNum - 1) {
        gsap.to(allBullets, .25, { width: "0%" });
    }

    /* Fill the last bullet before going to next */
    if (currentSlideID !== slidesNum - 1) {
        gsap.to(allBullets[currentSlideID], { width: "100%" });
    }

    gotoSlide(slideToGo, 1, "next");
}

function gotoSlide (slideID, _time, _direction) {
    if (!isAnimating) {
        isAnimating = true;
        prevSlideID = currentSlideID;
        currentSlideID = slideID;
        var prevSlide = slides[prevSlideID];
        var currentSlide = slides[currentSlideID];
        var time = .8;
        if (_time !== null) {
            time = _time;
        }
        var direction = "next";
        if (_direction != null) {
            direction = _direction;
        }

        /* If timeline exists kill it */
        if (tl) {
            tl.kill();
        }

        /* Make sure that the button is hidden so we can animate it */
        var slBtn = currentSlide.querySelector('.slider-btn');

        if (slBtn) {
            slBtn.style.display = "none"
        }

        /* Animations if the slide direction is next or forward */
        if (direction == "next") {
            tl = gsap.timeline();


            if (!startingSlide) {
                /* Titles Animation for exiting previous slide */
                tl.fromTo(prevSlide.querySelectorAll('.sl_main_title, .sl_sub_title'), .25, {
                    width: "100%"
                }, {
                    width: "0%",
                    stagger: 0.1,
                    ease: Power4
                }, 0);

                /* Button outro animations */
                if (allButtons[prevSlideID] != undefined || allButtons[prevSlideID] != null) {

                    /* Button outro animation */
                    animateBtn(prevSlide.querySelector('.slider-btn'), "prev");

                    /* Button label outro animation */
                    tl.fromTo(prevSlide.querySelector('.slider-label'), .3, { opacity: 100 }, { opacity: 0 }, 0);
                }
            }

            /* Animate background images. */

            /* If not the starting slide, move 1st slide off stage. */
            if (!startingSlide) {
                tl.to(prevSlide, time, {
                    xPercent: -100,
                    ease: Power4
                }, .4);
            }

            /* No longer need starting slide variable so set to false. */
            startingSlide = false;

            /* Move next slide onto stage */
            tl.fromTo(currentSlide, time, {
                xPercent: 100
            }, {
                xPercent: 0,
                ease: Power4
            }, .4);

            /* Titles intro animation for current slide */
            tl.fromTo(currentSlide.querySelectorAll('.sl_main_title, .sl_sub_title'), .25, {
                width: "0%"
            }, {
                width: "100%",
                stagger: 0.1,
                ease: Power4
            });

            tl.fromTo(currentSlide.querySelectorAll('.span_title'), .7, {
                y: 250
            }, {
                y: 0,
                stagger: 0.1,
                ease: Power1,
                /* Button outline animation */
                onComplete: animateBtn,
                onCompleteParams: [currentSlide.querySelector('.slider-btn'), "next"]
            });

            /* Button label intro animation */
            if (allButtons[currentSlideID] != undefined || allButtons[currentSlideID] != null) {
                tl.set(currentSlide.querySelector('.slider-label'), { opacity: 100 });
                tl.fromTo(currentSlide.querySelector('.slider-label'), .25, { y: 75 }, { y: 0 }, 3.5);
            }

            /* Bullet fill */
            tl.fromTo(document.querySelectorAll('.slide-bullet-fill')[currentSlideID], delayTime, {
                width: "0%"
            }, {
                width: "100%",
                ease: Power2,
                onComplete: play
            });

        } else {
            /* Animations if the slide direction is previous or backwards */
            tl = gsap.timeline();

            /* Titles exit animation for previous slide */
            tl.fromTo(prevSlide.querySelectorAll('.sl_main_title, .sl_sub_title'), .25, {
                width: "100%"
            }, {
                width: "0%",
                stagger: 0.1,
                ease: Power4
            }, 0);

            /* Button outro animation */
            if (allButtons[prevSlideID] != undefined || allButtons[prevSlideID] != null) {
                animateBtn(prevSlide.querySelector('.slider-btn'), "prev");


                /* Button label outro animation */
                tl.fromTo(prevSlide.querySelector('.slider-label'), .3, { opacity: 100 }, { opacity: 0 }, 0);
            }


            /* Animate background images. */
            tl.to(prevSlide, time, {
                xPercent: 100,
                ease: Power4
            }, .4);
            tl.fromTo(currentSlide, time, {
                xPercent: -100
            }, {
                xPercent: 0,
                ease: Power4
            }, .4);

            /* Titles intro animation */
            tl.fromTo(currentSlide.querySelectorAll('.sl_main_title, .sl_sub_title'), .25, {
                width: "0%"
            }, {
                width: "100%",
                stagger: 0.1,
                ease: Power4
            });

            tl.fromTo(currentSlide.querySelectorAll('.span_title'), .7, {
                y: 250
            }, {
                y: 0,
                stagger: 0.1,
                ease: Power1,
                /* Button outline animation */
                onComplete: animateBtn,
                onCompleteParams: [currentSlide.querySelector('.slider-btn'), "next"]
            });

            /* Button label intro animation */
            if (allButtons[currentSlideID] != undefined || allButtons[currentSlideID] != null) {
                tl.set(currentSlide.querySelector('.slider-label'), { opacity: 100 });
                tl.fromTo(currentSlide.querySelector('.slider-label'), .25, { y: 75 }, { y: 0 }, 3.5);
            }

            /* Bullet fill */
            tl.fromTo(document.querySelectorAll('.slide-bullet-fill')[currentSlideID], delayTime, {
                width: "0%"
            }, {
                width: "100%",
                ease: Power2,
                onComplete: play
            });

        }

        gsap.delayedCall(time, function () {
            isAnimating = false;
        });

    }
}

function play () {

    gotoNextSlide();
}

function animateBtn (slide, direction) {

    if (slide) {

        /* The button is hidden so we need to show the button before animating */
        slide.style.display = "flex";
        slideSVG = slide.querySelector('svg path');

        /* SVG button outline animation */
        if (direction == "next") {
            anime({
                targets: slideSVG,
                strokeDashoffset: [anime.setDashoffset, 0],
                easing: 'easeInOutSine',
                duration: 800,
                delay: function (el, i) {
                    return i * 250
                }
            });
        } else {
            anime({
                targets: slideSVG,
                strokeDashoffset: [0, anime.setDashoffset],
                easing: 'easeInOutSine',
                duration: 380,
                delay: function (el, i) {
                    return i * 250
                }
            });
        }
    }
}

function hoverEnterBtn () {

    anime({
        targets: 'svg path',
        fillOpacity: 1,
        easing: 'easeInOutSine',
        duration: 500,
    });
}

function hoverLeaveBtn () {

    anime({
        targets: 'svg path',
        fillOpacity: 0,
        easing: 'easeInOutSine',
        duration: 500,
    });
}

init();
              
            
!
999px

Console