gpt4 book ai didi

javascript - 在放大弹出回调函数中使用 data 属性定义 owl carousel

转载 作者:行者123 更新时间:2023-12-02 22:31:21 24 4
gpt4 key购买 nike

我在Magnific Popup中实现了Owl carousel。并且,它正常工作正常。如果,我使用简单的函数 $('.owl-carousel').owlCarousel({autoplay:true, items:1})

但是,当我使用数据属性定义 Owl 轮播时,Owl 轮播不起作用。

HTML:

<a class="ajax-popup btn btn-dark" href="ajaxproject.html">Click To Open Popup</a>

ajaxproject.html 文件:

<div class="container ajax-container">
<h2 class="text-7 text-center mb-4">Title 1</h2>
<div class="row">
<div class="col-sm-7">
<div class="owl-carousel owl-theme" data-autoplay="true" data-items="1">
<div class="item"> <img class="img-fluid" alt="" src="images/bg/image-6.jpg"> </div>
<div class="item"> <img class="img-fluid" alt="" src="images/bg/image-5.jpg"> </div>
</div>
</div>
<div class="col-sm-5">
<h4 class="text-4 font-weight-600">Description:</h4>
<p>Lisque persius interesset his et, in quot quidam persequeris vim, ad mea essent possim iriure. Lisque persius interesset his et, in quot quidam persequeris vim, ad mea essent possim iriure.</p>
</div>
</div>
</div>

使用数据属性定义猫头鹰轮播:但是,这不起作用。这里出了什么问题?

$(".ajax-popup").magnificPopup({
type: "ajax",
mainClass: "mfp-fade",
closeBtnInside: true,
gallery: {
enabled: true,
},
callbacks: {
ajaxContentAdded: function() {

$(".owl-carousel").each(function (index) {
var items = $(this).data('slides');
var autoplay = $(this).data('autoplay');
$(this).owlCarousel({
items: items,
autoplay: autoplay
});
});

}
}
});

工作正常。如果我使用没有数据属性的定义猫头鹰轮播:

$(".ajax-popup").magnificPopup({
type: "ajax",
mainClass: "mfp-fade",
closeBtnInside: true,
gallery: {
enabled: true,
},
callbacks: {
ajaxContentAdded: function() {

$('.owl-carousel').owlCarousel({
autoplay:true,
items:1
})

}
}
});

最佳答案

要访问 data-* 属性,您需要使用 .attr() 而不是 .data()。您的代码应该是:

$(".ajax-popup").magnificPopup({
type: "ajax",
mainClass: "mfp-fade",
closeBtnInside: true,
gallery: {
enabled: true,
},
callbacks: {
ajaxContentAdded: function() {

$(".owl-carousel").each(function (index) {
var items = $(this).attr('data-items');
var autoplay = $(this).attr('data-autoplay');
$(this).owlCarousel({
items: items,
autoplay: autoplay
});
});

}
}
});

关于javascript - 在放大弹出回调函数中使用 data 属性定义 owl carousel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58890465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com