事件监听器不在owl carousel中工作

时间:2016-06-28 12:16:10

标签: javascript jquery owl-carousel

我想听听活动,但它不适合我 正如你所看到我尝试使用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);

        }



  

你能帮忙吗?

1 个答案:

答案 0 :(得分:2)

<强>回调

除了附加事件处理程序,您还可以添加回调到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

您的示例对我来说很合适,只需打印一次,以确保您的财产将由owlCarroussel拍摄。你添加了正确的jQuery标题吗?

<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>