Prepin
Log in

Build a carousel using plain JavaScript.

Approach / Explanation

This question requires you to describe your method for building a carousel solely with "vanilla" JavaScript. This implies that you should avoid using any external libraries or frameworks, including jQuery, React, or Angular, for this task. You will need to outline how you would design and organize the essential HTML, the CSS for styling, and the JavaScript to implement the carousel's functionality. Key aspects to focus on include managing user interactions, automatically rotating through images, and accommodating various screen sizes.

Suggested Answer

To build a carousel using vanilla JavaScript, the HTML structure will include a div element for the carousel, with each image placed inside its own div. All of these divs will share the same class, which we will refer to as 'carousel-image'. For illustration, let’s use three images: <div id="carousel"> <div class="carousel-image"> <img src="image1.jpg"> </div> <div class="carousel-image"> <img src="image2.jpg"> </div> <div class="carousel-image"> <img src="image3.jpg"> </div> </div> For the CSS styling, we will define the carousel div to have a specific width and height while hiding any overflow content. Each image will be absolutely positioned within the carousel div, and only the first image will be visible initially, while the others will be hidden: #carousel { width: 500px; height: 300px; position: relative; overflow: hidden; } .carousel-image { position: absolute; visibility: hidden; } .carousel-image:first-child { visibility: visible; } The JavaScript code will manage the transitions between the images. We will first select all the divs with the class 'carousel-image' and then set up an interval to cycle through the images, making the current image hidden and revealing the next one: const images = document.querySelectorAll('.carousel-image'); let currentImage = 0; setInterval(() => { images[currentImage].style.visibility = 'hidden'; currentImage = (currentImage + 1) % images.length; images[currentImage].style.visibility = 'visible'; }, 3000);

Alternative Answer

The carousel can alternatively be created using the opacity property instead of visibility. In this approach, we would execute cross-fading transitions between the images. Therefore, the HTML structure will remain identical to the first example, with only adjustments needed in the CSS and JavaScript code. #carousel { width: 500px; height: 300px; position: relative; } .carousel-image { position: absolute; opacity: 0; transition: opacity 1s; } .carousel-image:first-child { opacity: 1; } In JavaScript, rather than altering the visibility, we would adjust the opacity of the images. var images = document.querySelectorAll('.carousel-image'); var currentImage = 0; var carousel = setInterval(() => { images[currentImage].style.opacity = 0; currentImage = (currentImage + 1) % images.length; images[currentImage].style.opacity = 1; }, 3000); In this case as well, the opacity property serves as a dynamic method for showing and hiding images, resulting in smoother transitions between them.

Question Details

Difficulty & Category

Medium
technical

Companies

Amazon

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.