Image carousel widget improvements

This commit is contained in:
Fabio Manganiello 2018-05-07 09:54:30 +02:00
parent 2483c6d612
commit af11358a76
3 changed files with 32 additions and 7 deletions

View file

@ -1,8 +1,16 @@
.carousel {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
background-color: black;
background-size: auto 100%;
background-position: center;
background-repeat: no-repeat;
color: #999;
}
.carousel > img {
flex-shrink: 0;
max-height: 100%;
border-radius: 10px;
height: 22.5em;
}

View file

@ -1,5 +1,5 @@
$(document).ready(function() {
var $imgElement = $('.image-carousel').find('.carousel'),
var $imgContainer = $('.image-carousel').find('.carousel'),
config = window.widgets['image-carousel'],
images = config.imageUrls,
processedImages = 0;
@ -14,7 +14,22 @@ $(document).ready(function() {
};
var refreshImage = function() {
$imgElement.css('background-image', "url('" + images[processedImages++] + "')");
var $oldImg = $imgContainer.find('img');
var $newImg = $imgContainer.find('img').clone()
.attr('src', images[processedImages++])
.hide().appendTo($imgContainer);
if ($newImg.width() > $newImg.height()) {
$newImg.css('min-width', '100%');
} else {
$newImg.css('min-width', '');
}
$newImg.on('load', function() {
$oldImg.remove();
$newImg.fadeIn();
});
if (processedImages == images.length-1) {
shuffleImages();
processedImages = 0;

View file

@ -8,5 +8,7 @@
window.widgets['image-carousel'].imageUrls = {{ images|safe }};
</script>
<div class="carousel"></div>
<div class="carousel">
<img alt="Your carousel images">
</div>