gpt4 book ai didi

javascript - 事件监听器在 Owl Carousel 中不起作用

转载 作者:行者123 更新时间:2023-12-02 14:27:46 25 4
gpt4 key购买 nike

我想听事件,但它对我不起作用正如你所看到的,我尝试使用 console.log(pages);听页数,但当我打开控制台时,我没有得到任何响应

var owl = $('.owl-carousel');


owl.owlCarousel({

onDragged: callback,

animateOut: 'fadeOut',
animateIn: 'fadeIn',
items: 1,
loop: true,
autoplay: false,
nav: true,
dots: true
});
function callback(event) {

var pages = event.page.count; // Number of pages

console.log(pages);

}

你能帮忙吗?

最佳答案

回调

您还可以只向 Owl Carousel 的选项添加回调,而不是附加事件处理程序。

$('.owl-carousel').owlCarousel({
onDragged: callback
});
function callback(event) {
...
}

数据

每个事件都会在事件对象内传递非常有用的信息。基于上面的例子:

function callback(event) {
// Provided by the core
var element = event.target; // DOM element, in this example .owl-carousel
var name = event.type; // Name of the event, in this example dragged
var namespace = event.namespace; // Namespace of the event, in this example owl.carousel
var items = event.item.count; // Number of items
var item = event.item.index; // Position of the current item
// Provided by the navigation plugin
var pages = event.page.count; // Number of pages
var page = event.page.index; // Position of the current page
var size = event.page.size; // Number of items per page
}

来自:

http://www.owlcarousel.owlgraphic.com/docs/api-events.html

你的例子对我来说看起来不错,只需打印前后,以确保你的属性(property)将被 owlCarroussel 拿走。您添加了正确的 jQuery header 吗?

<a href="http://jquery.com/">jQuery</a>
<script src="jquery.js"></script>
<script>
var owl = $('.owl-carousel');

alert('before')
owl.owlCarousel({

onDragged: callback,

animateOut: 'fadeOut',
animateIn: 'fadeIn',
items: 1,
loop: true,
autoplay: false,
nav: true,
dots: true
});
alert('After')
function callback(event) {

var pages = event.page.count; // Number of pages

console.log(pages);

}
</script>

关于javascript - 事件监听器在 Owl Carousel 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38076018/

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