gpt4 book ai didi

javascript - 带滚动条的 Swiper 自定义分页

转载 作者:行者123 更新时间:2023-11-28 15:14:56 25 4
gpt4 key购买 nike

我正在尝试使用 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;
}

关于javascript - 带滚动条的 Swiper 自定义分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47455105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com