slick icon indicating copy to clipboard operation
slick copied to clipboard

centerMode not centering correctly

Open nathanhornby opened this issue 7 years ago • 40 comments

I've been having issues getting centerMode to center the first slide on initialisation when using variableWidth. I find that I have to move to the next slide, and back again for Slick to work out what the center actually is.

====================================================================

The provided jsfiddle required too much reworking to be relevant as this is a variable width example. I kept it as simple as possible though.

http://jsfiddle.net/1wyegw6L/16/

Interestingly in my real-world example the offset goes the other way, to the right. It actually looks like the first slide is just being offset by 50% with no correction for the slides width:

screen shot 2017-03-14 at 15 49 19

But once I've gone forward and then back a slide it corrects itself:

screen shot 2017-03-14 at 15 50 39

====================================================================

What is the expected behaviour?

Center the slideshow with a focus on the first slide.

====================================================================

What is observed behaviour?

The first slide is off-center on initialisation, but centers itself after navigation.

====================================================================

More Details

- Which browsers/versions does it happen on? Chrome 56.0.2924.87 (64-bit)

- Which jQuery/Slick version are you using? jQuery 3.1.1 Slick 1.6.0

- Did this work before? Before when?

nathanhornby avatar Mar 14 '17 15:03 nathanhornby

I just had a showerthought that maybe it's due to Slick not knowing the width of the elements on initialisation because they're images that might not have loaded yet. But I tried explicitly stating the width and it didn't seem to help:

http://jsfiddle.net/1wyegw6L/18/

I also tried creating the same example but using divs instead of images, just in case it was a similar but different issue to above:

http://jsfiddle.net/1wyegw6L/19/

But… same thing 😢

In fact the slides don't even need variable widths for the problem to occur, simply having the option turned on is enough:

http://jsfiddle.net/1wyegw6L/20/

nathanhornby avatar Mar 17 '17 14:03 nathanhornby

This has become a show-stopper on a project where I've used this plugin quite extensively - so I've created an SO in addition to this issue. Any kind of input would be appreciated @kenwheeler

nathanhornby avatar Mar 30 '17 11:03 nathanhornby

Yeah this is a legit bug

kenwheeler avatar Mar 30 '17 13:03 kenwheeler

@kenwheeler Awesome thanks for confirming Ken! 99% of the time it's a mistake at my end, so it's somewhat comforting to know ☺️

nathanhornby avatar Mar 30 '17 13:03 nathanhornby

Any suggestions on a temp fix @nathanhornby @kenwheeler? I've tried reiniting, disabling lazyload, navigating to slide 1 after init, nothing will work

henrybarn avatar Apr 12 '17 22:04 henrybarn

I'm afraid not no - waiting on a fix myself :(

On Wed, 12 Apr 2017, 23:09 henryaaron, [email protected] wrote:

Any suggestions on a temp fix @nathanhornby https://github.com/nathanhornby @kenwheeler https://github.com/kenwheeler? I've tried reiniting, disabling lazyload, navigating to slide 1 after init, nothing will work

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/kenwheeler/slick/issues/2795#issuecomment-293721786, or mute the thread https://github.com/notifications/unsubscribe-auth/AB4jgAcJusf45d95U7cTqIOJsOxFQ8VQks5rvUuegaJpZM4McxSw .

nathanhornby avatar Apr 12 '17 22:04 nathanhornby

Also able to confirm this. I weep that I won't be able to use Slick Slider on a client project because of this bug. But fully believe that the mythical Ken Wheeler shall save the day. :P

mrpritchett avatar Apr 13 '17 12:04 mrpritchett

I'm glad to work on it but I haven't even figured out where the bug stems from. I unslick'ed and slick'ed from the console with all images downloaded and saw the same issue so I don't think it's an issue with downloading the images

henrybarn avatar Apr 13 '17 13:04 henrybarn

I took a quick look when i reported it but the problem is a bit beyond me i feel. There must be a difference in the calculation when the slider is initialised to when it's interacted with - the fact it 'fixes' itself as soon as you slide leads me to believe this - in which case it could be as simple as a typo in an algorithm somewhere, or a function not being called.

nathanhornby avatar Apr 13 '17 14:04 nathanhornby

I tried to resize window after 1sec timeout, it is still buggy, but seems a good enough temp solution for now.

window.setTimeout(refreshWindow, 1000)

function refreshWindow() {
  $(window).resize()
}

darktef avatar Apr 25 '17 14:04 darktef

I was able to "solve" this by creating a breakpoint every 200px going up to a high number. Not ideal, but the client was happy. 😂 ... 😢

mrpritchett avatar Apr 25 '17 14:04 mrpritchett

@mrpritchett Curious, how did that solve your issue? The container isn't the problem for me - variable-width slides don't seem to be centred correctly at any width.

nathanhornby avatar Apr 25 '17 15:04 nathanhornby

For me it was a miscalculation between centerPadding and the browser width. So setting that at each breakpoint at a reasonable resolution change (200px) was enough to fake it. I did have instances where it wouldn’t center at all, but after playing with centerPadding and the breakpoints they seem to smooth over.

On Apr 25, 2017, at 11:55 AM, Nathan Hornby [email protected] wrote:

@mrpritchett https://github.com/mrpritchett Curious, how did that solve your issue? The container isn't the problem for me - variable-width slides don't seem to be centred correctly at any width.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/kenwheeler/slick/issues/2795#issuecomment-297076647, or mute the thread https://github.com/notifications/unsubscribe-auth/ADLMOVKYUzzxmpd4uL2UvLnW4-H1rSnQks5rzhdmgaJpZM4McxSw.

mrpritchett avatar Apr 25 '17 15:04 mrpritchett

Interesting. I think it's very specific circumstances that trigger this bug. I mean, the variableWidth example on the Slick website works fine, all whilst using centerMode, and doesn't utilise centerPadding at all. But my dead-simple jsfiddle upthread demonstrates that given the most basic of circumstances it doesn't work at all. I really don't get it.

nathanhornby avatar Apr 25 '17 16:04 nathanhornby

My settings are: centerMode, variableWidth, and initialSlide 0.

My observations are that the slide either centers properly, or the center is the left edge of initialSlide-1 (the last slide), or the center is the left edge of initialSlide-2 (second to last slide).

The refreshWindow fix didn't work for me unfortunately.

henrybarn avatar Apr 25 '17 22:04 henrybarn

Very annoyingly, the ms I open the console, the slide centers.

henrybarn avatar Apr 25 '17 22:04 henrybarn

My fix (doesn't ALWAYS work):

Disable lazyLoading.

$(window).load(function(){
        $('.slider').slick("slickGoTo",1,true);
	$('.slider').slick("slickGoTo",0,true);
});

henrybarn avatar May 04 '17 22:05 henrybarn

@henrybarnathan That does seem to do the trick! I haven't been able to test properly yet but it entirely fixes the issue on the jsfiddle: http://jsfiddle.net/b84okape/1/

nathanhornby avatar May 19 '17 09:05 nathanhornby

+1, i seem to have a variation of the same bug. there is another bug somewhere that talks about centerMode and whitespace at the start and end of the group of slides. it is to do with the clones that are made for centerMode. This means of the first of the two clones is always aligned straight to the left of my container and causes my active slide (which is supposed to be in the center) to be to the left of center. then after the autoplay kicks in i get the bug that is described here. once the screen is resized then the active slide is snapped to the center. see below for the settings i've been playing with:

settings = { centerMode: true, dots: false, variableWidth: true, autoplay: true, focusOnSelect: true, pauseOnHover: false, arrows: false, centerPadding: "7.5px", infinite:true }

i also have some sass to style my center image and create some spacing:

`.slick-slide { margin: 0 2.5px; img { opacity: 0.5; height: 190px; margin: 10px 0; } }

.slick-center { img { opacity: 1; transition: all 0.75s; height: 210px; margin: 0; } }`

finbarmaginn avatar Jun 05 '17 13:06 finbarmaginn

I came here because of the same or a similar bug. For me centerMode only works correctly when slidesToShow is an odd number.

diegovdc avatar Sep 28 '17 01:09 diegovdc

I have the same problem, centerMode simple does not center items. I've tried changing the amount of slides, and several other settings but no luck yet. Any news on a fix for this?

sergioavazquez avatar Oct 09 '17 15:10 sergioavazquez

Same problem here. Only using odd numbers on slidesToShow works. Annoying bug.

Roywcm avatar Dec 01 '17 08:12 Roywcm

Same here: https://gyazo.com/0858b983bb601c7458d7c157fb60c98e 0858b983bb601c7458d7c157fb60c98e

In first load working fine, when click next/prev or direct slide not centered right.

My code: 44649ae9b66574434f1a23599b08a47a

844ac1f0911c7374af61014511778f82

luizrrodrigues avatar May 30 '18 01:05 luizrrodrigues

the centerMode problem on even number of slides is a deal breaker. Been using this slider for about a year on number of projects and kinda sucks that this bug hasn't been fixed. :/ Temporary and "sometimes works" fixes aren't acceptable unfortunately. Good slider tho.

ForkInSpace avatar Jul 20 '18 17:07 ForkInSpace

In getTrackCSS function, spec.left doesn't have the correct value at the first render. I haven't find yet where the calculation error is

philippehenoch avatar Oct 09 '18 21:10 philippehenoch

I had the same problem ; Fixed it by refreshing the slideshow right after initializing it, with $('mySlideshow').slick('refresh'); It quickly glitches on page loading, but it's better than having it offset before change.

marionviault avatar Nov 30 '18 15:11 marionviault

Hi guys I have a problem with CenterMode

https://monosnap.com/file/0e0SWTDgUSaIbkKj8emNJ1cwoLBzOs This screen is on the MacBoock Safari with screen size 1280px - part of nex slide is visible but I dont need this

https://monosnap.com/file/yyDjCmGyiyU0jBloVcyGmlA25gQ47d On win chrome it is ok

js $('.rooms-slider').slick({ centerMode: true, dots: false, centerPadding: '50px', slidesToShow: 1, variableWidth: true, prevArrow: '.prev-slide', nextArrow: '.next-slide', responsive: [ { breakpoint: 769, settings: { dots: true, centerMode: false, variableWidth: false, centerPadding: '0px', } }, { breakpoint: 480, settings: { dots: true, } } ] });

css `.slide { @include animate(height);

	background-position: 50% 50%;
	background-size: cover;
	background-repeat: no-repeat;
	width: 320px;
	height: 500px;
	outline: none;
	display: flex;
	align-items: center;
	justify-content: center;
	position: relative;

	@include media('>=375px') {
		width: 375px;
	}

	@include media('>=414px') {
		width: 414px;
	}

	@include media('>=768px') {
		width: 768px;
	}

	@include media('>=1024px') {
		width: 1024px;
	}

	@include media('>=1200px') {
		width: 1200px;
	}

	@include media('>=1280px') {
		width: 1280px;
	}

	@include media('>=1366px') {
		height: 600px;
		width: 700px;
		margin: 0 25px;
	}

	@include media('>=1920px') {
		width: 1260px;
	}`

Smile3D avatar Apr 25 '19 17:04 Smile3D

Had problems on safari iphone, fixed it by set centerPadding to 0

KonstantinGreat avatar Jun 24 '19 13:06 KonstantinGreat

I fixed this by adding box-sizing border-box on slick-list:

.slick-list {
    box-sizing: border-box;
}

rafaelfndev avatar Nov 15 '19 16:11 rafaelfndev

Two and a half years later, the bug still exists.

astralmaster avatar Feb 01 '20 09:02 astralmaster