This is my Javascript code -
var slideshows = document.querySelectorAll('[data-component="slideshow"]');
slideshows.forEach(initSlideShow);
function initSlideShow(slideshow) {
var slides = document.querySelectorAll(`#${slideshow.id} [role="list"] .slide`);
var index = 0, time = 10000;
slides[index].classList.add('active');
setInterval( () => {
slides[index].classList.remove('active');
index++;
if (index === slides.length) index = 0;
slides[index].classList.add('active');
}, time);
}
This is my markup -
<?php foreach( $hero_images as $image_id ): ?>
<div class="slide" style="background-image: linear-gradient(rgba(0,0,0, 0.5), rgba(0,0,0, 0.5)), url('<?php echo wp_get_attachment_image_url( $image_id, 'full' ); ?>')">
<div class="text">
<h1>Example</h1>
</div>
</div>
</div>
<?php endforeach; ?>
I have an array of images from Word Press and have them in the background of the parent div.
This is the CSS -
[data-component="slideshow"] .slide {
display: none;
}
[data-component="slideshow"] .slide.active {
display: block;
}
It just toggles the CSS display rule
The code works and the divs transition between display none and display block, but I want the transition to be smoother. I want to add a fade in.
I thought something like this would do it -
.slide {
opacity: 0;
transition: 1s;
}
.active {
opacity: 1 !important;
transition: 1s;
}
But it does not have any affect.
Is it possible to add a delay to the transition of display:none to display:block?
You can use a keyframe-animation to controll the timing of your attributes more accurately.
Here is an example of a fadein-animation (onclick) using keyframes:
I would still recommend you to use a library like jquery or pure javascript for animations like this, since its alot simpler to implement animations in this way. If you have the creative freedom to choose, of course.