Splidejs / splide

Splide is a lightweight, flexible and accessible slider/carousel written in TypeScript. No dependencies, no Lighthouse errors.
https://splidejs.com
MIT License
4.83k stars 418 forks source link

How to get data attribute of current slide, moved pagination does not work #1278

Closed eformx closed 7 months ago

eformx commented 7 months ago

Checks

Version

v4

Description

How do I get the data attribute, for example slide2? I have tried pagination moved and can't get the attribute. Javascript of Jquery solution?

$(document).ready(function (e) {

var splide = new Splide( '.splide', {
    type   : 'loop',
    snap   : true,
    direction   : 'ttb',
    height: '90vh'
});

splide.mount();

splide.on( 'moved', function (newIndex, prevIndex, destIndex, slide, container) {
    console.log(newIndex);
    console.log(container)
} );

});

Reproduction Link

https:

Steps to Reproduce

see above

Expected Behaviour

return slide1 or 2 or 3

eformx commented 7 months ago

chatGPT to the rescue :)

$(document).ready(function (e) {

var splide = new Splide( '.splide', {
    type   : 'loop',
    snap   : true,
    direction   : 'ttb',
    height: '90vh'
});

splide.mount();

// Function to log the data-slide attribute of the active slide
function logActiveSlide() {
    var activeSlide = splide.Components.Elements.slides[splide.index];
    var slideData = activeSlide.getAttribute('data-slide');
    console.log(slideData);
}

// Log the initial active slide
logActiveSlide();

// Listen for the moved event (triggered when the user hits the previous and next slide buttons)
splide.on('moved', logActiveSlide);

});
reilldesign commented 6 months ago

Hi,

with the option perPage: 2 two slides are displayed per move. how can i read out the html elements here. only the first one is displayed.