Turn off slide duplication if total slides is less than perPage | potential bug with redefining breakpoints? #419
Replies: 5 comments
-
|
I've developed a workaround by targeting the number of slides before Splide is mounted, then using ternary operators to define breakpoints. var elms = document.getElementsByClassName( 'splide' );
for ( var i = 0, len = elms.length; i < len; i++ ) {
// save the total number of slides to a variable
var slideCount = elms.querySelector('ul.splide__list').childElementCount;
if ( slideCount < 6 ) {
new Splide( elms[ i ], {
perPage: slideCount,
breakpoints: {
1180: {perPage: (slideCount < 4 ? slideCount : 4)},
1080: {perPage: (slideCount < 3 ? slideCount : 3)},
880: {perPage: 2} // if there can be only one slide, this should also be ternary
}
});
} else {
// normal initialization
}
}This is working so far for me. I'll try to remember to update this post if I find any new information or if something goes horrendously wrong. |
Beta Was this translation helpful? Give feedback.
-
|
In case anyone is still looking for a solution to this, this seems to work: |
Beta Was this translation helpful? Give feedback.
-
|
Coming back for all my homies still using jQuery. Here's a solution that mounts multiple splides and includes breakpoints... |
Beta Was this translation helpful? Give feedback.
-
|
I think the better approach is to work with the clones option. //set the splide option variables depending on the number to be displayed |
Beta Was this translation helpful? Give feedback.
-
|
I have the same problem and it seems to be a recurring issue with other vanilla JS slider libraries. The behaviour works great when 'type' is set to 'slide' but not when set to 'loop'. The other challenge is getting this to work on resize as well as load. I can't get any of the above solutions to work so would be awesome if this was built into the library. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using Splide to generate a product recommendations slider. The slides are pulled from a list of similar products. Generally, the recommendations section will show six similar products at a time (perPage: 6). However, not all items will have as many as six similar products; for example, the product I'm testing with only has four recommended items. Splide's default behavior is to make enough clones visible to fill the slider. On my test item, this means two products in the recommendations will be duplicates. This pointlessly clutters up the page, as it does no good to recommend the same product twice.
If I wasn't using breakpoints to make the Splide responsive, I could just do this:
I've tested this and it works perfectly as long as the user doesn't change the viewport. However, if the viewport does get changed, it will revert entirely back to the old behavior.
So, I thought I just need to update the breakpoints, right? Well, two problems:
Beta Was this translation helpful? Give feedback.
All reactions