javascript Owlcarousel 2 动态加载内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25383792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Owlcarousel 2 dynamically loaded content
提问by Arkadiusz Rosiak
I have troubles with ajax loading content to owl carousel 2. I'm trying to get new items by this function:
我在将 ajax 内容加载到 owl carousel 2 时遇到了麻烦。我正在尝试通过此功能获取新项目:
function loadDecisions(pageNumber) {
$.ajax({
url: globalURL,
type: 'POST',
data: {
action: 'lw_infinite_scroll',
loop_file: 'video',
page_no: pageNumber,
posts_per_page: postsPerPage
},
success: function(data) {
$(".owl-stage").append(data);
owl = jQuery('.owl-carousel');
count++;
resizeStage();
},
error: function(e) {
}
});
return false;
}
If I append new items to .owl-stage like this $(".owl-stage").append(data);
I can see them, but I can't slide to them. I think owlcarousel think that there still are only 5 items, even if i added much more.
如果我像这样将新项目附加到 .owl-stage 中,$(".owl-stage").append(data);
我可以看到它们,但我无法滑动到它们。我认为 owlcarousel 认为仍然只有 5 个项目,即使我添加了更多。
The situation changes if i change $(".owl-stage").append(data);
to $(".owl-stage").html(data);
. Then obviously old content disapears, but I can slide to new items.
如果我改为$(".owl-stage").append(data);
,情况就会改变$(".owl-stage").html(data);
。然后显然旧内容消失了,但我可以滑动到新项目。
Can anyone suggest what I should do? I'm using owlcarousel 2.0.4.
谁能建议我应该做什么?我正在使用 owlcarousel 2.0.4。
回答by Alex C
For me the accepted answer did not work, but this solution did the job:
对我来说,接受的答案不起作用,但这个解决方案完成了工作:
$('.owl-carousel').owlCarousel().trigger('add.owl.carousel',
[jQuery('<div class="owl-item">' + html +
'</div>')]).trigger('refresh.owl.carousel');
This because, at least on the Owl Carousel v2 of today needs a jQuery object to be passed to "add".
这是因为,至少在今天的 Owl Carousel v2 上需要一个 jQuery 对象来传递给“add”。
See Github ticket #1170where Gurunave explains this best.
请参阅 Github 票号#1170,Gurunave 对此进行了最好的解释。
回答by witrin
Don't add items by using jQuery on a running carousel. You should use the add
method instead like this:
不要在正在运行的轮播上使用 jQuery 添加项目。您应该add
像这样使用该方法:
$('.owl-carousel').owlCarousel('add', '<markup>').owlCarousel('refresh')
Please notice that you need the latest develop for this: https://github.com/OwlFonk/OwlCarousel2#building.
请注意,您需要为此进行最新开发:https: //github.com/OwlFonk/OwlCarousel2#building。