Allow for event listening

Created on 6 March 2025, about 1 month ago

Unless I am missing something, there seems to be no way to listen to any event (like slide.bs.carousel) on a carousel created with this module.

✨ Feature request
Status

Active

Version

5.4

Component

Carousel

Created by

πŸ‡¨πŸ‡­Switzerland mrupsidown

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @mrupsidown
  • πŸ‡ΊπŸ‡ΈUnited States shelane

    This module only provides the style processing for view data. All of the JavaScript for Bootstrap carousel behavior should be provided by the theme.

  • πŸ‡¨πŸ‡­Switzerland mrupsidown

    Which is exactly what I am trying to do. Listen to the "slide.bs.carousel" event in my theme JS and which doesn't work. How would you do that?

  • πŸ‡ΊπŸ‡ΈUnited States shelane

    Can you try:

    Drupal.behaviors.bsCarousel = {
      attach: function (context, settings) {
        $(once('bs-carousel', '#views-bootstrap-home-slider-block-1', context)).each(function () {
          // Ensure the carousel element exists
          $(this).on('slid.bs.carousel', function () {
            console.log('CAROUSEL HAS SLID'); // This should now work
          });
        });
      }
    };
  • πŸ‡¨πŸ‡­Switzerland mrupsidown

    Thanks for the follow up, unfortunately this doesn't work either.

    I tried logging `$(this)` and it seems the element is fine but still nothing from the slid event.

  • πŸ‡ΊπŸ‡ΈUnited States shelane

    I just saw that there is some javascript in the module related to the carousels. I'm going to update it to remove its dependence on jQuery and see what else it may be doing to interfere.

  • πŸ‡ΊπŸ‡ΈUnited States shelane

    In light of the other script and changes I made to it to remove jQuery, try this using the dev branch:

    (function () {
    
      'use strict';
    
      /**
       * Attaches the behavior to the user's custom carousel functionality.
       */
      Drupal.behaviors.bsCarousel = {
        attach: function (context, settings) {
          // Select the specific carousel element
          const carousel = document.querySelector('#views-bootstrap-home-slider-block-1');
    
          if (carousel) {
            // Ensure the carousel has more than one slide
            const slides = carousel.querySelectorAll('.carousel-inner > div');
            if (slides.length > 1) {
              // Attach the event listener for the 'slid.bs.carousel' event
              carousel.addEventListener('slid.bs.carousel', function () {
                console.log('CAROUSEL HAS SLID');
              });
            } else {
              console.log('Carousel has only one slide, event listener not attached.');
            }
          }
        }
      };
    
    }());
Production build 0.71.5 2024