Creating a Carousel Feature 2
In this session, we'll create a currentSlide function using JavaScript to enable transitioning to a specific slide.
Implementing Transition to a Specific Slide
<span class="dot" onclick="currentSlide(1)"></span> <span class="dot" onclick="currentSlide(2)"></span> <span class="dot" onclick="currentSlide(3)"></span>
We have created slide indicators in addition to the previous/next buttons.
When you click on these dot indicators, the carousel should transition to a specific slide.
Clicking the first dot will take you to Slide 1, the second dot will take you to Slide 2, and the third dot will take you to Slide 3.
function currentSlide(n) { showSlides(slideIndex = n); }
The currentSlide function is implemented similarly to the previously created plusSlides function by calling the showSlides function.
However, when calling the showSlides function, the slideIndex value is set to n.
Therefore, if the value of n is 1, slideIndex will be 1, if it is 2, slideIndex will be 2, and if it is 3, slideIndex will be 3.
So, if slideIndex is originally 1, showing the first slide, calling currentSlide(3) will change slideIndex to 3, displaying the third slide.
Lessons in this chapter ยท Creating a Carousel
- 1. What is a Carousel?
- 2. The Role of a Carousel
- 3. Carousel Components
- 4. Creating Carousel Structure 1
- 5. Creating Carousel Structure 2
- 6. Creating Carousel Structure 3
- 7. Styling the Carousel Container
- 8. Styling the Carousel with CSS
- 9. Slide Animation
- 10. Carousel Button Styling 1
- 11. Carousel Button Styling 2
- 12. Creating a Carousel Function 1
- 13. Creating a Carousel Feature 2
- 14. Creating Carousel Function 3
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help