Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slick initializes with width:0 on all relevant elements #235

Closed
FredericoAndrade opened this issue May 15, 2014 · 21 comments
Closed

Slick initializes with width:0 on all relevant elements #235

FredericoAndrade opened this issue May 15, 2014 · 21 comments

Comments

@FredericoAndrade
Copy link

Hi there,

in adding to the $(document).ready( function() { $('.slickPhotos').slick() }), the script runs properly, creates all the DOM elements etc, but does not determine the correct widths until I first drag on the image.

screen shot 2014-05-14 at 11 57 39 pm

Only when I drag on the image does the carousel seem to fully execute.

screen shot 2014-05-15 at 1 35 24 am

There are no console errors thrown so I'm at a loss. Thanks for the help!

@kenwheeler
Copy link
Owner

Is your carousel hidden at first? Are you executing prior to document being ready? An easy way to reinit slick dimensions is to manually trigger a window resize event.

@Splintex
Copy link

It still working bad even with trigger a window resize event. Could you check this bug?

@simeydotme
Copy link
Collaborator

by the HTML screenshot it looks like you have Slick in a modal/lightbox, if this is true then as @kenwheeler said: your carousel is hidden... and javascript cannot calculate dimensions of a hidden object :)

The way to rectify this is to reset the dimensions _after_ the modal/lightbox is displayed. :)

@goodpixels
Copy link

this looks like something i am experiencing: #1325

@lichen-mill
Copy link

I experienced the same problem and fixed it according to @kenwheeler's suggestion, triggering a resize function:

  $(document).ready(function() {

            $('.slideshow').slick({
                fade: true,
            });

            $('.project-body').hide();

            $('.project-link').click(function(e){
                 e.preventDefault();
                 var $this = $(this).parent().find('.project-body');
                 $(".project-body").not($this).hide();
                 $(".project").not($(this).parent()).removeClass("project-border");  
                 $($this).toggle();
                 $(this).parent().toggleClass("project-border");

                 $(window).trigger('resize');

                 $("body, html").animate({ 
                     scrollTop: $( $(this).attr('href') ).offset().top 
                });
            });
   }); 

So that when you click on ".project-link", the corresponding ".project-body" expands and other projects close, and ideally the slick dimensions could be reset. This only works for the first project I click on, however... for some reason it doesn't work every time a project link is clicked after that. Here's the HTML:

       <div class = "project>
                 <a href = "#" class = "project-link">Project Link</a>
       </div>

       <div class = "project-body">
                 <div class = "project-gallery">
                          <div class = "project-text">Project Text</div>
                          <div class = "slideshow">
                                    <div><img class="project-image" src="IMAGE.JPG" alt="Home"></div>
                          </div>
                 </div>
        </div>

Any idea why all subsequent project links aren't resetting the dimensions so that the first slideshow image shows? Thank you!

@jimfu
Copy link

jimfu commented Jan 11, 2017

I've found out that the AD-block makes it hidden in the beginning because I named the class as 'ad-banner'

@MisterBalboa
Copy link

MisterBalboa commented Jan 20, 2017

@em-elle is correct ! Having a hidden node makes width crash. The problem is solved as @kenwheeler's suggestion, just to clarify, you should trigger the resize function where ever you have the function that renders the slide. Most commonly on a click event

button.onclick = function() {
  // Other actions..
  $(window).trigger('resize');
}

@vinayakjo
Copy link

Here is what I did to resolve the issue. I was loading slick slider with thumbnail navigation in jquery's colorbox and was hidden by default.

Code for slick slider:

$('.slider-for').slick({
	  slidesToShow: 1,
	  slidesToScroll: 1,
	  arrows: false,
	  fade: true,
	  asNavFor: '.slider-nav'
	});
	$('.slider-nav').slick({
	  slidesToShow: 3,
	  slidesToScroll: 1,
	  asNavFor: '.slider-for',
	  dots: true,
	  centerMode: true,
	  focusOnSelect: true
	});	

Code for refreshing the slider after colorbox was opened:

$(".quickview-link").colorbox({
inline:true, 
width:"700px",
top:"100px",
onComplete:function(){
$(".slider-for").slick("refresh");
$(".slider-nav").slick("refresh");
} 
});	

Hope this helps as I spent a while searching for the solution and this one works for me.

@booster085
Copy link

booster085 commented Apr 6, 2017

I am using slick with kendo and the problem was that slick is called at the same time after the dom is rendered, in pure html there's no problem, but with kendo for example you need a little timeout to let slick calculate right dimensions. So adding timeout makes things work fine.
var st = setTimeout(function () { $('.carousel').slick({ dots: true }) }, 100);

@RBrNx
Copy link

RBrNx commented Sep 21, 2017

I also struggled with this issue, but it was not related (at least directly) to an element with display: none set. In my project I was also using less.js for processing my .less files in the browser, this was taking around 100-150ms to complete and slick was working fine.

The width: 0px appeared when I added an @import to one of my .less files causing the processing to go up to around 200-230ms, this was enough of a delay that the HTML elements were not styled when slick was initialized. To fix it I simply added a delay to the initialization of slick:

    setTimeout(function () {
        $(".container-body").slick({
            arrows: false,
            swipe: false,
            infinite: false
        });
    }, 500);

@Defite
Copy link

Defite commented Mar 31, 2018

@RBrNx setTimeout 0 will work as well :)

@Aerials92
Copy link

Aerials92 commented Jun 5, 2018

First of all:
@kenwheeler thanks a lot for this awesome slider. :) its damn sexy!

To help you guys fixing this "hidden slider Problem" i found a solution at least for my project:
@vinayakjo helped me a lot for that! Thank you buddy :)

My slick slider is defined in a hidden tab too, so the width of the sliding elements cant be calculated.
I fixed the problem by using this simple line of code:
$('.your_hidden_slider_class_or_id').slick("refresh");
Paste this into the function which sets the hidden element to display:block.
Cheers.

@IbnAhmed
Copy link

IbnAhmed commented Jul 23, 2018

@Aerials92 thanks bro. It's work for me too.

$('.your_hidden_slider_class_or_id').slick("refresh");

@seriiserii825
Copy link

@Aerials92 It's work for me too.

@pqminh
Copy link

pqminh commented Dec 10, 2018

@Aerials92 It's work for me too. Tks!

@WebmasterManish
Copy link

WebmasterManish commented Oct 22, 2019

I found this issue when my slick was inside accordion.Other tricks was not working.Here is the short trick if your slider is inside accordion.

Wrap your slider code inside accordion tab click. for example

$('#accordion-tab').click(function(){

Your slick slider functions goes here

})

@naveenlb-epsilon
Copy link

This worked for me too, thanks @Aerials92

@tanero
Copy link

tanero commented Dec 11, 2019

This issue and solution still valid, thank you for recommendations.

@bearpress
Copy link

Only thing that worked for me:

$(".element")[0].slick.setPosition();

@avocadoslab
Copy link

Instead of targeting whole window, following worked for me

$('.product-images-div').trigger('resize');

@tiffanybowers
Copy link

First of all: @kenwheeler thanks a lot for this awesome slider. :) its damn sexy!

To help you guys fixing this "hidden slider Problem" i found a solution at least for my project: @vinayakjo helped me a lot for that! Thank you buddy :)

My slick slider is defined in a hidden tab too, so the width of the sliding elements cant be calculated. I fixed the problem by using this simple line of code: $('.your_hidden_slider_class_or_id').slick("refresh"); Paste this into the function which sets the hidden element to display:block. Cheers.

This totally worked for me. Thanks!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests