Insert Carousel slider in HYPERTEXT MARKUP LANGUAGE.pptx

MarioCaday2 11 views 18 slides Oct 09, 2024
Slide 1
Slide 1 of 18
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18

About This Presentation

Insert Carousel slider in HMTL


Slide Content

Insert Carousel slider using Javascript

Explanation let currentIndex = 0; This line declares a variable currentIndex and initializes it with a value of 0. It keeps track of the currently displayed slide (starting with the first slide, which has an index of 0).

function showSlide (index) { This defines a function named showSlide that takes a parameter index. This parameter represents the slide we want to display.

const slides = document.querySelectorAll ('.carousel-item'); This line selects all elements with the class carousel-item (which are the individual slides) and stores them in a constant slides. The method querySelectorAll returns a list (called a NodeList ) of all matching elements.

if (index >= slides.length ) { This condition checks if the passed index is greater than or equal to the total number of slides ( slides.length ). This happens when the user tries to go beyond the last slide.

currentIndex = 0; If the index is greater than or equal to the number of slides, the currentIndex is reset to 0. This effectively loops back to the first slide.

} else if (index < 0) { This condition checks if the passed index is less than 0. This happens when the user tries to navigate before the first slide.

currentIndex = slides.length - 1; If the index is less than 0, this sets currentIndex to the last slide ( slides.length - 1). This loops back to the last slide if the user navigates back past the first one.

} else { currentIndex = index; } If neither of the previous conditions are met (i.e., the index is valid and within bounds), the currentIndex is simply set to the passed index.

const carouselInner = document.querySelector ('.carousel-inner'); This line selects the carousel-inner element, which contains all the slides, and stores it in the variable carouselInner . This is the container that will be shifted left or right to display different slides.

const offset = - currentIndex * 100; This line calculates the offset that is needed to shift the slides horizontally to show the correct one. Each slide takes up 100% of the width, so multiplying currentIndex by 100 gives the amount to shift. The negative sign (-) ensures that the slides move left when advancing to the next one.

carouselInner.style.transform = \ translateX (${offset}%)`;` This applies the calculated offset as a transformation to the carousel-inner element. The translateX CSS function moves the container left or right by the specified percentage. For example, if offset is -200%, the container is shifted 200% to the left, showing the third slide.

function nextSlide () { This defines a function called nextSlide that will be called when the user clicks the "Next" button. It moves to the next slide.

showSlide ( currentIndex + 1); Inside nextSlide , the showSlide function is called with currentIndex + 1, which increases the currentIndex by 1 and moves to the next slide. If the current slide is the last one, this function will loop back to the first slide (due to the logic in showSlide ).

function prevSlide () { This defines another function called prevSlide that is called when the user clicks the "Previous" button. It moves to the previous slide.

showSlide ( currentIndex - 1); Inside prevSlide , the showSlide function is called with currentIndex - 1, which decreases the currentIndex by 1 and moves to the previous slide. If the current slide is the first one, it will loop back to the last slide.

END
Tags