使用滚动条进行Swiper自定义分页

时间:2017-11-23 12:01:31

标签: javascript jquery html css swiper

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

我想要一半类似于卷轴的this page。请参阅每个滚动部分上方的标题。

根据API,我可以使用renderCustom功能。我似乎无法让它工作。我看不到屏幕上的分页,虽然swiper工作正常。

有人可以帮忙解决这个问题吗?

到目前为止我的代码;

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;
    }
});

感谢任何建议。

1 个答案:

答案 0 :(得分:1)

你必须将renderCustom函数放在这样的分页中:

avis-digital

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

    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',
    }
});