如何重新渲染猫头鹰旋转木马项目?

Alf*_*ang 10 html css jquery owl-carousel owl-carousel-2

好吧,我现在正在使用owl-carousel-2插件.

我遇到以下问题:

标记代码:

<div class="owl-carousel" style="display: none;">
    <div class="item"><img src="..." /></div>
    <div class="item"><img src="..." /></div>
    <!-- ... -->
    <div class="item"><img src="..." /></div>
</div>

<script>
$(function() {
    var $owl = $('.owl-carousel');
    $owl.owlCarousel();

    // Doing many things here.

    $owl.show();
});
</script>
Run Code Online (Sandbox Code Playgroud)

问题是:

当我使用$owl.owlCarousel();隐藏状态下的语句初始化时,其大小未初始化.

因此,当我显示该控件时,控件显示一团糟!

但是当我调整窗口大小时,它似乎触发了重新渲染.控件呈现内容,然后显示良好.


所以我想知道是否有办法在它上面触发这种重新渲染(或刷新)方法.

为了确保控件不会显示一团糟.

我试图阅读文档和来源,但还没有一个很好的解决方案.

请帮忙.

Alf*_*ang 36

我发现了一个丑陋,肮脏的解决方案.无论如何,它工作:

主要程序:

  1. 摧毁猫头鹰 - 旋转木马.
  2. 手动将标记更改为初始状态.
  3. 初始化猫头鹰轮播.

var $owl = $('.owl-carousel');
$owl.trigger('destroy.owl.carousel');
// After destory, the markup is still not the same with the initial.
// The differences are:
//   1. The initial content was wrapped by a 'div.owl-stage-outer';
//   2. The '.owl-carousel' itself has an '.owl-loaded' class attached;
//   We have to remove that before the new initialization.
$owl.html($owl.find('.owl-stage-outer').html()).removeClass('owl-loaded');
$owl.owlCarousel({
    // your initial option here, again.
});
Run Code Online (Sandbox Code Playgroud)

它工作,但以这样一个肮脏的方式.希望看到一个更好,更整洁的解决方案.


mst*_*cke 13

这对我有用:

// get owl element
var owl = $('.owl-carousel');

// get owl instance from element
var owlInstance = owl.data('owlCarousel');

// if instance is existing
if(owlInstance != null)
    owlInstance.reinit();
Run Code Online (Sandbox Code Playgroud)

更多信息:http://owlgraphic.com/owlcarousel/demos/manipulations.html

  • 请注意,上述内容仅适用于owlCarousel v1(OP使用的是v2). (3认同)
  • 链接坏了 (2认同)

小智 5

和OP一样陷入同样的​​问题.我设法通过首先删除owl-loaded课程来使其工作.然后在渲染中,在重新添加类后触发刷新事件.

// Remove the owl-loaded class after initialisation 
$owl.owlCarousel().removeClass('owl-loaded');

// Show the carousel and trigger refresh
$owl.show(function(), {
  $(this).addClass('owl-loaded').trigger('refresh.owl.carousel');
})
Run Code Online (Sandbox Code Playgroud)


Cud*_*ead 5

这是我的解决方案,基于fish_ball的巧妙解决方法:

if($('.owl-carousel').hasClass('owl-theme')){ //resize event was triggering an error, this if statement is to go around it

            $('.owl-carousel').trigger('destroy.owl.carousel'); //these 3 lines kill the owl, and returns the markup to the initial state
            $('.owl-carousel').find('.owl-stage-outer').children().unwrap();
            $('.owl-carousel').removeClass("owl-center owl-loaded owl-text-select-on");

            $(".owl-carousel").owlCarousel(); //re-initialise the owl
        }
Run Code Online (Sandbox Code Playgroud)