The JavaScript array slideshow is experiencing some glitches in its transition

After diving into JavaScript recently, I managed to center a div and make it fullscreen as the window resizes. However, things got tricky when I added a script I found online to create an image transition using an array. Unfortunately, these scripts are conflicting and causing issues with the animation, especially when resizing the window. Check out my jsfiddle link below to see what I mean. Any help would be greatly appreciated.

http://jsfiddle.net/xPZ3W/

function getWidth() {
var w = window.innerWidth;
x = document.getElementById("wrapper");
x.style.transition = "0s linear 0s";
x.style.width= w +"px"; 
}

function moveHorizontal() {
var w = window.innerWidth;
x = document.getElementById("wss");
x.style.transition = "0s linear 0s";
x.style.left= w / 2 -720 +"px" ;         
}

function moveVertical() {
var h = window.innerHeight;
x = document.getElementById("wss");
x.style.transition = "0s linear 0s";
x.style.top= h / 2 -450 +"px" ;         
}

var i = 0; 
var wss_array = ['http://cdn.shopify.com/s/files/1/0259/8515/t/14/assets/slideshow_3.jpg?48482','http://cdn.shopify.com/s/files/1/0259/8515/t/14/assets/slideshow_5.jpg?48482'];   
 var wss_elem;

function wssNext(){

  i++;                                 
  wss_elem.style.opacity = 0; 

if(i > (wss_array.length - 1)){           
i = 0;                           
 }
 setTimeout('wssSlide()',1000);
}


function wssSlide(){
 wss_elem = document.getElementById("wss")
 wss_elem.innerHTML = '<img src="'+wss_array[i]+'">';       
 wss.style.transition = "0.5s linear 0s";

 wss_elem.style.opacity = 1;

setTimeout('wssNext()',3000); 
}

Answer №1

After creating this unique JSFiddle from scratch, I believe it will be a helpful tool for you. It showcases pure CSS transitions between classes using an array of URLs to smoothly switch among different pictures.

The code essentially progresses the "active" class to the next one each time it is called, assuming the first picture is initially set as the "active" class.

var pics = document.getElementById('slideshow').children,
    active = 0;

function slideshow() {
    for (var i = 0; i < pics.length; i++) {
        if (i == active && pics[i].className == "active") {
            console.log(i, active, (active + 1) % pics.length);
            active = (active + 1) % pics.length;
        }
        pics[i].className = "";
    }
    pics[active].className = "active";
    setTimeout(slideshow, 2000);
}
setTimeout(slideshow, 2000);

Additionally, here is the CSS that positions the container absolutely and only displays its children with the active class in a smooth transition effect.

#slideshow {
    position: absolute;
    top: 20%;
    bottom: 20%;
    left: 20%;
    right: 20%;
}
#slideshow img {
    position: absolute;
    max-height: 100%;
    max-width: 100%;
    opacity: 0;
    transition: opacity 1s linear;
}
#slideshow .active {
    opacity: 1;
}

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

Sequential invocations to console.log yield varying outcomes

So, I'm feeling a bit perplexed by this situation. (and maybe I'm missing something obvious but...) I have 2 consecutive calls to console.log. There is nothing else between them console.log($state); console.log($state.current); and here's ...

Manipulating visibility of an input tag using JQuery

Having a simple input tag: <input id="DAhour" type="number" style="width:50px; font-size: xx-small; visibility:hidden"> Initially, the input tag is set to be hidden. Upon changing a combobox to a specific index, it should become visible. Despite su ...

formula for an arbitrary velocity vector

In the game I'm developing, I want the ball to move in a random direction on the HTML canvas when it starts, but always with the same velocity. The current code I have is: vx = Math.floor(Math.random() * 20) vy = Math.floor(Math.random() * 20) Howev ...

What purpose does the class serve in typescript?

This is a unique version of app.component.ts in the Angular Tour of Hero tutorial. import { Component } from '@angular/core'; export class Superhero{ name : string; id : number; } const SUPERHEROES : Superhero[] = [ {name : 'Wonder ...

What is the best way to update a page and retrieve fresh data from a service in Angular?

On my webpage, there is a table and two charts coming from three different controllers. I am looking for a way to refresh all of them simultaneously by clicking on a single refresh button. This will allow the data to be fetched from the webservice again an ...

Stop bullet points from overlapping when using a floating left image in Internet Explorer 9 and above

Having trouble with bullets displaying in IE9+? Check out this link for more information: I've attempted to fix the issue by creating an ie.css file using conditional comments, but it hasn't worked. Can anyone provide assistance? ...

Passing both the event and a local Vue variable to a bound function in a idiomatic manner

I had been grappling with the problem of accessing both a v-for member and the original event data within a bound function on an event in vue.js. Despite my efforts, I couldn't seem to find a solution in the documentation. Here is what I was aiming f ...

Strategies for aligning a div element in the middle of the screen

Positioned in the center of the current viewport, above all other elements. Compatible across different browsers without relying on third-party plugins, etc. Can be achieved using CSS or JavaScript UPDATE: I attempted to use Javascript's Sys.UI.Dom ...

Verification of custom data type validation

I am puzzled by the behavior of this custom type state: interface DataType { [key: string]: string;} const [data, setData] = React.useState<DataType>({}); When I attempt to execute console.log(data === {}) It surprisingly returns false. Why ...

Execute a script once the AngularJS view has finished loading

Just starting out with AngularJS and I've encountered an issue; I'm populating part of my view with HTML from a variable in my controller: <div ng-bind-html="deliberatelyTrustDangerousSnippet()"></div> Everything works as expected ...

Create a dynamic animation effect by making a div slide on and off the screen

I'm having trouble figuring out how to make my div slide with a toggle button. I attempted to use variables, but since I have multiple instances of this, it's becoming too complicated to keep track of all the clicks on each one. $(document).r ...

Enhance visual content using JavaScript to filter images in React

I am currently developing a React app with multiple pages that are being imported into another component page. The structure of one of my pages looks like this: export default class Product1 extends React.Component { render() { return ( <di ...

To avoid any sudden movements on the page when adding elements to the DOM using jQuery, is there a way to prevent the page from

I have a challenge where I am moving a DIV from a hidden iFrame to the top of a page using jQuery. Here is my current code: $(document).ready(function($) { $('#precontainer').clone().insertBefore(parent.document.querySelectorAll(".maincontainer" ...

Struggling to retrieve information using the filter() method in MongoDB

I am currently attempting to retrieve the tasks assigned to a specific user using this setup router.get('/all', auth, async (req, res) => { try { const assignments_raw = await Assignment.find({listP: listP.indexOf(req.user.userId)}) ...

Why won't JQuery .ajax send my request?

I'm having trouble sending POST/Get requests using jQuery with my Python Flask server. Curl requests are working fine, and I can see regular requests hitting the server, but these ajax requests aren't making it through. Here is the code I've ...

Converting a tree structure from GraphQL to JSON format using JavaScript

I'm attempting to convert my client-side data from this structure: [{ id: 1, name: "dad", parent: [], children: [{ id: 2, name: "child1", parent: { parent: 1 } }, ...

Connect the mileage tracker to a Datalist or grid view component

I recently downloaded the Odometer Sample from , however, I am facing an issue where only the first element in the Datalist is getting the Odometer display, while others are not displaying it. Here is the snippet of code: <script type="text/javascript" ...

JavaScript - Utilizing appendChild as soon as an element becomes available

I'm encountering an issue with my Chrome Extension where I am unable to detect some of the elements that I need to select within a page. var innerChat = document.querySelector('.chat-list'); My goal is to appendChild to this element, but t ...

Swapping out the current div with the outcome from jQuery

I'm attempting to retrieve the most recent data for a div and replace it. The ajax query is fetching the entire HTML page, from which I am locating the right div. Now I want to update the current right div with the new content. $.ajax( { ...

The Node.js application successfully receives an emit event from the browser, however, the browser does not receive any emit

I'm having trouble understanding why the node.js server can connect and receive an emit() from the browser, but when I try to emit() back from node.js it doesn't reach the browser. Am I overlooking something here? The console log displays "Test ...