TheOrdinaryGeek
TheOrdinaryGeek

Reputation: 2323

Swiper Custom Pagination With Scrollbar

I am trying to create a custom pagination scrollbar using Swiper 4.0.6.

I would like something similar to the scroller halfway down this page. See the titles above each scroll section.

According the the API I can use the renderCustom function for this. I can't seem to get it working. I can't see the pagination on screen, although the swiper works fine.

Can anybody help with this issue?

My code so far;

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

Any advice is appreciated.

Upvotes: 1

Views: 6633

Answers (1)

Zayaty
Zayaty

Reputation: 26

You have to put renderCustom function inside pagination like this:

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

and put style to span.swiper-pagination-custom like this:

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

Upvotes: 1

Related Questions