带滚动条的 Swiper 自定义分页

Swiper Custom Pagination With Scrollbar

我正在尝试使用 Swiper 4.0.6 创建自定义分页滚动条。

我想要类似于滚动条中途的东西 this page。请参阅每个滚动部分上方的标题。

根据 API 我可以为此使用 renderCustom 函数。我似乎无法让它工作。我在屏幕上看不到分页,尽管滑动器工作正常。

任何人都可以帮助解决这个问题吗?

到目前为止我的代码;

HTML

<div class="swiper-container">
    <div class="swiper-wrapper">
        <div class="swiper-slide" data-name="Item 1">
            <p>Testing</p>
        </div>
        <div class="swiper-slide" data-name="Item 2">
            <p>Testing</p>
        </div>
        <div class="swiper-slide" data-name="Item 3">
            <p>Testing</p>
        </div>
    </div>
    <div class="swiper-pagination1"></div>
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
</div>

JS

var names = [];
$(".swiper-wrapper .swiper-slide").each(function(i) {
    names.push($(this).data("name"));
});
var swiper1 = new Swiper('.swiper-container', {
    pagination: {
        el: '.swiper-pagination1',
        type: 'custom',
    },
    navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
    },
    renderCustom: function(swiper, current, total) {
        var text = "<span class='pContainer' style='background-color:transperent;text-align: center;width:100%; display:block'>";
        for (let i = 1; i <= total; i++) {
            if (current == i) {
                text += "<span style='display:inline-block;border-top:3px solid #afd869;text-align:left;margin-right:4px;width: 20%;color:#afd869;padding:5px;'>" + names[i] + "</span>";
            } else {
                text += "<span style='display:inline-block;border-top:3px solid white;text-align:left; margin-right:4px;width: 20%;color:white;padding:5px;'>" + names[i] + "</span>";
            }
        }
        text += "</span>";
        return text;
    }
});

如有任何建议,我们将不胜感激。

您必须像这样将 renderCustom 函数放入分页中:

    var names = [];
$(".swiper-wrapper .swiper-slide").each(function(i) {
    names.push($(this).data("name"));
});
var swiper1 = new Swiper('.swiper-container', {
    pagination: {
        el: '.swiper-pagination1',
        type: 'custom',
    renderCustom: function(swiper, current, total) {
        var text = "<span class='pContainer' style='background-color:transperent;text-align: center;width:100%; display:block'>";
        for (let i = 1; i <= total; i++) {
                //alert(total);
            if (current == i) {

                text += "<span style='display:inline-block;border-top:3px solid #afd869;text-align:left;margin-right:4px;width: 20%;color:#afd869;padding:5px;'>" + names[i-1] + "</span>";

            } 

            else {

                text += "<span style='display:inline-block;border-top:3px solid white;text-align:left; margin-right:4px;width: 20%;color:white;padding:5px;'>" + names[i] + "</span>";
            }
        }
        text += "</span>";
        return text;
    }
    },
    navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
    }
});

并将样式设置为 span.swiper-pagination-custom,如下所示:

.swiper-pagination-custom{
   position: absolute;
   bottom: 0;
   z-index: 10;
}