El carrusel de imágenes de Visual Composer no se repite correctamente

7 minutos de lectura

avatar de usuario
Radoslaw Roszkowski

Es mi primera publicación, así que por favor perdonen mi inglés.

Tengo un problema con el componente Image Carousel VC. Necesito configurar algo como el control deslizante de logotipos (5 logotipos seguidos) y cuando lo configuro para mostrar 5 de 6 imágenes, hay un espacio al final (como 4 espacios vacíos).

Mi configuración:

6 imágenes añadidas, diapositivas por vista: 5, bucle deslizante: sí

El objetivo es configurar la carrusel para que se reproduzca en bucle, sin espacios vacíos entre las imágenes.

También hay un problema de respuesta. Cuando cambio el tamaño de las imágenes de la ventana del navegador, pierde su proporción (el ancho es un porcentaje de escala y la altura es fija).

¿Alguien puede ayudarme a lidiar con eso?

  • Problema de respuesta: el ancho es%, ¿la altura es fija? Eso debería distorsionar las imágenes. % de ancho y alto: aplica estilo automáticamente a la imagen para que responda a los cambios de ancho, sin cambios en la relación de aspecto.

    – muka.gergely

    19/09/2016 a las 12:30

  • Esto debería solucionar su problema. Asegúrese de agregar vc_custominfiniteloop como el nombre de clase adicional humbertosilva.com/visual-composer-infinite-image-carousel

    – Anna Wellington

    24 de diciembre de 2017 a las 21:10

Portando a Humberto Silva correo en una respuesta de desbordamiento de pila, parece que no hay una forma integrada de hacer esto, pero hay una solución en JavaScript.

Primero, agrega una clase extra vc_custominfiniteloop al elemento carrusel.

bucle infinito de carrusel de compositor visual

Luego, agregue este código JavaScript después de que jQuery y Visual Composer javascript se hayan cargado:

/*
Turn Visual Composer Image Carousel into Visual Composer Infinite Image Carousel
Include before the </head> tag on yout theme's header.php 
Read the detailed step-by-step at https://humbertosilva.com/visual-composer-infinite-image-carousel/
*/

// auxiliary code to create triggers for the add and remove class for later use
(function($){
$.each(["addClass","removeClass"],function(i,methodname){
    var oldmethod = $.fn[methodname];
    $.fn[methodname] = function(){
          oldmethod.apply( this, arguments );
          this.trigger(methodname+"change");
          return this;
    }
});
})(jQuery);

// main function for the infinite loop
function vc_custominfiniteloop_init(vc_cil_element_id){

  var vc_element="#" + vc_cil_element_id; // because we're using this more than once let's create a variable for it
  window.maxItens = jQuery(vc_element).data('per-view'); // max visible items defined
  window.addedItens = 0; // auxiliary counter for added itens to the end 

  // go to slides and duplicate them to the end to fill space
  jQuery(vc_element).find('.vc_carousel-slideline-inner').find('.vc_item').each(function(){
    // we only need to duplicate the first visible images
    if (window.addedItens < window.maxItens) {
      if (window.addedItens == 0 ) {
        // the fisrt added slide will need a trigger so we know it ended and make it "restart" without animation
        jQuery(this).clone().addClass('vc_custominfiniteloop_restart').removeClass('vc_active').appendTo(jQuery(this).parent());            
      } else {
        jQuery(this).clone().removeClass('vc_active').appendTo(jQuery(this).parent());         
      }
      window.addedItens++;
    }
  });

  // add the trigger so we know when to "restart" the animation without the user knowing about it
  jQuery('.vc_custominfiniteloop_restart').bind('addClasschange', null, function(){

    // navigate to the carousel element , I know, its ugly ...
    var vc_carousel = jQuery(this).parent().parent().parent().parent();

    // first we temporarily change the animation speed to zero
    jQuery(vc_carousel).data('vc.carousel').transition_speed = 0;

    // make the slider go to the first slide without animation and because the fist set of images shown
    // are the same that are being shown now the slider is now "restarted" without that being visible 
    jQuery(vc_carousel).data('vc.carousel').to(0);

    // allow the carousel to go to the first image and restore the original speed 
    setTimeout("vc_cil_restore_transition_speed('"+jQuery(vc_carousel).prop('id')+"')",100);
  });

}

// restore original speed setting of vc_carousel
function vc_cil_restore_transition_speed(element_id){
// after inspecting the original source code the value of 600 is defined there so we put back the original here
jQuery('#' + element_id).data('vc.carousel').transition_speed = 600; 
}

// init     
jQuery(document).ready(function(){    
  // find all vc_carousel with the defined class and turn them into infine loop
  jQuery('.vc_custominfiniteloop').find('div[data-ride="vc_carousel"]').each(function(){
    // allow time for the slider to be built on the page
    // because the slider is "long" we can wait a bit before adding images and events needed  
    var vc_cil_element = jQuery(this).prop("id");
    setTimeout("vc_custominfiniteloop_init('"+vc_cil_element+"')",2000);      
  });    
});

Si tiene problemas, intente ponerlo en cola con PHP_INT_MAX y dependiendo de jQuery, ejemplo:

function enqueue_my_scripts()
{
    // This example expects you to create a file with the JavaScript above in wp-content/themes/yourtheme/assets/js/infinite_visualcomposer_carousel.js
    wp_enqueue_script('infinite-vs-carousel',  get_stylesheet_directory_uri() . 'assets/js/infinite_visualcomposer_carousel.js', array('jquery'), filemtime(get_stylesheet_directory() . '/assets/js/infinite_visualcomposer_carousel.js'), true);
}
add_action('wp_enqueue_scripts', 'enqueue_my_scripts', PHP_INT_MAX);

Todos los créditos para Humberto Silva, solo transfiero esta respuesta aquí para guardar la solución en caso de que el blog se desconecte.

  • Encontré esto muy útil. Sin embargo, en la función de secuencias de comandos en cola, a la primera referencia a su archivo assets/js/infinite_visualcomposer_carousel.js le falta una barra inclinada

    – combot

    5 de octubre de 2020 a las 2:36


/* Convierta el carrusel de imágenes de Visual Composer en un carrusel de imágenes infinito de Visual Composer Incluya antes de la etiqueta en el encabezado de su tema. php Lea el paso a paso detallado en https://humbertosilva.com/visual-composer-infinite-image-carousel/
*/

    // auxiliary code to create triggers for the add and remove class for later use
    (function($){
    $.each(["addClass","removeClass"],function(i,methodname){
        var oldmethod = $.fn[methodname];
        $.fn[methodname] = function(){
              oldmethod.apply( this, arguments );
              this.trigger(methodname+"change");
              return this;
        }
    });
    })(jQuery);

    // init     
    jQuery(document).ready(function(){    
      // find all vc_carousel with the defined class and turn them into infine loop
      jQuery('.vc_custominfiniteloop').find('div[data-ride="vc_carousel"]').each(function(){
        // allow time for the slider to be built on the page
        // because the slider is "long" we can wait a bit before adding images and events needed  
        var vc_cil_element = jQuery(this).prop("id");
        
        setTimeout(vc_custominfiniteloop_init(vc_cil_element),3000);      
      });    
    });

    // main function for the infinite loop
    function vc_custominfiniteloop_init(vc_cil_element_id){

      var vc_element="#" + vc_cil_element_id; // because we're using this more than once let's create a variable for it
     
      window.maxItens = jQuery(vc_element).data('per-view'); // max visible items defined
      window.addedItens = 0; // auxiliary counter for added itens to the end 

      // go to slides and duplicate them to the end to fill space
      jQuery(vc_element).find('.vc_carousel-slideline-inner').find('.vc_item').each(function(){
        // we only need to duplicate the first visible images
        if (window.addedItens < window.maxItens) {
          if (window.addedItens == 0 ) {
            // the fisrt added slide will need a trigger so we know it ended and make it "restart" without animation
            jQuery(this).clone().addClass('vc_custominfiniteloop_restart').removeClass('vc_active').appendTo(jQuery(this).parent());            
          } else {
            jQuery(this).clone().removeClass('vc_active').appendTo(jQuery(this).parent());         
          }
          window.addedItens++;
        }
      });

      // add the trigger so we know when to "restart" the animation without the user knowing about it
      jQuery('.vc_custominfiniteloop_restart').bind('addClasschange', null, function(){

        // navigate to the carousel element , I know, its ugly ...
        var vc_carousel = jQuery(this).parent().parent().parent().parent();

        // first we temporarily change the animation speed to zero
        jQuery(vc_carousel).data('vc.carousel').transition_speed = 0;

        // make the slider go to the first slide without animation and because the fist set of images shown
        // are the same that are being shown now the slider is now "restarted" without that being visible 
        jQuery(vc_carousel).data('vc.carousel').to(0);

        // allow the carousel to go to the first image and restore the original speed 
        setTimeout(vc_cil_restore_transition_speed(jQuery(vc_carousel).prop('id')),5000);
      });

      // // restore original speed setting of vc_carousel
    function vc_cil_restore_transition_speed(element_id){
    // after inspecting the original source code the value of 600 is defined there so we put back the original here
    jQuery('#' + element_id).data('vc.carousel').transition_speed = 5000; 
    }

    }

Todos los créditos para Humberto Silva, solo transfiero esta respuesta aquí para guardar la solución en caso de que el blog se desconecte.

Solo ha hecho algunas correcciones en este código de acuerdo con mi necesidad. Así que no te molestes en preguntar por qué pego el mismo código….

¿Ha sido útil esta solución?