Creating an uncomplicated selector bar for a basic javascript slideshow

I've spent the last couple of hours teaching myself some basic JavaScript, so please bear with me.

Currently, I have a simple code in place for a straightforward slideshow on a website. Here is the JavaScript I'm using (where the 'slide' class has an opacity of 0 and the 'current' class has an opacity of 1 to only show the 'current' slide):

<script>

var Slideshow = document.querySelectorAll('#slides .slide');
var Current = 0;
var Timer = setInterval(SlideMove,3000);

function SelectSlide(n) {
  SlideMove(Current = n);
}

function SlideMove(n) {
    Slideshow[Current].className = 'slide';
    Current = (Current+1)%Slideshow.length;
    Slideshow[Current].className = 'current slide';
}
</script>

Using CSS and HTML, I created 5 small squares positioned under the slideshow. Each square corresponds to one of the 5 photos in the slideshow. When clicked, I want the slideshow to move to that specific photo and continue the automatic loop from there.

Here is the HTML for the 5 squares (each square has the class 'selector'):

<div id="selectors">
  <span class="selector" onclick="SelectSlide(0)"></span> 
  <span class="selector" onclick="SelectSlide(1)"></span> 
  <span class="selector" onclick="SelectSlide(2)"></span> 
  <span class="selector" onclick="SelectSlide(3)"></span>
  <span class="selector" onclick="SelectSlide(4)"></span>
</div>

For the 'onclick' part, I thought I was instructing it to run a new JavaScript function I created called 'SelectSlide(n)', which looks like this:

function SelectSlide(n) {
  SlideMove(Current = n);
}

Based on what I learned from a tutorial, I believed this would take the slide number inputted (n) and execute the SlideMove function with 'Current' set as 'n'. My expectation was that it would simply change the 'Current' value to the selected one and start the slideshow loop afresh from that point. However, it's not working as expected. Clicking on one of the selector boxes displays the desired image but doesn't hide the previous one, resulting in a chaotic display. Additionally, the newly chosen image may suddenly vanish.

I suspect the issue lies in the algorithm for the automatic slide transition loop and the fact that I'm not hiding the previous slide properly, but I'm unsure how to resolve it. Any advice you can offer would be greatly appreciated.

If my question seems unclear or poorly articulated in any way, feel free to ask for clarification.

Answer №1

Utilize custom JavaScript code with the display:none property to toggle visibility on and off.

var Slideshow = document.querySelectorAll('#slides .slide');
var Current = 0;
var Timer = setInterval(SlideMove,3000);

function SelectSlide(n) {
  SlideMove(Current = n);
}

function SlideMove(n) {
    Slideshow[Current].className = 'slide';
    Current = (Current+1)%Slideshow.length;
    Slideshow[Current].className = 'current slide';
}
.slide{
  height:100px;
  display:none;
  background-size:100% 100%;
    background-size: 100% 100%;
    text-align: center;
    line-height: 0px;
    font-size: 0px;
    color: red;
}
.current{
  display:block;
}/*
.slide:nth-child(even){
  background:red;
}
.slide:nth-child(odd){
  background:green;
}*/
<div id="slides">
  <span class="slide current" onclick="SelectSlide(0)" style="background-image:url('https://placeholdit.imgix.net/~text?txtsize=77&bg=ff0000&txtclr=ffffff&txt=1230x373&w=1230&h=373');">0</span> 
  <span class="slide" onclick="SelectSlide(1)" style="background-image:url('https://placeholdit.imgix.net/~text?txtsize=77&bg=00ff00&txtclr=ffffff&txt=1230x373&w=1230&h=373');">1</span> 
  <span class="slide" onclick="SelectSlide(2)" style="background-image:url('https://placeholdit.imgix.net/~text?txtsize=77&bg=0000ff&txtclr=ffffff&txt=1230x373&w=1230&h=373');">2</span> 
  <span class="slide" onclick="SelectSlide(3)" style="background-image:url('https://placeholdit.imgix.net/~text?txtsize=77&bg=ffff00&txtclr=ffffff&txt=1230x373&w=1230&h=373');">3</span>
  <span class="slide" onclick="SelectSlide(4)" style="background-image:url('https://placeholdit.imgix.net/~text?txtsize=77&bg=000000&txtclr=ffffff&txt=1230x373&w=1230&h=373');">4</span>
</div>

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

Can you tell me the method of checking the number of members in a voice channel?

Is there a method to determine the number of members in a voice channel or check if it's empty using discord.js (V. 13.8.1)? I attempted the following code: async function countMembers(voiceState){ let users = await voiceState.channel.members.siz ...

JavaScript and CSS animations out of sync in terms of timing

I've been encountering some issues. I have an array containing 5 lines of text that change with a timer, but it seems that the css timer animation might not be synced properly or my @keyframes slidesdown setup could be incorrect. The delay between te ...

The data for my registration is not getting stored in the Firebase database as expected

I'm currently working on a registration app and it's my first time using firebase. My email address is successfully stored in the authentication section of firebase. However, when I try to connect my app to the database to store additional infor ...

Is there a way to identify if you are at the bottom row of a column defined by CSS styling?

I have implemented the new CSS-style column to divide a single unordered list into multiple columns. Now, I am trying to figure out how to target the last element in each column using JavaScript or CSS. Here is my HTML: <ul> <li /> <li ...

Animating with CSS3 triggered by JavaScript

Can you help me with an issue I'm having? When attempting to rotate the blue square using a function, it only works once. After that, the page needs to be reloaded in order for the rotation function to work again. Additionally, after rotating 120 degr ...

Searching across multiple attributes in Jquery UI Autocomplete: A comprehensive guide

My data is stored in an array of objects with a structure similar to this: $scope.usersList = [{ "name": "John", "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f09a9f989eb088898ade939f9d">[email protected ...

The render function is not being executed due to a disruption in the code flow

Within the given code snippet, there is a peculiar issue with the render function being called inside the loop below. Strangely, the console.log statement before the function call executes successfully, but the one directly inside the function does not s ...

Sharing data between controllers within an MVC architecture in JavaScript

I'm currently working on an app using Express and Node. Within my routes, I have '/new-poll' and '/poll-create' //The route poll-create allows users to create a new poll app.route('/poll-create') .get(functi ...

Preventing the addition of hash tags to the URL by the anything slider

Why does the Anything Slider add hash tags like #&panel1-1 to the end of the URL? I attempted using hashtags:false but it was unsuccessful. Are there alternative methods to prevent it from creating these hashtags? ...

Accessing JSON data model from Ember route or controller

Exploring the capabilities of Ember.js Is it possible to expose my data model as JSON through a route or controller? The object saved in the store looks like this: this.store.createRecord('Person', { id: 1, name: this.get('name'), ...

A Study on Particle Systems using THREE.js

While working on my particle System in THREE.js with SPARK.js, I have completed the necessary code for the system. However, I am facing an issue where nothing related to the Particle System is being displayed on the screen. Currently, I am trying to creat ...

Issue with triggering a modal while simultaneously closing another one

Let's consider this situation. I currently have a modal called Modal A, which has 2 buttons in the footer: Save and Close. When I click on the "Save" button, my intention is to close Modal A and open Modal B. The code that achieves this functionality ...

Can you provide a way to directly calculate the total length of all arrays within an array of objects?

How can I find the total length of arrays in an array of objects based on a specific property? var myArray = [{ "a" : 1, "b" : another Array }, { "c" : 2, "b" : another Array } ..... ] Is there a more efficient way to achieve this instea ...

Asynchronous Return in NodeJS Class Methods

Currently, I am in the process of developing a JavaScript class that includes a login method. Here is an overview of my code: const EventEmitter = require('events'); const util = require('util'); const Settings = require('./config ...

Decoding various JSON arrays received through AJAX requests

After returning two objects as JSON through AJAX, I am facing an issue with accessing the values in these two lists. Previously, when I had only one list, I could parse it easily. data = serialize("json", vm_obj) data2 = serialize("json", user_networks_li ...

Unusual host value being returned by next/headers in Next.js version 13

In my current Next.js project, I am utilizing next/headers to dynamically set a baseUrl for calls to my API. const baseUrl = () => { const protocol = process?.env.NODE_ENV === "development" ? "http" : "https"; const ...

Message displayed within Ng-repeat loop

Having trouble implementing a tooltip within an ng-repeat for each item in a td element. Whenever the mouse hovers over an item inside the td, I want a tooltip to display with more details. The code below shows my setup, using ng-if to prevent displaying e ...

Creating a stylish design: integrating a progress bar within a card using Bootstrap 5

I'm currently delving into the world of bootstrap and I'm curious about how to incorporate a progress bar into a card layout to enhance my design skills. I experimented with inline-block and inline display properties, but they didn't yield t ...

Increasing the identifier of duplicated input fields using jQuery

There is a specific section in my form that consists of a select box and three checkboxes, which needs to be duplicated on request. While I have successfully cloned the div and incremented its specific div, I am facing challenges in incrementing the checkb ...

steps for including preset text in an input field within a form on WordPress

I am currently utilizing a WordPress plugin that automatically generates a form in the admin area based on user input. The form includes five fields that require URL validation. Users have the option to enter optional link text which will be displayed as t ...