swiper shortcode - options, padding

#232
This commit is contained in:
zzossig 2020-03-18 18:57:28 +09:00
parent 4651968bfe
commit 4875f1d702
3 changed files with 45 additions and 13 deletions

8
data/swiper.json Normal file
View File

@ -0,0 +1,8 @@
{
"option1": {
"loop": true
},
"option2": {
"loop": false
}
}

View File

@ -27,21 +27,41 @@
'use strict';
document.addEventListener('DOMContentLoaded', function () {
{{ $options := .Params }}
var options = JSON.parse({{ $options | jsonify }});
{{ $swiper := .Site.Data.swiper }}
var options = JSON.parse({{ $swiper | jsonify }});
{{ $optionName := .Params.option }}
var optionName = JSON.parse({{ $optionName | jsonify }});
var id = JSON.parse({{ $id | jsonify }});
var enableNavigation = JSON.parse({{ $enableNavigation | jsonify }});
var enablePagination = JSON.parse({{ $enablePagination | jsonify }});
var mySwiper = new Swiper('#' + id, {
...options,
pagination: {
el: enablePagination ? '.swiper-pagination' : null,
},
navigation: {
nextEl: enableNavigation ? '.swiper-button-next' : null,
prevEl: enableNavigation ? '.swiper-button-prev' : null,
},
});
if (optionName) {
var parsedOptions = Object.assign({}, options[optionName]);
}
if (optionName && enablePagination) {
parsedOptions.pagination = {
el: '.swiper-pagination',
};
}
if (optionName && enableNavigation) {
parsedOptions.navigation = {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
};
}
if (enableNavigation) {
var allSlideElem = document.querySelectorAll('.swiper-slide__inner');
allSlideElem ?
allSlideElem.forEach(function (slideElem) {
slideElem.style.padding = '0 40px';
}) : null;
}
var mySwiper = new Swiper('#' + id, parsedOptions);
});
</script>

View File

@ -1 +1,5 @@
<div class="swiper-slide" data-align="{{ .Get "align" | default "center" }}">{{ .Inner | markdownify }}</div>
<div class="swiper-slide" data-align="{{ .Get "align" | default "center" }}">
<div class="swiper-slide__inner">
{{ .Inner | markdownify }}
</div>
</div>