• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Loop through items and after set interval remove class from all whilst adding class to next item

Resolved: Loop through items and after set interval remove class from all whilst adding class to next item

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I have built a simple carousel which fades in/out of various images. The visual changes happen in CSS when JS changes the class.
This worked perfectly with two slides. Now I’ve introduced a third it does not and I can now see why it worked with 2 and doesn’t with 3 or more.
Essentially what I’d like to achieve is this:
Every 7 seconds the script removes all instances of slide--active and adds it to the next item in the loop.
I’m using Jquery throughout the site, so it makes sense to utilise it. The CSS below can be ignored, I’m only interested in the class change.


Answer:

You can do it like this:
This will remove the active class from the current element, and set the active class on the next element (or first element if current element is last)
Demo


If you have better answer, please add a comment about this, thank you!

javascript jquery
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How can I copy files using the ansible.builtin.copy module and avoid conflicting file names?

26/03/2023

Resolved: Reshape tensors of unknown shape with tf.function

26/03/2023

Resolved: Use Svelte Component as Slot

26/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.