` Why isn't Glide.js functioning correctly when incorporated into a Bootstrap 4 modal component?`

I'm currently utilizing Glide.js and a Bootstrap 4 modal on our team page to showcase the biography of the selected team member. This functionality is achieved by extracting the attribute of the clicked team member and using it as the startAt: index for glide.

The code executes flawlessly upon the first interaction. However, if I close the modal and reopen it by clicking on another team member, the modal retains all its HTML elements and glide controls, yet the inline styles of all slides have been set to width:0;, rendering the modal empty with no visible slides.

In addition, the navigation dots fail to function even on the initial load.

import Glide from "@glidejs/glide";

class LeadershipSlider {
    constructor() {
        if (document.querySelector("#leadership-slider")) {
            // Determine the number of slides
            const dotCount = document.querySelectorAll(".leadership-slider__slide").length;

            // Generate 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}"></button>`;
            }

            var glide = new Glide("#leadership-slider", {
                type: "carousel",
                perView: 1,
                gap: 0,
            });

            // Fetch data attribute for slide to startAt
            let slides = document.querySelectorAll(".icon-biography-opt");

            for (var i = 0; i < slides.length; i++) {
                slides[i].addEventListener("click", function () {
                    var slideToStart = this.getAttribute("data-leader-index");

                    glide.update({
                        startAt: slideToStart,
                    });
                });
            }

            glide.mount();

            // Append the dots HTML to the DOM
            document.querySelector(".glide__bullets").insertAdjacentHTML("beforeend", dotHTML);
        }
    }
}

export default LeadershipSlider;

Answer №1

Encountering the same issue of integrating glide into a modal has been quite tricky. Through numerous tests, it seems that the problem arises from the timing of when the glide initialization occurs. In my scenario, I noticed that the slide had a width: 0 because the slider hadn't been initialized yet.

Here's an example I came across that might shed some light on the issue: https://github.com/glidejs/glide/issues/504

Answer №2

It is crucial to initialize Glide after the modal has been generated. You should track the shown.bs.modal event within the modal to trigger Glide setup at the appropriate time.

document.getElementById('modal').addEventListener('shown.bs.modal', function () {
    new Glide('.carousel', {
        type: 'carousel',
    }).mount();
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Clickable link unresponsive on parallax-enhanced webpage

Currently, I am utilizing Zurb foundation's Manifesto theme for creating a parallax scrolling landing page. The anchor tag is essential for the scrolling effect on this page, causing a conflict when regular anchor links are included. Here is the HTML ...

How to Customize the Size and Color of secureTextEntry Inputs in React Native

When it comes to styling a password input like the one below: <TextInput name="Password" type="password" mode="outline" secureTextEntry={true} style={styles.inputStyle} autoCapitalize="none" autoFocus={true} /> I also ap ...

Having trouble showing my Google map using canvas

Can anyone help me with this issue that I'm facing? I am working on a JavaScript web page and trying to show a Google map (Google API) along with a canvas tag in the html body. Currently, the map and canvas are only displaying separately. https://i ...

VueTablePlus dropdown filter options for Vue.js version 2

I am facing an issue with populating dropdown options on vue good table. My approach involves making an API call to fetch the possible values for the dropdown and then assigning them to the column filter. However, I am struggling to make it work. <vue ...

Optimal approach for incorporating individual identifiers into a complex hierarchy of object arrays

I am looking to assign unique ids to an array of objects: For example: const exercises = [ { type: "yoga", locations: [ { name: 'studio', day: 'Wednesday' }, { name: 'home' ...

When attempting to use JQuery autocomplete, the loading process continues indefinitely without successfully triggering the intended function

Currently, I am utilizing JQuery autocomplete to invoke a PHP function via AJAX. Below is the code snippet I am working with: $("#client").autocomplete("get_course_list.php", { width: 260, matchContains: true, selectFirst: false }); Upon execution, ...

Abbreviating Column Labels in Google Visualization

Is there a way to use the google visualization API to display column headers in an abbreviated form in a table, but show the full labels in a pie chart using the same dataset? Check out this snippet of JavaScript: //create the dashboard and table chart ...

Swapping React Components with a Click of a Button

My webpage features a button labeled "Sign Up". Once this button is clicked, I want it to display a new component named "SignUp" in place of the original button. Currently, my method involves using setState to trigger the rendering of the new component upo ...

What are the different ways to utilize the Angular Loading Bar for specific waiting scenarios?

In the project I am currently working on, navigation is primarily done through Ajax loading of HTML templates. The Angular Loading Bar feature is proving to be quite effective in this setup, as it employs interceptors to keep track of most $http requests. ...

Choosing an option from a PHP MySQL table based on a JavaScript value

I am attempting to create a select box that displays a value based on whether the database has a "yes" or "no" in the specified column. Despite my efforts, I am unable to identify any syntax errors causing this code snippet to not function properly. JavaSc ...

Issue with useEffect causing a delay in updating the state value

I'm facing an issue with a component that displays the number of people who have liked a book. The problem is, I can't seem to consistently get the correct result in my states. Here's the code snippet: ///Fetching the book details cons ...

Sift through JavaScript problems

My goal is to implement a filtering function on my input that will display a different result. However, I have encountered an issue where the this.data.stationInfo.stations array is being changed, as shown in the console.log output. According to the Mozil ...

No matter the way I input the URL in the AJAX call, the response always comes back as successful

I ran into an issue with my ajax request. It was supposed to be a GET request that returned some data, but no matter how I configured the URL, it always gave a success response even when it didn't actually succeed. var id = 5; $.ajax({ type: ...

Angular2's change detection mechanism does not behave as anticipated after receiving a message from a Worker

Within my angular2 application, I encountered a rather intriguing scenario which I will simplify here. This is AppComponnet export class AppComponent { tabs: any = []; viewModes = [ { label: "List View"}, { label: "Tree View" }, ...

How can we implement the MUI snackbar to only show when a successful login occurs in ReactJS?

How can I display the material-ui snackbar in ReactJS only upon successful login? What approaches can be used to achieve this in ReactJS? ...

Plotly: maintaining consistent color scheme across identical elements in multiple graphs

I am currently utilizing Plotly in Javascript to generate some interactive graphs. My goal is to create 2 pie charts that display the distribution of a sale based on A) units and B) value. While I am able to generate the plots successfully, I have notice ...

Warning message in React about missing floating prop in components

application.js (Webpack Entry Point) import React from 'react'; import ReactDOM from 'react-dom'; import App from './App.jsx'; document.addEventListener('DOMContentLoaded', () => { ReactDOM.render(<App /> ...

What could be preventing req.session from being set in node.js?

Having trouble setting anything for req.session My goal is to securely store oauth token and secret in a session for verification after the authorize callback. Below is the code snippet: var express = require('express'), app = express ...

Tips for eliminating duplicate entries in ag grid using Angular

Is there a way to eliminate the recurring assetCode entries in ag grid? The PRN and PRN1 values seem to be repeating unnecessarily. Check out the code below: list.component.ts ngOnInit() { this.rowData.push( { 'code': 'Machi ...

evaluate individual methods within a stateless component with unit testing

I am working with a stateless component in React that I need to test. const Clock = () => { const formatSeconds = (totalSeconds) => { const seconds = totalSeconds % 60, minutes = Math.floor(totalSeconds / 60) return `${m ...