Agent67
Honorary Master
So, my slideshow is bugging out. Basically, the first image of the slideshow shows on startup (as it should), but when I click the buttons to operate the slideshow, it basically spawns the other images in the slideshow below the initial image (in sequence, so if the second image is visible and I click the next button, then the third slide spawns, etc), and after everything has basically spawned, they just trade places on the page to become the world's worst slideshow... (This is the most difficult part of the assignment, funnily enough, JSON and AJAX should go way easier than this...) I've tried messing around with the CSS and adapt the script here and there, but I'm no closer to the solution...
Code:
CSS:
Code:
JavaScript:
// the slideshow element
let slideShowDiv = document.getElementById("slideshow");
/* Slideshow */
if (fileName[0] === "index.html")
{
let images = ["images/hero.jpg", "images/staff.jpg", "images/clamps_create.jpg"];
let imgCount = images.length;
document.addEventListener("load", createSlideShow());
function createSlideShow()
{
// create the parts of the slideshow
let previousSlideButton = document.createElement("div");
let nextSlideButton = document.createElement("div");
let slideImages = document.createElement("div");
previousSlideButton.id = "previous_slide_button";
nextSlideButton.id = "next_slide_button";
slideImages.id = "slide_images";
previousSlideButton.innerHTML = "◀";
nextSlideButton.innerHTML = "▶";
let currentImg = 1;
slideShowDiv.appendChild(previousSlideButton);
slideShowDiv.appendChild(nextSlideButton);
slideShowDiv.appendChild(slideImages);
const createOverlay = (image) => {
let overlay = document.createElement("div");
overlay.id = "overlay";
let overlayImgHolder = document.createElement("img");
overlay.appendChild(overlayImgHolder);
overlayImgHolder.appendChild(image);
let closeButtonHolder = document.createElement("div");
closeButtonHolder.id = "close_overlay";
closeButtonHolder.innerHTML = "×";
closeButtonHolder.onclick = () => {
document.body.removeChild(overlay);
};
overlay.appendChild(closeButtonHolder);
document.body.appendChild(overlay);
};
// add the images to the slide_images element
for (let i = 0; i < imgCount; i++)
{
let image = document.createElement("img");
image.src = images[i];
image.style.display = "none";
image.onclick = createOverlay(image);
slideImages.appendChild(image);
}
slideImages.childNodes[currentImg - 1].style.display = "block";
const showNextSlide = () => {
console.log(`counter: ${currentImg}`);
slideImages.appendChild(slideImages.firstElementChild);
(currentImg < imgCount) ? currentImg++ : currentImg = 1;
};
const showPreviousSlide = () => {
console.log(`counter: ${currentImg}`);
slideImages.insertBefore(slideImages.lastElementChild, slideImages.firstElementChild);
(currentImg > 1) ? currentImg-- : currentImg = imgCount;
};
previousSlideButton.onclick = showPreviousSlide;
nextSlideButton.onclick = showNextSlide;
}
}
CSS:
JavaScript:
/* Desktop: Slideshow */
#slideshow {
max-width: 100%;
position: relative;
margin-bottom: 5em;
top: 0;
}
#slide_images > img{
height: min-content;
}
#previous_slide_button, #next_slide_button {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
margin-top: -22px;
padding: 16px;
color: black;
font-weight: bold;
font-size: 18px;
z-index: 1;
}
#next_slide_button {
right: 0;
}
#overlay {
display: none;
}


