diff --git a/data/swiper.json b/data/swiper.json new file mode 100644 index 0000000..52718d4 --- /dev/null +++ b/data/swiper.json @@ -0,0 +1,8 @@ +{ + "option1": { + "loop": true + }, + "option2": { + "loop": false + } +} \ No newline at end of file diff --git a/layouts/shortcodes/swiper.html b/layouts/shortcodes/swiper.html index 63dea68..68e0d1c 100644 --- a/layouts/shortcodes/swiper.html +++ b/layouts/shortcodes/swiper.html @@ -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); + }); \ No newline at end of file diff --git a/layouts/shortcodes/swiperItem.html b/layouts/shortcodes/swiperItem.html index f35cb98..e39385e 100644 --- a/layouts/shortcodes/swiperItem.html +++ b/layouts/shortcodes/swiperItem.html @@ -1 +1,5 @@ -
\ No newline at end of file + \ No newline at end of file