DeskCat
DeskCat

Reputation: 31

GlideJS Renders slides too far off screen

Long story short, Adding a carousel to a wordpress site. I'm using glideJS for this and I am encountering an issue where the width and transform values are ridiculously large and the slides are rendering off-screen.

Additional Context: I am using the wordpress wp-scripts to build js and scss packages. The build is compiling correctly and it is registered correctly because I am also using glideJS slides elsewhere.

Here are screenshots and code:

snippet from my front-page.php, This is the carousel in question:

<div class="featured-slider">
              <div data-glide-el="track" class="glide__track">
                <div class="glide__slides">
                  
                  <div class="featured-slider__slide">
                      <div class="featured-slider__interior">
                          <div class="card-window">

                          </div>
                      </div>
                  </div>
                  <div class="featured-slider__slide">
                      <div class="featured-slider__interior">
                          <div class="card-window">

                          </div>
                      </div>
                  </div>
                  <div class="featured-slider__slide">
                      <div class="featured-slider__interior">
                          <div class="card-window">

                          </div>
                      </div>
                  </div>
                  <div class="featured-slider__slide">
                      <div class="featured-slider__interior">
                          <div class="card-window">

                          </div>
                      </div>
                  </div>
                  <div class="featured-slider__slide">
                      <div class="featured-slider__interior">
                          <div class="card-window">

                          </div>
                      </div>
                  </div>         
                </div>
              </div>
              <div class="slider__bullets glide__bullets" data-glide-el="controls[nav]"></div>
      </div>

Here is the scss being used:

.featured-img{
  width: 350px;
  height: 400px;
}

.featured-slider {
  // display: flex;
  // justify-content: center;
  // align-items: center;
  // flex-direction: row;
  margin-bottom: 400px; //remove later
    position: relative;
    overflow: hidden;
    
    div {
      outline: none;
    }
    
    & > .glide__track > .glide__slides{
      // display: flex;
      // justify-content: center;
      // align-items: center;
      // flex-direction: row;
      // gap:0.5rem;
    }
    &__interior {
      z-index: 0;
      
      // padding-top: 60px;
      // padding-bottom: 60px;

      // display: flex;
      // justify-content: center;
      // align-items: flex-end;
  
      @include atMedium {
        // padding-top: 130px;
        // padding-bottom: 50px;
      }

      & > .card-window{
        background-color: plum;
        height: 400px;  
        width: 400px;
      }
    }
  
    &__slide {
     

      width: 400px;
      height: 400px;

    }
  
    &__overlay {
      // text-align: center;
      margin: 0 auto;
      // background-color: rgba(0, 0, 0, 0.68);
      // padding: 40px;
      color: #fff;
      @include atMedium {
        width: 50%;
      }
    }
  }

The js code to mount glideJS: (HeartBullet is just a heart shaped icon for the carousel bullets)

import Glide from "@glidejs/glide";
import HeartBullet from "../../assets/images/heart-bullet.png";

class FeaturedCarousel {
  constructor() {
    if (document.querySelector(".featured-slider")) {
      // count how many slides there are
      const dotCount = document.querySelectorAll(
        ".featured-slider__slide"
      ).length;

      // Generate the HTML for the navigation dots
      let dotHTML = "";
      for (let i = 0; i < dotCount; i++) {
        dotHTML += `<button class="slider__bullet glide__bullet" data-glide-dir="=${i}" style="background-image: url(${HeartBullet})"></button>`;
      }

      // Add the dots HTML to the DOM
      document
        .querySelector(".featured-slider > .glide__bullets")
        .insertAdjacentHTML("beforeend", dotHTML);

      // Actually initialize the glide / slider script
      var glide = new Glide(".featured-slider", {
        type: "carousel",
        perView: 4,
        // autoplay: 1000,
      });

      glide.mount();
    }
  }
}

export default FeaturedCarousel;

Here is the issue I am encountering: The styles generated by glideJS for the glide__slides div

I have found the issue on the glideJS github: https://github.com/glidejs/glide/issues/654

I have tried one of the suggested solutions as you can see where I commented out pretty much all center alignments.

I have tried overwriting the transform but it breaks the autoplay and navigation functions because the only way to overwrite it is with the "!important" tag.

I have another working carousel on the same page that is working perfectly fine but I have updated all the classes to be unique and using the scss hierarchy for styling.

Upvotes: 2

Views: 723

Answers (1)

DeskCat
DeskCat

Reputation: 31

I have discovered the issue and also a relatively simple solution.

To understand the issue I need to expand my initial code snippet:

<section class="page-section container--flex">
  <h1 class="headline headline--medium font-marker font-color__dark">
    Featured Products
  
  </h1>
  
    <div class="featured-slider">
      <div data-glide-el="track" class="glide__track">
        <div class="glide__slides">
          
          <div class="featured-slider__slide">
              <div class="featured-slider__interior">
                  <div class="card-window">

                  </div>
              </div>
          </div>
          <div class="featured-slider__slide">
              <div class="featured-slider__interior">
                  <div class="card-window">

                  </div>
              </div>
          </div>
          <div class="featured-slider__slide">
              <div class="featured-slider__interior">
                  <div class="card-window">

                  </div>
              </div>
          </div>
          <div class="featured-slider__slide">
              <div class="featured-slider__interior">
                  <div class="card-window">

                  </div>
              </div>
          </div>
          <div class="featured-slider__slide">
              <div class="featured-slider__interior">
                  <div class="card-window">

                  </div>
              </div>
          </div>         
        </div>
      </div>
      <div class="slider__bullets glide__bullets" data-glide-el="controls[nav]"></div>
    </div>
 
</section>

Initially I thought that the featured-slider class was the issue and should have center alignments removed. However, after takeing a closer look at the DOM I noticed that the featured-slider class had additional GlideJS classes appended to it:

Screenshot of DOM Element

I then removed the container--flex class from the section tag and it just worked. Here is the container--flex scss:

.container--flex{

    display: flex;
    flex-direction: column;
    
    justify-content: center;
    align-items: center;
    
}

Conclusion: Turns out the parent element of the element you use to mount glide cannot use center alignment.

However, there is a very simple way to get around this issue which is to use the align-self property to set the feature-slide's alignment to normal

.featured-slider {
  align-self: normal; // <--- this is all that was needed



  margin-bottom: 400px; //remove later
  position: relative;
  overflow: hidden;
    
    div {
      outline: none;
    }
    
   
    &__slide {
     

      width: 400px;
      height: 400px;

    }

}

Upvotes: 1

Related Questions