Started this thread yesterday, marked as solved! Thx.
Want to add some functionality to the BTNs below the IMGs: Clicking a BTN should a) do everything the solution already does and b) change the BTNs .text("some_text") and addClass ("some_css") to the BTN. Re-clicking the same button should reset itself. Clicking another BTN needs to reset everything as well. The idea below works, but I feel it should be solved way more elegantly.
$(document).ready(function() {
$('.btn').on("click", function() {
if ($(this).hasClass("playing")) {
const btnIndex = $(this).data("index"); // index of the clicked button
$('.img').each((_,img) => {
const $img = $(img);
const imgIndex =$img.data("index");
const suffix = (btnIndex == imgIndex) ? "a" : "a"
$img.attr("src", `image-${imgIndex}-${suffix}.gif`);
});
// resets ALL BTNs text
$(".btn").text("Play Animation");
// resets ALL BTNs css
$(".btn").removeClass("btn_state_active");
// resets ALL BTNs incl. THIS one to "not-playing"..
$(".btn").removeClass("playing");
} else {
const btnIndex = $(this).data("index"); // index of the clicked button
$('.img').each((_,img) => {
const $img = $(img);
const imgIndex =$img.data("index");
const suffix = (btnIndex == imgIndex) ? "b" : "a"
$img.attr("src", `image-${imgIndex}-${suffix}.gif`);
});
// resets ALL BTNs text
$(".btn").text("Play Animation");
// resets ALL BTNs css
$(".btn").removeClass("btn_state_active");
// resets ALL BTNs incl. THIS one to "not-playing"..
$(".btn").removeClass("playing");
// updates THIS BTNs text
$(this).text("Stop Animation");
// updates THIS BTNs css
$(this).addClass("btn_state_active");
// changes/updates THIS BTNs status
$(this).addClass("playing");
}
});
});