Why does this image slider begin with blankness?

For my recent school project, I created an image slider. However, upon starting the page or reloading it, only the arrows and dots are displayed. View screenshot of the issue

Below is the code snippet:


// JavaScript function to control the slideshow
showSlides(slideIndex);

// Function for next/previous controls
function plusSlides(n) {
  showSlides(slideIndex += n);
}

// Function for thumbnail image controls
function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  // Code logic for displaying slides and dots
}

A detailed CSS snippet has also been included in the code.

If you could provide a quick solution as the project deadline nears, I would greatly appreciate it. Thanks!

https://i.sstatic.net/ILmeh.png

Answer №1

To ensure the correct functioning of the JavaScript code, remember to set the slideIndex variable like this:

var slideIndex = 1; showSlides(slideIndex);
. Additionally, if you are facing issues with the JavaScript not loading properly, consider moving the script tag to the end of the body section instead of placing it in the head.

Answer №2

Your code has some semantic issues that need to be addressed.

Upon reviewing your JavaScript code, I noticed the following problems:

showSlides(slideIndex); // --> Here, you are calling the showSlides function with slideIndex being undefined,
                        //     perhaps you are missing a declaration like "var slideIndex = 0" or something similar

function plusSlides(n) {
   showSlides(slideIndex += n);
}

function currentSlide(n) {
   showSlides(slideIndex = n);
}

function showSlides(n) {
   var i;
   var slides = document.getElementsByClassName("mySlides");
   var dots = document.getElementsByClassName("dot");
   if (n > slides.length) {
   slideIndex = 1 // --> At this point, slideIndex has not been declared yet, is it supposed to be global?
                  //     If not, you need to declare it at the top, maybe after "var i;" declaration.
   }
   if (n < 1) {
      slideIndex = slides.length
   }
   for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "block";
   }
   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";
}

Please fix these issues, test it out, and let me know how it goes.

Lastly, if you intend to load your JavaScript code within the head tag, make sure to wrap it in a function that executes only after the DOM has fully loaded, like this:

document.addEventListener("DOMContentLoaded", function(event) { 
    //your code
});

Alternatively, you can place your script tag right before the closing body tag.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue encountered when compiling Vue 3 with TypeScript and Webpack Encore: Vue Router's $route is missing

I've been grappling with setting up vue-router's $route in my shims.vue.d.ts file to avoid getting an error on the this scope. Whenever I try to access this.$route.params.paymentId, I keep receiving a type error: TS2339: Property '$route&apo ...

Devising a method to display configurable products as related items along with their customizable options in Magento

I am in the process of creating an e-commerce website using Magento for a client. The issue I am facing is that when I include a configurable product as a related item, it displays the product but without a checkbox. From what I have researched, Magento do ...

Displaying Edit and Delete options upon hovering over the data row

I'm working on a table that uses ng-repeat on the <tr> element. In the last column of each row, I have edit/delete links that should only be visible when the user hovers over the <tr> element. <tr ng-repeat="form in allData | filter:se ...

Steps for setting a background image for a div

I am facing an issue with 3 horizontally aligned images and a background image. The problem is that when I set the background image for the aligned images, it flows over them instead of staying behind. I want the 3 aligned images to be on top of the backgr ...

JavaScript button is not functioning properly to increase or decrease the value in the input field

I'm facing an issue with the javascript increase/decrease buttons on my website. When I assign my JS variable as the class name of the input field, pressing the button results in all input fields being affected simultaneously: https://i.stack.imgur.c ...

Perform a JSON query using JavaScript

Is there a way to query JSON using JavaScript? In my JavaScript code, I have a JSON stored in a variable called 'json'. This JSON is an array of data, each containing 3 attributes: 'name', 'city', and 'age'. I am in ...

Tips for detecting HTML updates using Python requests

My goal is to monitor a page for updates while maintaining the same session and cookies, without sending a completely new request. How can I determine if there are HTML updates within my current request? The page will redirect but keep the same URL. This ...

Develop a series of sequential tests for the playwright to execute

Can someone assist me with my code? I am attempting to write a test in Playwright that navigates to the forgot password page, creates a new password, and then tries to log in using that new password. However, I am encountering an issue with retrieving the ...

What is the best way to dynamically adjust the height of an iframe based on its changing content

My webpage contains an iframe that loads content dynamically, and I want to center it on the page based on its height. The issue I'm facing is that while my code works well for increasing content heights, it fails to recognize when the content size de ...

How to effectively manage errors in WCF using JavaScript?

Does anyone have instructions on using callback functions in a WCF Service that is accessible to Javascript? I am particularly interested in retrieving information from the FailureCallback to understand why my method is not working as expected. To clarify ...

Fetch solely the metadata from HTML5 video and audio files

Before we dive in, let me address a question I have regarding video metadata loading without any additional video content. The preload = "metadata" attribute doesn't seem to be functioning as expected. My testing thus far has been limited to Win Chrom ...

What is the best way to center a div vertically inside another div?

Aligning a div element horizontally within another div element is possible by using the margin: 0 auto; property as long as both elements have a specified width other than auto. However, this method does not work for vertical alignment. Is there a way to ...

Can you place 4 table cells in the first row and 3 table cells in the second row?

Exploring the world of HTML and CSS designs for the first time. Here's a code snippet I've been working on: <html> <body> <table width="100%"> <tr> <td width="25%">&#160;</td> ...

Implementing PHP echo alerts using Javascript

My current task involves validating the IP address entered in a textbox using PHP. $('#check_ip').click(function() { var iptext = $('#Ip_Txt').val(); $.ajax({ type : "POST", url : "mypage.php", data : { iptext : ip ...

Display the hidden div element when clicked

In my code, I have two div elements as follows: <div class='staticMap'></div> <div class='geolocation-common-map'></div> Initially, the 'geolocation-common-map' div is removed using jQuery when the pa ...

Is it possible for ng-model to verify its own value?

Recently, I've been experimenting with Angular and found myself facing a dilemma. Is it possible to apply a different CSS style based on whether the value of ng-model is greater than 0? `<span ng-model="users2" ng-hide="!myVar" ng-class="{'te ...

Finding the index of a nested div element with a click event using jQuery

I'm currently working on a click event to retrieve the index of the outermost parent div, but I'm facing some difficulties in getting it to work. Here is a snippet showcasing a list of several divs: <div class="owl-item"> <div class= ...

Embedding SVG styling directly into the HTML document

When importing svg images with color into Mapbox Studio, they appear as black and white. The troubleshooting website mentions: If your SVG appears black after adding to Mapbox Studio, it may be due to using style properties in tags instead of inline sty ...

Leveraging the useContext and useReducer hooks within NextJS, while encountering the scenario of reading null

Trying to develop a tic-tac-toe game in NextJS and setting up a board context for my components to access. However, after integrating a reducer into the Wrapper to handle a more complex state, I'm encountering a null error. Errors: TypeError: Cannot r ...

A step-by-step guide to building an API page with Python

Hello everyone! I am just starting out with Vue.js and Python, and I have a question for you. My task is to create a basic webpage using Vue.js on the front end and Python on the backend, with Heidi SQL as my database. My team has requested that I create ...