Slide is not centering
I'm unable to centre the slide image to take an even space between the left/right arrows:

render() {
const items = this.props;
const settings = {
dots: true,
infinite: false,
speed: 500,
fade: true,
};
return (
<div className={css.container}>
{ items.length && <Slider {...settings}>
{ items.map((entry, index) => (
<div key={index}>
<img src="http://placehold.it/350x150" alt="Items"/>
</div>
))}
</Slider> }
</div>
);
}
// css.container compiles to:
.container {
padding: 3rem;
max-width: 960px;
margin: 0 auto;
}
Adding centerMode: true completely breaks it by showing all images as dispay:block; and opacity:0; so it takes up space vertically.
When you resize the browser it snaps into place and everything centers/works flawlessly. So, looks like its not doing something its meant to on initialise?
I just checked the demo in this project and made a copy of the CenterMode, it's working like it should in demo :-)
I'm seeing centerMode={true} with centerPadding break the slider by showing multiple (vertical) rows of images too
If anyone has any problem like this anymore, please consider creating a demo on codesandbox and reopening the issue or creating a new one.
According to @chellberg I'v noticed it happens when the wrapping element have a width set. Setting centerPadding to 0px fixes the problem that the carousel has two rows.
https://codesandbox.io/s/0mzw0k4x7w
This is 2024 and I don't want anyone to wrap their head around this issue.
The trick is using afterChange.
const settings = {
className: "center slider variable-width slick-gradient",
centerMode: true,
infinite: true,
slidesToShow: 1,
speed: 100,
rows: 1,
slidesPerRow: 1,
arrows: false,
centerPadding: "100px",
beforeChange: (_oldIndex, newIndex) => {
// ...
},
afterChange: (currentIndex) => {
// Select the element with the class 'slick-track'
var element = document.querySelector('.slick-track');
// Get the current transform value
var currentTransform = getComputedStyle(element).transform;
// Extract the x translation value. The match function attempts to find the numbers in the matrix transform, which are separated by commas.
// The matrix format is matrix(a, b, c, d, tx, ty) where tx is our x translation.
// If the transform is in the translate3d format, it should still match correctly.
var match = currentTransform.match(/matrix\(([^,]+),([^,]+),([^,]+),([^,]+),([^,]+)/);
if (match) {
var currentX = parseFloat(match[5]); // Get the x translation value
// Calculate the new x value by adding 45
var newX = currentX + 45;
// Set the new transform value
element.style.transform = 'translate3d(' + newX + 'px, 0px, 0px)';
} else {
console.log("Could not parse the current transform value.");
}
},
variableWidth: true,
};
Consider to follow me if this solves your issue. :D