首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Visual Composer图像轮播未正确循环

Visual Composer图像轮播未正确循环
EN

Stack Overflow用户
提问于 2016-09-19 16:29:00
回答 2查看 6.7K关注 0票数 7

这是我的第一篇文章,所以请原谅我的英语。

我有一个图像旋转木马VC组件的问题。我需要设置一些类似于标识类型的滑块(5个标识在一行中),当我设置它显示6个图像中的5个时,末尾有一个间隙(比如4个空格)。

我的设置:

添加了6张图片,每张幻灯片: 5张,滑块循环:是

目标是设置要循环的嘉年华,图像之间没有任何空格。

还有一个响应性的问题。当我调整浏览器窗口图像的大小时,会丢失它们的比例(宽度是缩放百分比,高度是固定的)。

有人能帮我解决这个问题吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-11-09 01:58:53

将Humberto Silva的post移植到Stack Overflow答案中,似乎没有内置的方法可以做到这一点,但JavaScript中有一个变通方法。

首先,向carousel元素添加一个额外的类vc_custominfiniteloop

然后,在加载jQuery和Visual Composer JavaScript之后添加以下javascript代码:

代码语言:javascript
复制
/*
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);      
  });    
});

如果遇到问题,请尝试使用PHP_INT_MAX将其排队,并根据jQuery进行排队,例如:

代码语言:javascript
复制
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);

所有的功劳都归功于Humberto Silva,我只是将这个答案移植到这里来保存解决方案,以防博客离线。

票数 16
EN

Stack Overflow用户

发布于 2021-07-08 00:01:38

/*将Visual Composer Image Carousel转变为Visual Composer无限图像Carousel在您主题的header.php标签之前包含请在https://humbertosilva.com/visual-composer-infinite-image-carousel/上阅读详细的分步操作*/

代码语言:javascript
复制
    // 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; 
    }

    }

所有的功劳都归功于Humberto Silva,我只是将这个答案移植到这里来保存解决方案,以防博客离线。

只是根据我的需要对这段代码做了一些修复。所以不用费心问我为什么要粘贴相同的代码...

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39568588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档