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 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

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

              
                    <div class="container">

        <div class="title">Carousel 1</div>
        <div id="carousel1">

            <div class="item">
                <div class="item_image">
                    <img src="https://fillmurray.com/800/480" alt="">
                </div>
                <div class="item_body">
                    <div class="item_title">Title 1</div>
                    <div class="item_desc">
                        Lorem ipsum dolor sit amet consectetur adipisicing elit.
                    </div>
                </div>
            </div>

            <div class="item">
                <div class="item_image">
                    <img src="https://fillmurray.com/800/480" alt="">
                </div>
                <div class="item_body">
                    <div class="item_title">Title 2</div>
                    <div class="item_desc">
                        Lorem ipsum dolor sit amet consectetur adipisicing elit.
                    </div>
                </div>
            </div>

            <div class="item">
                <div class="item_image">
                    <img src="https://fillmurray.com/800/480" alt="">
                </div>
                <div class="item_body">
                    <div class="item_title">Title 3</div>
                    <div class="item_desc">
                        Lorem ipsum dolor sit amet consectetur adipisicing elit.
                    </div>
                </div>
            </div>

            <div class="item">
                <div class="item_image">
                    <img src="https://fillmurray.com/800/480" alt="">
                </div>
                <div class="item_body">
                    <div class="item_title">Title 4</div>
                    <div class="item_desc">
                        Lorem ipsum dolor sit amet consectetur adipisicing elit.
                    </div>
                </div>
            </div>

            <div class="item">
                <div class="item_image">
                    <img src="https://fillmurray.com/800/480" alt="">
                </div>
                <div class="item_body">
                    <div class="item_title">Title 5</div>
                    <div class="item_desc">
                        Lorem ipsum dolor sit amet consectetur adipisicing elit.
                    </div>
                </div>
            </div>

        </div>

    </div>



              
            
!

CSS

              
                * {
    box-sizing: border-box;
}

body,
html {
    margin: 0;
    padding: 0;
    min-height: 100%;
    overflow-x: hidden;

}

body {
    font-family: sans-serif;
    padding: 0 50px 50px;
    background: linear-gradient(45deg, #f37646, #f3e946) 50% no-repeat;
    line-height: 1.4;
}

.title {
    font-size: 25px;
    color: #fff;
    font-weight: 700;
    margin-bottom: 25px;
}

.carousel {
    margin-bottom: 30px;
}

.item{
    margin: 40px 10px;
    background-color: #fff;
    box-shadow: 0 15px 35px #3232321a, 0 5px 15px #0000001a;
    transition: transform 0.3s;
    transform: translate3d(0, -1px, 0);
}
.item:hover {
    transform: translate3d(0, -10px, 0);
}
.item img {
    display: block;
    width: 100%;
    height: auto;
}

.item_body {
    padding: 25px;
}

.item_title {
    font-style: 24px;
    margin-bottom: 5px;
    font-weight: bold;
    color: #232323;
}

.item_desc {
    color: #585858;
}

/*
CAROUSEL
*/

.carousel {
    position: relative;
}

.carousel_container {
    transition: transform 0.3s;
    transform: translate3d(0, 0, 0);
}

/* technique pour éviter le vide de flux */
.carousel_container::after {
    content: "";
    clear: both;
    display: table;
}

.carousel_item {
    float: left;
}

.carousel_next, .carousel_prev {
    position: absolute;
    width: 40px;
    height: 40px;
    background: #fff url(https://banner2.kisspng.com/20180203/tsq/kisspng-arrow-ico-icon-right-arrow-png-photo-5a758aa9a205b1.5795578115176526496637.jpg) center center no-repeat;
    background-size: 20px 20px;
    border-radius: 50%;
    top: 50%;
    margin-top: -20px;
    right: -20px;
    box-shadow: 0 15px 35px #3232321a, 0 5px 15px #0000001a;
    cursor: pointer;
    transition: transform 0.3s;
}

.carousel_next:hover, .carousel_prev:hover {
    transform: scale(1.2);
}
.carousel_prev {
    background-image: url(https://banner2.kisspng.com/20180417/wiw/kisspng-font-awesome-arrow-web-typography-computer-icons-left-arrow-5ad67e34deecc8.0026425615240064529131.jpg);
    right: auto;
    left: -20px;
}
              
            
!

JS

              
                /**
 * usejsdoc pour des commentaires uniformisés
 * @param {HTMLElement} element
 * @param {Object} options
 * @param {Object} options.slideToScroll Nombre élément à faire défiler
 * @param {Object} options.slidesVisible Nombre élément visibles dans un slide
 */

class Carousel {

    constructor (element, options = {}) {
        this.element = element
        this.options = Object.assign({}, {
            slideToScroll: 1,
            slidesVisible: 1
        }, options)
        let children = [].slice.call(element.children)
        this.currentItem = 0
        this.root = this.createDivWithClass('carousel')
        this.container = this.createDivWithClass('carousel_container')
        this.root.appendChild(this.container)
        this.element.appendChild(this.root)
        this.items = children.map((child) => {
            let item = this.createDivWithClass('carousel_item')
            item.appendChild(child)
            this.container.appendChild(item)
            return item
        })
        this.setStyle()
        this.createNavigation()
    }
    
    setStyle () {
        let ratio = this.items.length / this.options.slidesVisible
        this.container.style.width = (ratio * 100) + "%"
        this.items.forEach(item => item.style.width = ((100 / this.options.slidesVisible) / ratio) + "%")
    }

    /**
     * Applique les bonnes dimensions aux éléments du carousel
     */
    createNavigation () {
        let nextButton = this.createDivWithClass('carousel_next')
        let prevButton = this.createDivWithClass('carousel_prev')
        this.root.appendChild(nextButton)
        this.root.appendChild(prevButton)
        nextButton.addEventListener('click', this.next.bind(this))
        prevButton.addEventListener("click", this.prev.bind(this))

    }

    next() {
        this.gotToItem(this.currentItem + this.options.slideToScroll)
    }
    
    prev() {
        this.gotToItem(this.currentItem - this.options.slideToScroll)
    }

    /**
     * Déplace le carousel vers l'élément ciblé
     * @param {number} index
     */
    gotToItem (index) {
        if (index < 0) {
            index = this.items.length - this.options.slidesVisible
        } else if (index >= this.items.length) {
            index = 0
        }
        let translateX = index * -100 / this.items.length
        this.container.style.transform = 'translate3d(' + translateX + '%, 0, 0)'
        this.currentItem = index
    }


    /**
     * @param {string} className
     */
    createDivWithClass (className) {
        let div = document.createElement('div')
        div.setAttribute('class', className)
        return div
    }

}


// je lance le chargement de la fonction après le chargement du DOM (async)
document.addEventListener('DOMContentLoaded', function () {

    // j'instancie un objet qui se nomme Carousel
    new Carousel (document.querySelector('#carousel1'), {
        //slideToScroll:3,
        slidesVisible: 4
    })

})
              
            
!
999px

Console