猫头鹰Carousel 2随机function

有没有办法在猫头鹰旋转木马2中制作一个王随机function。 我需要页面上的幻灯片随机加载。

在旧的Owl Carousel版本之前,我这样做了:

$(document).ready(function () { //Sort random function function random(owlSelector) { owlSelector.children().sort(function () { return Math.round(Math.random()) - 0.5; }).each(function () { $(this).appendTo(owlSelector); }); } $(".feedback").owlCarousel({ autoPlay: 5000, slideSpeed: 200, items: 1, itemsDesktop: [1199, 1], itemsDesktopSmall: [979, 1], itemsTablet: [768, 1], itemsMobile: [479, 1], autoHeight: true, //Call beforeInit callback, elem parameter point to $(".feedback") beforeInit: function (elem) { random(elem); } }); }); 

如何在Owl Carousel 2中以最佳方式完成此操作?

你必须使用新的onInitialize回调,如下所示:

 var owl = $('.owl-carousel'); owl.owlCarousel({ onInitialize : function(element){ owl.children().sort(function(){ return Math.round(Math.random()) - 0.5; }).each(function(){ $(this).appendTo(owl); }); }, }); 

在2.x文档中查找更多信息。

出于某种原因,AdmireNL的答案给了我iOS设备的问题但是对其他一切都很有效。 不知道为什么会这样,但我通过使用这里列出的最佳答案解决了我的问题: 如何随机排序列表项?

我的最终代码:

 $.fn.randomize = function(selector){ var $elems = selector ? $(this).find(selector) : $(this).children(), $parents = $elems.parent(); $parents.each(function(){ $(this).children(selector).sort(function(){ return Math.round(Math.random()) - 0.5; }).detach().appendTo(this); }); return this; }; var slider = $('#slider'); slider.owlCarousel({ onInitialize : function(){ $(slider).randomize(); } });