1
How to understand this Javascript?
zeno99 (0)

This JavaSript in HTML for image Slideshow, how can understand the code?

var slideIndex = 1;
showSlides(slideIndex);

// Next/previous controls
function plusSlides(n) {
showSlides(slideIndex += n);
}

// Thumbnail image controls
function currentSlide(n) {
showSlides(slideIndex = n);
}

function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("demo");
var captionText = document.getElementById("caption");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
captionText.innerHTML = dots[slideIndex-1].alt;
}

You are viewing a single comment. View All
3
Vandesm14 (483)

I have made comments on almost every line of code, explaining what each line does.

// Set slide # to 1
var slideIndex = 1;
// Show the current slide
showSlides(slideIndex);

// Next/previous controls
function plusSlides(n) {
	// Increase the slide index by 1 and show that slide
	showSlides(slideIndex += n);
}

// Thumbnail image controls
function currentSlide(n) {
	// Show the current slide
	showSlides(slideIndex = n);
}

// Toggle correct slides
function showSlides(n) {
	var i;

	// All of the slides in the document
	var slides = document.getElementsByClassName("mySlides");

	// All of the slide dots in the document
	var dots = document.getElementsByClassName("demo");

	// All of the slide captions in the document
	var captionText = document.getElementById("caption");

	// If slide # is greater than the total number of slides
	if (n > slides.length) {
		slideIndex = 1
	}

	// If slide # is less than total number of slides
	if (n < 1) {
		slideIndex = slides.length
	}

	// Hide each of the slides
	for (i = 0; i < slides.length; i++) {
		slides[i].style.display = "none";
	}

	// Replace each of the dots with the class "active" with ""
	for (i = 0; i < dots.length; i++) {
		dots[i].className = dots[i].className.replace(" active", "");
	}

	// Set the current slide to display as a block element
	slides[slideIndex - 1].style.display = "block";

	// Set the current slide's respective dot to the "active" class
	dots[slideIndex - 1].className += " active";

	// Set the caption text to the current slide's caption text
	captionText.innerHTML = dots[slideIndex - 1].alt;
}

Please mark this as answered if I helped your problem 👍

3
heyitsmarcus (285)

@Vandesm14 First off, great job! Upvoted!

The only one critique I have is that the following line is an incorrect explanation:

// All of the slide captions in the document
var captionText = document.getElementById("caption");

It should be:

// The element with an id of caption
var captionText = document.getElementById("caption");
1
zeno99 (0)

@Vandesm14 Thank you very much for your help

1
Vandesm14 (483)

@heyitsmarcus Oof. I forgot about that. Thanks.

1
heyitsmarcus (285)

@Vandesm14 Of course, but like I said, great job! I love seeing people helping out others!