简体   繁体   中英

How to add Ease-in-out effect with JavaScript

Well, I'm new at javascript, I'm learning. Now I want to add an effect of ease in this code..how do I do that?

let slideIndex = 0;

const showSlides = () => {
    const slides = document.getElementsByClassName("slide");

    for(let i = 0; i < slides.length; i++){
    slides[i].style.display = "none";
    }

    slideIndex++;

    if(slideIndex > slides.length){
    slideIndex = 1;
    }

    slides[slideIndex - 1].style.display = "block";
    setTimeout(showSlides,3000);
};

showSlides();

You can do it using css:

.slide {
  transition: opacity 1s ease;
}

For more information check: MDN - Using CSS transitions

And then instead of switching display, you switch opacity:

slides[i].style.opacity = 0; // hide

slides[i].style.opacity = 1; // show

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM