handles multiple carousels on same page

This commit is contained in:
buttle 2021-09-13 14:37:47 +02:00
parent 1259955cb8
commit 4aaeddc487
2 changed files with 29 additions and 23 deletions

View File

@ -3,7 +3,7 @@
flex-wrap: nowrap !important; flex-wrap: nowrap !important;
} }
.siema { .siema {
margin: 1rem 0; margin: margin: 0; /*1rem 0;*/
background-color: #000; background-color: #000;
position: relative; position: relative;
} }
@ -12,9 +12,10 @@
display: block; display: block;
margin-left: auto; margin-left: auto;
margin-right: auto; margin-right: auto;
width: 100%;
} }
#siema-ui { .siema-ui {
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
position: absolute; position: absolute;
@ -33,11 +34,11 @@
opacity: 1; opacity: 1;
} }
.caret#left { .caret-left {
margin-left: 20px; margin-left: 20px;
} }
.caret#right { .caret-right {
transform: rotate(180deg); transform: rotate(180deg);
margin-right: 20px; margin-right: 20px;
} }

View File

@ -7,10 +7,8 @@
?> ?>
<style> <style>
.siema { margin: 0; } #siema-ui_<?= $carousel_id ?> {
.siema-img { width: 100%; } background-color: <?php echo $ui_background ?>;
#siema-ui {
/* background-color: <?php echo $ui_background ?>; */
} }
</style> </style>
@ -23,7 +21,8 @@
<?php } ?> <?php } ?>
<div style="position:relative;"> <div style="position:relative;">
<div class="siema_<?= $carousel_id ?> siema"> <div id="siema_<?= $carousel_id ?>"
class="siema">
<?php foreach ($images as $image) { ?> <?php foreach ($images as $image) { ?>
<div class="siema-wrap" <div class="siema-wrap"
style="<?= $wrapStyle ?>" style="<?= $wrapStyle ?>"
@ -35,7 +34,9 @@
</div> </div>
<?php } ?> <?php } ?>
</div> </div>
<div id="siema-ui"></div> <div id="siema-ui_<?= $carousel_id ?>"
class="siema-ui">
</div>
</div> </div>
<?php if ($title) { ?> <?php if ($title) { ?>
@ -47,14 +48,14 @@
<script> <script>
var sourceURLs = []; var sourceURLs_<?= $carousel_id ?> = [];
<?php foreach ($images as $image) { ?> <?php foreach ($images as $image) { ?>
sourceURLs.push("<?= $image['item_url'] ?>") sourceURLs_<?= $carousel_id ?>.push("<?= $image['item_url'] ?>")
<?php } ?> <?php } ?>
$(document).ready(function() { $(document).ready(function() {
let siema = new Siema({ let siema_<?= $carousel_id ?> = new Siema({
selector: '.siema_<?= $carousel_id ?>', selector: '#siema_<?= $carousel_id ?>',
easing: 'ease-out', easing: 'ease-out',
duration: <?php echo $duration ?>, duration: <?php echo $duration ?>,
perPage: <?php echo $perPage ?>, perPage: <?php echo $perPage ?>,
@ -64,14 +65,18 @@ $(document).ready(function() {
onChange: changeSourceURL, onChange: changeSourceURL,
}); });
let caret = '<?php echo $caret;?>'; let caret = $('<?php echo $caret;?>');
let caret_r = '<?php echo $caret_r;?>'; $(caret).prop('id', '<?= $carousel_id ?>_left')
$(caret).addClass('caret-left');
let caret_r = $('<?php echo $caret_r;?>');
$(caret_r).prop('id', '<?= $carousel_id ?>_right')
$(caret_r).addClass('caret-right');
$('#siema-ui').append(caret); $('#siema-ui_<?= $carousel_id ?>').append(caret);
$('#siema-ui accesskey=""').append(caret_r); $('#siema-ui_<?= $carousel_id ?>').append(caret_r);
$('.caret#left').click(function () {siema.prev()}); $('#<?= $carousel_id ?>_left').click(function () {siema_<?= $carousel_id ?>.prev()});
$('.caret#right').click(function () {siema.next()}); $('#<?= $carousel_id ?>_right').click(function () {siema_<?= $carousel_id ?>.next()});
<?php if ($autoSlide == true): ?> <?php if ($autoSlide == true): ?>
window.setInterval(() => { window.setInterval(() => {
@ -83,14 +88,14 @@ $(document).ready(function() {
function changeSourceURL(){ function changeSourceURL(){
var slide_idx = this.currentSlide var slide_idx = this.currentSlide
if (slide_idx < 0) { if (slide_idx < 0) {
slide_idx = sourceURLs.length -1; slide_idx = sourceURLs_<?= $carousel_id ?>.length -1;
} }
var source_link = $(".siema_<?= $carousel_id ?>") var source_link = $("#siema_<?= $carousel_id ?>")
.closest(".archive-carousel") .closest(".archive-carousel")
.find(".source-link") .find(".source-link")
.find('a'); .find('a');
if (source_link.length != 0) { if (source_link.length != 0) {
$(source_link).attr("href", sourceURLs[slide_idx]) $(source_link).attr("href", sourceURLs_<?= $carousel_id ?>[slide_idx])
} }
} }