gpt4 book ai didi

javascript - Owl Carousel 2 找到中心项目

转载 作者:行者123 更新时间:2023-12-02 22:15:49 25 4
gpt4 key购买 nike

我想在owl carousel 2( native )中找到center项目,而不是像$('.owl-item.center'那样通过class选择) 我想通过 owl carousel 原生函数和结果找到它,现在我可以获取事件,并且有一堆结果,我可以获取与 center 相关的任何值

$('.owl-carousel').owlCarousel({
center: true,
items: 3,
loop: false,
margin: 10,
});

$('.owl-carousel').on("dragged.owl.carousel", function(e) {
console.log(e);
if (e.itemClass === 'center') {
alert('it is center one!');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.min.css" integrity="sha256-UhQQ4fxEeABh4JrcmAJ1+16id/1dnlOEVCFOxDef9Lw=" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js" integrity="sha256-pTxD+DSzIwmwhOqTFN+DB+nHjO4iAsbgfyFq5K5bcE0=" crossorigin="anonymous"></script>

<div class="owl-carousel">
<div class="item">
<h4>1</h4>
</div>
<div class="item">
<h4>2</h4>
</div>
<div class="item">
<h4>3</h4>
</div>
<div class="item">
<h4>4</h4>
</div>
<div class="item">
<h4>5</h4>
</div>
<div class="item">
<h4>6</h4>
</div>
</div>

如何在 native 中检测 owl carousel 2 中心项目?再次,我不想通过 center 类使用 find each 或任何其他选择器来查找 jquery find items,我想猫头鹰给它对我来说。

最佳答案

我不认为 Owl Carousel 提供这样的选项来获取事件结果中的中心项目。你可以像这样做这个有点棘手的事情,只需获取e.item.index它是当前事件的项目,然后+1来获取居中的项目。检查下面的示例:

$('.owl-carousel').owlCarousel({
center: true,
items: 3,
loop: false,
margin: 10,
});

$('.owl-carousel').on("dragged.owl.carousel", function(e) {
console.log('center item is:'+ (e.item.index + 1));
});
.item {
border: 1px solid;
text-align: center;
}

.owl-item.active.center {
background: gray;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.min.css" integrity="sha256-UhQQ4fxEeABh4JrcmAJ1+16id/1dnlOEVCFOxDef9Lw=" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js" integrity="sha256-pTxD+DSzIwmwhOqTFN+DB+nHjO4iAsbgfyFq5K5bcE0=" crossorigin="anonymous"></script>

<div class="owl-carousel">
<div class="item">
<h4>1</h4>
</div>
<div class="item">
<h4>2</h4>
</div>
<div class="item">
<h4>3</h4>
</div>
<div class="item">
<h4>4</h4>
</div>
<div class="item">
<h4>5</h4>
</div>
<div class="item">
<h4>6</h4>
</div>
</div>

关于javascript - Owl Carousel 2 找到中心项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59388184/

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