猫头鹰旋转木马2自定义导航

owl carousel 2 custom nav

本文关键字:导航 自定义 旋转木马 猫头鹰      更新时间:2023-09-26

我正在使用owl carousel 1插件,我正试图将我所有的东西升级到版本2。我有一个带有自定义导航条的滑块:

$(document).ready(function () {
    function customPager() {
        $.each(this.owl.userItems, function (i) {
            var titleData = jQuery(this).find('img').attr('title');
            var paginationLinks = jQuery('.owl-controls .owl-pagination .owl-page span');
            $(paginationLinks[i]).append(titleData);
        });
    }
    $('.rri-carousel').owlCarousel({
        navigation      : false, // Show next and prev buttons
        slideSpeed      : 300,
        paginationSpeed : 400,
        singleItem      : true,
        lazyLoad        : true,
        afterInit       : customPager,
        afterUpdate     : customPager,
        transitionStyle : "fade",
        autoPlay        : true,
        stopOnHover     : true
    });
});

如何将此转换为猫头鹰旋转木马2?我现在有:

$(document).ready(function () {
    function customPager() {
        $.each(this.owl.userItems, function (i) {
            var titleData = jQuery(this).find('img').attr('title');
            var paginationLinks = jQuery('.owl-controls .owl-pagination .owl-page span');
            $(paginationLinks[i]).append(titleData);
        });
    }
    $('.rri-carousel').owlCarousel({
        loop          : true,
        items         : 1,
        nav           : false,
        onInitialized : customPager,
        onResized     : customPager
    });
});

然而,我得到Uncaught TypeError: Cannot read property 'userItems' of undefined在铬控制台,当我点击它,它显示错误是在this.owl.userItems上的$.each(this.owl.userItems, function (i) {

我假设函数在更新中被删除以更改,但我无法弄清楚这一点,我是javascript的新手。

我不仅得到这个错误,而且没有看到像第一个版本那样的分页。

谢谢,希望有人能给我指出正确的方向。

编辑:

我复制了你的javascript,但仍然没有分页。这是我的php输出的html:

function rri_function($type = 'rri_function') {
    $args = array(
        'post_type'      => 'rri_images',
        'posts_per_page' => 5
    );
    $result = '<div class="rri-carousel owl-theme">';
    //the loop
    $loop = new WP_Query($args);
    while ($loop->have_posts()) {
        $loop->the_post();
        $the_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), $type);
        $result .= '<div><img src="' . $the_url[0] . '" title="' . get_the_title() . '" data-thumb="' . $the_url[0] . '" alt="' . get_the_title() . '"></div>';
    }
    $result .= '</div>';
    return $result;
}

,下面是打印在页面上的内容:

上一页下一个div>

我是新的javascript,但这为我工作。Owl carousel 2有不同的CSS类:。owl-dots .owl-dot而不是

我也改变了this.owl。userItems$('.owl-item')选择每个carousel项目(不确定这是否是合适的解决方案)。

$(document).ready(function () {
    function customPager() {
        $.each($('.owl-item'), function (i) {
            var titleData = jQuery(this).find('img').attr('title');
            var paginationLinks = jQuery('.owl-dots .owl-dot span');
            $(paginationLinks[i]).append(titleData);
    });
}
$('.rri-carousel').owlCarousel({
        loop          : true,
        items         : 1,
        nav           : false,
        onInitialized : customPager,
     });
});

编辑:我已经删除了onResized: customPager参数,因为它在调整浏览器窗口大小时反复添加现有链接后的新链接。然而,我不清楚这个参数的目的是什么。

该错误表明'owl'没有在'this'即customPager函数上定义。实际上,在你的代码中没有任何地方定义名为'owl'的变量。我查了一下owl-carousel 2.0的文档,我猜你需要这样的东西。

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

和我在文档中没有看到任何名为'userItems'的变量。但是您可以使用jquery访问这些元素。从长远来看,

$(document).ready(function () {
    var owl = $('.rri-carousel');
    function customPager() {
        $.each($(owl).find('.item'), function (i) {
            var titleData = jQuery(this).find('img').attr('title');
            var paginationLinks = jQuery('.owl-controls .owl-pagination .owl-page span');
            $(paginationLinks[i]).append(titleData);
        });
    }
    owl.owlCarousel({
        loop          : true,
        items         : 1,
        nav           : false,
        onInitialized : customPager,
        onResized     : customPager
    });
});

我不能完全测试它是否工作,因为我没有你的html来测试它,但我在我的机器上没有任何问题。