HTML
<div role="group" class="swiper-slide w-dyn-item" aria-label="1 / 13"
<div role="group" class="swiper-slide w-dyn-item" aria-label="2 / 13"
<div role="group" class="swiper-slide w-dyn-item swiper-slide-prev" aria-label="3 / 13"
<div role="group" class="swiper-slide w-dyn-item swiper-slide-active" aria-label="4 / 13"
<div role="group" class="swiper-slide w-dyn-item swiper-slide-next" aria-label="5 / 13"
<div role="group" class="swiper-slide w-dyn-item" aria-label="6 / 13"
Here I have a JS script working to get the number of the active slide in result of a variable. to get it, I've substring info from "aria-label" (keeping only slide number before " / 13") of the active slide:
var activeslide = $('.swiper-slide-active').attr('aria-label');
activeslide = activeslide.substring(0, activeslide.indexOf(" "))
var numactiveslide = parseInt(activeslide);
numactiveslide = numactiveslide - 1;
players[(numactiveslide)].play();
result // numactiveslide: "4"
**How to get the same for not 1 but 3 special slides (prev,active,next). I try to get the same, working for a new array of 3 actives slides (swiper-slide-prev, swiper-slide-active and swiper-slide-next).
This is the var of the selected slides:
var **activeslides** = $('.swiper-slide-prev,.swiper-slide-active,.swiper-slide-next');
Expected result is the 3 actives slides numbers: // numactiveslides = 3,4,5
How to get a code working by writing a loop or forEach or this kind of thing? This should be easy but I didn't manage to repeat the substring for each element, to get at the end an array of :
numactivesslides (3):
numactivesslides[0] = 3
numactivesslides[1] = 4
numactivesslides[2] = 5
CodePudding user response:
ok, I've finally found the answer. It works fine.
var activeslides;
var activslidesnumber = [];
var activplyrs = [];
var activslidlength;
setTimeout(function(){
activeslides = $('.swiper-slide-prev,.swiper-slide-active,.swiper-slide-next');
console.log("actives slides:",activeslides);
activslidlength = activeslides.length;
console.log('activslidlength ',activslidlength);
var arialabel;
for (let n = 0; n < activslidlength; n ) {
//get numbers
arialabel = activeslides[n].getAttribute('aria-label');
//update to keep only first num before space
arialabel = arialabel.substring(0, arialabel.indexOf(" "));
arialabel = parseInt(arialabel);
// put each arialabels on this var list
activslidesnumber.push(arialabel);
// fit fist slide to first player
activplyrs.push(arialabel - 1);
};
console.log("actives slides num:",activslidesnumber);
console.log('activplyrs ',activplyrs)
// THEN IF NEEDED, PLAY PLYR ON ACTIVE SLIDES
if (activplyrs[2] == undefined) {
covplayers[(activplyrs[0])].play();
covplayers[(activplyrs[1])].play();
} else {
covplayers[(activplyrs[0])].play();
covplayers[(activplyrs[1])].play();
covplayers[(activplyrs[2])].play();
}, 200);
//then do all the same for passives slides with covplayers.pause();