This JavaScript code creates a vertical image slider on a web page. It allows you to display multiple images with corresponding content in a stylish, sliding manner. The core functionality is to move between these images and their associated text by clicking the up and down buttons.
This slider enhances user engagement and is helpful for showcasing content or products in an interactive way on your website.
How to Create Vertical Image Slider in JavaScript
1. First of all, load the Font Awesome CSS (for icons) by adding the following CDN link to the head tag of your website.
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" />
2. Copy the following HTML structure and paste it where you want to display the slider. This structure includes the slider container, left and right slide sections, and action buttons. Replace the sample images and content with your own. Each <div>
element within the .left-slide
section represents a slide. Customize the background images, headings (h1), and text (p) to match your content.
<div class="slider-container"> <div class="left-slide"> <div style="background-color: #fde"> <h1>Pink Cherrish</h1> <p>to evolve into care</p> </div> <div style="background-color: rgb(254,160,0)"> <h1>Yellow Luck</h1> <p>Shinning the clouds</p> </div> <div style="background-color: rgb(231, 58, 95)"> <h1>Redness Lust</h1> <p>in the wilderness</p> </div> <div style="background-color: rgb(176, 136, 240)"> <h1>Lavanda Love</h1> <p>in the sunset</p> </div> </div> <div class="right-slide"> <div style="background-image: url('https://images.unsplash.com/photo-1528756514091-dee5ecaa3278?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MXx8Zmxvd2Vyc3xlbnwwfHwwfA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1296&q=60');"></div> <div style="background-image: url('https://images.unsplash.com/photo-1519378058457-4c29a0a2efac?ixid=MXwxMjA3fDB8MHxzZWFyY2h8Mnx8Zmxvd2Vyc3xlbnwwfHwwfA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1296&q=60');"></div> <div style="background-image: url('https://images.unsplash.com/photo-1490750967868-88aa4486c946?ixid=MXwxMjA3fDB8MHxzZWFyY2h8M3x8Zmxvd2Vyc3xlbnwwfHwwfA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1296&q=60');"></div> <div style="background-image: url('https://images.unsplash.com/photo-1462275646964-a0e3386b89fa?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1400&q=80');"></div> </div> <div class="action-buttons"> <button class="down-button"> <i class="fas fa-arrow-down"></i> </button> <button class="up-button"> <i class="fas fa-arrow-up"></i> </button> </div> </div>
3. The code relies on CSS styles for layout and appearance. Make sure to include the following CSS code by adding it to the <style>
tag in your website’s HTML file or by linking to an external CSS file.
@import url('https://fonts.googleapis.com/css2?family=Lato&display=swap'); * { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: 'Lato', sans-serif; height: 100vh; } .slider-container { position: relative; overflow: hidden; width: 100vw; height: 100vh; } .left-slide { height: 100%; width: 35%; position: absolute; top: 0; left: 0; transition: transform 0.5s ease-in-out; } .left-slide > div { height: 100%; width: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; color: #fff; } .left-slide h1 { font-size: 40px; margin-bottom: 10px; margin-top: -30px; } .right-slide { height: 100%; position: absolute; top: 0; left: 35%; width: 65%; transition: transform 0.5s ease-in-out; } .right-slide > div { background-repeat: no-repeat; background-size: cover; background-position: center center; height: 100%; width: 100%; } button { background-color: #fff; border: none; color: #aaa; cursor: pointer; font-size: 16px; padding: 15px; } button:hover { color: #222; } button:focus { outline: none; } .slider-container .action-buttons button { position: absolute; left: 35%; top: 50%; z-index: 100; } .slider-container .action-buttons .down-button { transform: translateX(-100%); border-top-left-radius: 5px; border-bottom-left-radius: 5px; } .slider-container .action-buttons .up-button { transform: translateY(-100%); border-top-right-radius: 5px; border-bottom-right-radius: 5px; }
4. Copy and paste the following JavaScript code just before closing the <body> element. This JavaScript code enables the slider’s functionality.
const sliderContainer = document.querySelector('.slider-container') const slideRight = document.querySelector('.right-slide') const slideLeft = document.querySelector('.left-slide') const upButton = document.querySelector('.up-button') const downButton = document.querySelector('.down-button') const slidesLength = slideRight.querySelectorAll('div').length let activeSlideIndex = 0 slideLeft.style.top = `-${(slidesLength - 1) * 100}vh` upButton.addEventListener('click', () => changeSlide('up')) downButton.addEventListener('click', () => changeSlide('down')) const changeSlide = (direction) => { const sliderHeight = sliderContainer.clientHeight if(direction === 'up') { activeSlideIndex++ if(activeSlideIndex > slidesLength - 1) { activeSlideIndex = 0 } } else if(direction === 'down') { activeSlideIndex-- if(activeSlideIndex < 0) { activeSlideIndex = slidesLength - 1 } } slideRight.style.transform = `translateY(-${activeSlideIndex * sliderHeight}px)` slideLeft.style.transform = `translateY(${activeSlideIndex * sliderHeight}px)` }
That’s it! You’ve successfully implemented a Vertical Image Slider on your website. By following this user guide and customizing the code to your needs, you can create an engaging and interactive slider to showcase your content effectively. 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.