The image slideshow on W3 Schools displays images in a stacked formation

Utilizing this code snippet from W3 Schools to incorporate an image slideshow into my web project. However, upon page load/reload, the images appear stacked vertically rather than horizontally (one above and one below). The slideshow functions properly after clicking the navigation arrows, but I would prefer it to display correctly from the start. Minimal modifications were made to the HTML and CSS (added centering), with minor adjustments in the JavaScript based on suggestions.

P.S. In the provided code, square images saved locally were replaced with random photos from Pexels for visual demonstration (x, x). Interestingly, the issue of stacking images did not occur here...

var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
    showDivs(slideIndex += n);
}

function showDivs(n) {
    var x = document.getElementsByClassName("mySlides");
    if (n > x.length) {slideIndex = 1}
    if (n < 1) {slideIndex = x.length}
    for (var i = 0; i < x.length; i++) {
    x[i].style.display = "none";  
    }
    x[slideIndex-1].style.display = "block";  
}
.mySlides {
    display: block;
    margin-left: auto;
    margin-right: auto;
}

.w3-button {
    display: block;
    margin-left: 210px;
    margin-right: 210px;
}
<head>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>

<div class="w3-content w3-display-container">
            <img class="mySlides" src="https://images.pexels.com/photos/965879/pexels-photo-965879.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2" style="width:300px">
            <img class="mySlides" src="https://images.pexels.com/photos/570047/pexels-photo-570047.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2" style="width:300px">        
            <button class="w3-button w3-brown w3-display-left" onclick="plusDivs(-1)">&#10094;</button>
            <button class="w3-button w3-brown w3-display-right" onclick="plusDivs(1)">&#10095;</button>

Answer №1

When you insert the <script> tag at the beginning of the <body> or in the <head>, it can cause issues:

<body>

    <script src="javascript.js"></script>

    <div class="w3-content w3-display-container">
            <img class="mySlides" src="https://images.pexels.com/photos/965879/pexels-photo-965879.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2" style="width:300px">
            <img class="mySlides" src="https://images.pexels.com/photos/570047/pexels-photo-570047.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2" style="width:300px">        
            <button class="w3-button w3-brown w3-display-left" onclick="plusDivs(-1)">&#10094;</button>
            <button class="w3-button w3-brown w3-display-right" onclick="plusDivs(1)">&#10095;</button>
    </div>

  </body>

To avoid problems, it's recommended to place the <script> tag at the end of the <body>:

<body>

    <div class="w3-content w3-display-container">
            <img class="mySlides" src="https://images.pexels.com/photos/965879/pexels-photo-965879.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2" style="width:300px">
            <img class="mySlides" src="https://images.pexels.com/photos/570047/pexels-photo-570047.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2" style="width:300px">        
            <button class="w3-button w3-brown w3-display-left" onclick="plusDivs(-1)">&#10094;</button>
            <button class="w3-button w3-brown w3-display-right" onclick="plusDivs(1)">&#10095;</button>
    </div>

    <script src="javascript.js"></script>

  </body>

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

Using Conditional Rendering and ReactCSSTransitionGroup for Dynamic Animations

I have developed a small application that displays different components based on a Redux state. I am trying to add a "fade" animation when one of the components renders, but unfortunately, it is not working as expected. Here is my current setup: content.j ...

What is the best method for decreasing the space between ordered list elements?

Help with removing unwanted top margin space I have been experiencing an issue where I am getting extra space above the list items when using a ul before an ol. The result is that only the unwanted space appears above the circle and number 1. I attempted ...

Using pre-built modules in an environment that does not support Node.js

Despite the limitations on node API usage (such as fs, http, net...), vanilla JS can still be used on any engine. While simple functionalities can be easily extracted from packaged modules if licensing terms are met, things get complicated when dealing wit ...

Identifying tick markers in the event loop of JavaScript and Node.js

Is there a way to determine if a function is called in the current tick or if it will be called in the next tick of Node.js event loop? For instance: function exampleFunction(callback){ // we can call callback synchronously callback(); // or we c ...

Using Bootstrap 4, you can create nested rows that will automatically expand to fill their parent container, which in turn has been set

In my current setup, I have a div with the class "d-flex flex-column" that contains a navbar and a container. Within this container, there is another div with the class "d-flex flex-column" which then contains two rows. I am using flex-grow to make the con ...

Vue.JS encounter a hiccup while attempting to store data in the local storage

My application is set up to fetch data from a rest api, and it consists of two key components: 1 - (main) Display the results 2 - (list) To display a list of selected items from the main results The feature I am trying to implement involves adding a save ...

The connection to MongoDB is failing due to an incorrect URI

I tried setting up mongoDB on my node server and referred to the official MongoDB documentation. Here are the details of my setup: MongoDB version: 4.4.3 Node.js version: v15.7.0 I copied the starter code from MongoDB and here is what I used: const { Mon ...

Changes in browser size will not affect the height

Is there a way to smoothly change the height of the navigation bar when resizing the browser? Currently, it snaps to the new height when the width goes below or above 1000px. Any suggestions? nav.cust-navbar { -webkit-transition: height 2s; tran ...

Change wiki text into HTML using Python for showcasing on a webpage

Despite trying various tools for 6 hours, I have been unable to find one that can properly interpret wikitext like the example below: '<center>Welcome to the world's foremost open content<br><big><big><big>'&ap ...

I'm having trouble getting the aggregation of a child collection to work properly in MongoDB when attempting to apply the $count

Could someone help me troubleshoot my aggregate query? I'm trying to sum the count values for each beacon, but it keeps returning 0. Please let me know if you spot any mistakes in the query. Sample Data [ { ...

What is the most efficient method for transferring Flask variables to Vue?

I am currently developing a visualization application using a flask server and vue.js for the front end. Other discussions on this topic explore how to avoid conflicts between vue.js and flask variable syntax, as shown here. In my scenario, I'm inte ...

Utilize a class method within the .map function in ReactJS

In my ReactJS file below: import React, { Component } from "react"; import Topic from "./Topic"; import $ from "jquery"; import { library } from '@fortawesome/fontawesome-svg-core' import { FontAwesomeIcon } from '@fortawesome/react-fontaw ...

Managing the initial record in mysql

As I work on a slideshow, my goal is to dynamically change the HTML class attribute from "item" to "item active" when the first record is fetched from the database. For all subsequent records, the class should revert back to just "item." $RelatedTo=1; $ ...

Using canvas transformation changes the way drawImage is applied

I have been working on a game as a hobby and you can find it at . I have managed to get most aspects of the game working well, such as transformation, selection, movement, and objects. However, there is one particular challenge that I am struggling with. ...

How can I exclude specific lines from an XML file using filters?

Let's say I have the following data: <div class="info"><p><b>Orange</b>, <b>One</b>, ... <div class="info"><p><b>Blue</b>, <b>Two</b>, ... <div class="info"><p><b& ...

The jQuery document.ready event fails to trigger when invoked after using ScriptManager.RegisterStartupScript in the code-behind

I am currently working with a filtered list of items utilizing a tool called Check out the screen for a visual example. In the user flow, after selecting to add another action, a fancybox popup is triggered displaying the next image: After the user adds ...

Tips for enabling custom object properties in Chrome DevTools

In my typescript class, I am utilizing a Proxy to intercept and dispatch on get and set operations. The functionality is working smoothly and I have successfully enabled auto-completion in vscode for these properties. However, when I switch to the chrome d ...

Using JavaScript to convert a UTC Date() object to the local timezone

I am working with a Date() object that holds a UTC date. I need to convert it to the local timezone of the user. Any suggestions on how I can achieve this? Let me know! :-) ...

The React component fails to render on a Razor page

Looking to render a React component within a Razor page but without using a div? You can achieve this by utilizing ReactDOM.render, however my goal is to utilize it as a tag within the Razor page itself. For example, if I have a class named App, I would li ...

Specify the controller to be used dynamically in an Angular directive

I want to be able to specify the controller that a directive uses by adding an attribute to the element - in other words, dynamically: HTML <div data-mydirective data-ctrl="DynamicController"></div> Angular angular.module('app', [ ...