Slick carousel JS library stacking all content on the first slide

Shandy Sulen

The Slick JS library for carousels and sliders has been working well for me aside from one irritating, buggy instance. My slider of four slides (removed infinite looping so it's more a slider than a carousel) is implemented within a Bootstrap modal. Upon clicking the button to bring the modal up, it's clear that all the content of all the Slick slides is stacked and overlapped upon one another on the first slide. Strangely, a fix seems to be resizing the browser window. A window resize event seems to adjust the slider appropriately so that the first slide only shows its content. I believe I would be able to implement the triggering of a window resize event, but this solution seems...janky. Is there any ideas for solutions that seem more suitable, e.g. passing in an init object to the slick initialization method that fixes the problem at its source?

I've seen similar questions on Stack Overflow and GitHub, but none of the provided solutions seems to work for me.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Slick Demo</title>
    <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">    
    <link rel="stylesheet" href="slick-1.8.1/slick/slick.css"/>
    <link rel="stylesheet" href="slick-1.8.1/slick/slick-theme.css"/>    
</head>
<body>    

  <button type="button" id="questionnaire-modal" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
    Launch demo modal
  </button>

  <!-- Modal -->
  <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Questionnaire</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body px-5 bg-light">                  
          <div class="questionnaire" style="padding: 20px;">
            <div>1</div>
            <div>2</div>
            <div>3</div>
            <div>4</div>
          </div> 
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary" id="submit" style="background-color: #fbb03b; border: black;">Submit</button>
        </div>  
      </div>
    </div>
  </div>
  <script src="node_modules/jquery/dist/jquery.min.js"></script>
  <script src="node_modules/popper.js/dist/popper.min.js"></script>
  <script src="node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
  <script type="text/javascript" src="slick-1.8.1/slick/slick.min.js"></script>  
  <script>
    $(document).ready(() => {   

      // Initialize slick slider
      let slickSlider = $('.questionnaire');  
      slickSlider.slick();          

      // Grab references to controls
      let submitButton = $('#submit');
      let prevButton = $('.slick-prev');
      let nextButton = $('.slick-next');                  

      // Disable 'previous' arrow and 'submit' button when modal is activated   
      $('#questionnaire-modal').one('click', (event) => {            
        prevButton.hide();
        submitButton.attr('disabled', true);
      });

      // React to changes in slider
      slickSlider.on('afterChange', (event, slick, currentSlide, nextSlide) => {

        // Immediately blur focus on 'previous' and 'next' arrow buttons
        prevButton.blur(); nextButton.blur();

        // Hide 'previous' button on first slide and show 'previous' button on all other slides
        (currentSlide === 0) ? prevButton.hide() : prevButton.show();

        // Hide 'next' button on last slide and show 'next' button on all other slides
        // Also show 'submit' button on last slide
        if (currentSlide === 3) {
          nextButton.hide();
          submitButton.attr('disabled', false);
        } else {
          nextButton.show();  
          submitButton.attr('disabled', true);
        }

      });      

    });                  
  </script>
</body>
</html>
Shandy Sulen

Thanks to the helpful comment, I was able to find a suitable solution. Change the visibility style of div.modal-content to hidden. Then add the following event handler to the script:

      $('#exampleModal').on('shown.bs.modal', (event) => {                         
        $slickSlider.slick('setPosition');
        $('.modal-content').css('visibility', 'visible');
      });   

This allows .slick('setPosition') to run when the content is invisible which avoids the user seeing the slider resize itself within the modal. Then make the content visible once again.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Slick Carousel with images not getting slide

Slick Carousel: slide is not full width inside grid child with dynamic width

Toggle Animate css classes on slick carousel slide change

Bootstrap Carousel affects all fonts on change slide

jQuery slick carousel "slide" setting

Slick Carousel with Angular JS

slick carousel individual slide duration

Append slick arrows inside the slide content

Slick slider animate back to first slide

Transform animation not working for first slide in bootstrap carousel

Slick slider, padding in the first slide

Slick Carousel full screen vertical slide gap between section

How to detect the last slide and first slide in slick?

Slick carousel: Slide variable number of elements

How To Fixed the first slide and start from second slide in Slick.js?

Slick carousel is broken until first slide

slide to last slide in carousel with timer - slick.js

slick.js - why is the first slide cloned when using bootstrap row?

FlexSlider carousel slides all images in first slide - WordPress

Slick slider YouTube glitch on last and first slide

Slick carousel doesn't slide all items

Slick carousel 2nd slide visible

Slick Carousel Resets to First Slide in Bootstrap 3

Javascript slick.js carousel dragging more than one slide backwards past first element not working

Fake a click to next slide in slick carousel

Change content based on active Bootstrap Carousel Slide

Carousel: return to the first slide after arrive to the end

Slick carousel jumping in between slide

javascript carousel not showing first slide

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive