This Vanilla JavaScript code snippet helps you to create 3D carousel slider with arrow buttons. It uses Font Awesome icons for the next and previous buttons that allows user to navigate the slider.
This slider is alternative to coverflow effect slider that doesn’t relay on jQuery. It provides 3D styled sliding containers in which you can place images, iframe, videos, or any other HTML element.
How to Create 3D carousel slider JavaScript
1. First of all, load the Font Awesome and Google fonts CSS by adding the following CDN links into the head tag of your HTML document.
<link rel='stylesheet' href='https://static.fontawesome.com/css/fontawesome-app.css'> <link rel='stylesheet' href='https://pro.fontawesome.com/releases/v5.9.0/css/all.css'> <link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400&display=swap'>
2. After that, create a div element with a class name “carousel” and place div elements for next previous buttons and slides as given below. Inside the slide, you can place your images or videos.
<p>3D Carousel/Slider!</p> <div class="carousel"> <div class="carousel__body"> <div class="carousel__prev"><i class="far fa-angle-left"></i></div> <div class="carousel__next"><i class="far fa-angle-right"></i></div> <div class="carousel__slider"> <div class="carousel__slider__item"> <div class="item__3d-frame"> <div class="item__3d-frame__box item__3d-frame__box--front"> <h1>1</h1> </div> <div class="item__3d-frame__box item__3d-frame__box--left"></div> <div class="item__3d-frame__box item__3d-frame__box--right"> </div> </div> </div> <div class="carousel__slider__item"> <div class="item__3d-frame"> <div class="item__3d-frame__box item__3d-frame__box--front"> <h1>2</h1> </div> <div class="item__3d-frame__box item__3d-frame__box--left"></div> <div class="item__3d-frame__box item__3d-frame__box--right"> </div> </div> </div> <div class="carousel__slider__item"> <div class="item__3d-frame"> <div class="item__3d-frame__box item__3d-frame__box--front"> <h1>3</h1> </div> <div class="item__3d-frame__box item__3d-frame__box--left"></div> <div class="item__3d-frame__box item__3d-frame__box--right"> </div> </div> </div> <div class="carousel__slider__item"> <div class="item__3d-frame"> <div class="item__3d-frame__box item__3d-frame__box--front"> <h1>4</h1> </div> <div class="item__3d-frame__box item__3d-frame__box--left"></div> <div class="item__3d-frame__box item__3d-frame__box--right"> </div> </div> </div> <div class="carousel__slider__item"> <div class="item__3d-frame"> <div class="item__3d-frame__box item__3d-frame__box--front"> <h1>5</h1> </div> <div class="item__3d-frame__box item__3d-frame__box--left"></div> <div class="item__3d-frame__box item__3d-frame__box--right"> </div> </div> </div> <div class="carousel__slider__item"> <div class="item__3d-frame"> <div class="item__3d-frame__box item__3d-frame__box--front"> <h1>6</h1> </div> <div class="item__3d-frame__box item__3d-frame__box--left"></div> <div class="item__3d-frame__box item__3d-frame__box--right"> </div> </div> </div> <div class="carousel__slider__item"> <div class="item__3d-frame"> <div class="item__3d-frame__box item__3d-frame__box--front"> <h1>7</h1> </div> <div class="item__3d-frame__box item__3d-frame__box--left"></div> <div class="item__3d-frame__box item__3d-frame__box--right"> </div> </div> </div> <div class="carousel__slider__item"> <div class="item__3d-frame"> <div class="item__3d-frame__box item__3d-frame__box--front"> <h1>8</h1> </div> <div class="item__3d-frame__box item__3d-frame__box--left"></div> <div class="item__3d-frame__box item__3d-frame__box--right"> </div> </div> </div> <div class="carousel__slider__item"> <div class="item__3d-frame"> <div class="item__3d-frame__box item__3d-frame__box--front"> <h1>9</h1> </div> <div class="item__3d-frame__box item__3d-frame__box--left"></div> <div class="item__3d-frame__box item__3d-frame__box--right"> </div> </div> </div> <div class="carousel__slider__item"> <div class="item__3d-frame"> <div class="item__3d-frame__box item__3d-frame__box--front"> <h1>10</h1> </div> <div class="item__3d-frame__box item__3d-frame__box--left"></div> <div class="item__3d-frame__box item__3d-frame__box--right"> </div> </div> </div> </div> </div> </div>
3. Add the following CSS to style the 3D slider:
:root { --crsl-bg: transparent; --box-bg: #1e272e; --box-shadow: #0000001c; --box-shadow-border: #0000000f; --box-border: #444; } html > p, body > p { font-size: 0.95em; font-weight: bold; position: absolute; top: 7.5%; width: 100%; letter-spacing: 4px; text-transform: uppercase; text-align: center; color: white; user-select: none; } .carousel { position: relative; display: block; width: 100%; min-height: 640px; box-sizing: border-box; } .carousel__prev, .carousel__next { position: absolute; bottom: 0; transition: transform 0.25s ease; } .carousel__prev i, .carousel__next i { font-size: 60px; color: var(--box-border); cursor: pointer; } .carousel__prev:hover, .carousel__next:hover { transform: scale(1.25); } .carousel__prev { left: 40%; } .carousel__next { right: 40%; } .carousel__body { width: 100%; padding: 20px 0 50px 0; overflow: hidden; } .carousel__body .carousel__slider { position: relative; transition: transform 1s ease-in-out; background: var(--crsl-bg); } .carousel__body .carousel__slider__item { position: relative; display: block; float: left; box-sizing: border-box; margin-left: 20px; margin-right: 20px; } .carousel__body .carousel__slider__item .item__3d-frame { position: relative; width: 100%; height: 100%; transition: transform 1s ease-in-out; -webkit-transform-style: preserve-3d; transform-style: preserve-3d; } .carousel__body .carousel__slider__item .item__3d-frame:after { content: ""; position: absolute; bottom: -15%; width: 100%; height: 40px; background: var(--box-shadow); box-shadow: 0px 0px 5px 5px var(--box-shadow); transform: rotateX(90deg) translate3d(0px, -20px, 0px); opacity: 0.85; } .carousel__body .carousel__slider__item .item__3d-frame__box { display: flex; align-items: center; vertical-align: middle; text-align: center; position: absolute; width: 100%; height: 100%; box-sizing: border-box; border-color: var(--box-border); background: var(--box-bg); border-width: 3px; border-style: solid; } .carousel__body .carousel__slider__item .item__3d-frame__box h1 { font-size: 7em; width: 100%; color: var(--box-border); } .carousel__body .carousel__slider__item .item__3d-frame__box--right, .carousel__body .carousel__slider__item .item__3d-frame__box--left { top: 0; width: 40px; backface-visibility: hidden; } .carousel__body .carousel__slider__item .item__3d-frame__box--left { left: 0; border-left-width: 5px; transform: translate3d(1px, 0, -40px) rotateY(-90deg); transform-origin: 0%; } .carousel__body .carousel__slider__item .item__3d-frame__box--right { right: 0; border-right-width: 5px; transform: translate3d(-1px, 0, -40px) rotateY(90deg); transform-origin: 100%; }
4. Finally, include the following JavaScript function between the <script> and </script> tag before closing the body tag and done.
(function() { "use strict"; var carousel = document.getElementsByClassName('carousel')[0], slider = carousel.getElementsByClassName('carousel__slider')[0], items = carousel.getElementsByClassName('carousel__slider__item'), prevBtn = carousel.getElementsByClassName('carousel__prev')[0], nextBtn = carousel.getElementsByClassName('carousel__next')[0]; var width, height, totalWidth, margin = 20, currIndex = 0, interval, intervalTime = 4000; function init() { resize(); move(Math.floor(items.length / 2)); bindEvents(); timer(); } function resize() { width = Math.max(window.innerWidth * .25, 275), height = window.innerHeight * .5, totalWidth = width * items.length; slider.style.width = totalWidth + "px"; for(var i = 0; i < items.length; i++) { let item = items[i]; item.style.width = (width - (margin * 2)) + "px"; item.style.height = height + "px"; } } function move(index) { if(index < 1) index = items.length; if(index > items.length) index = 1; currIndex = index; for(var i = 0; i < items.length; i++) { let item = items[i], box = item.getElementsByClassName('item__3d-frame')[0]; if(i == (index - 1)) { item.classList.add('carousel__slider__item--active'); box.style.transform = "perspective(1200px)"; } else { item.classList.remove('carousel__slider__item--active'); box.style.transform = "perspective(1200px) rotateY(" + (i < (index - 1) ? 40 : -40) + "deg)"; } } slider.style.transform = "translate3d(" + ((index * -width) + (width / 2) + window.innerWidth / 2) + "px, 0, 0)"; } function timer() { clearInterval(interval); interval = setInterval(() => { move(++currIndex); }, intervalTime); } function prev() { move(--currIndex); timer(); } function next() { move(++currIndex); timer(); } function bindEvents() { window.onresize = resize; prevBtn.addEventListener('click', () => { prev(); }); nextBtn.addEventListener('click', () => { next(); }); } init(); })();
That’s all! hopefully, you have successfully created 3D carousel slider. If you have any questions or suggestions, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.