简体   繁体   English

如何使用 JavaScript 添加缓入淡出效果

[英]How to add Ease-in-out effect with JavaScript

Well, I'm new at javascript, I'm learning.好吧,我是 javascript 新手,我正在学习。 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:您可以使用 css 来做到这一点:

.slide {
  transition: opacity 1s ease;
}

For more information check: MDN - Using CSS transitions有关更多信息,请查看: MDN - 使用 CSS 转换

And then instead of switching display, you switch opacity:然后不是切换显示,而是切换不透明度:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用具有过渡缓入效果的JavaScript隐藏/显示div? - How to hide/show the div using javascript with transition ease-in-out effect? 如何将宽度缓入过渡应用到元素? - How to apply width ease-in-out transition to element? 使用jQuery添加缓动效果 - Add ease out effect with jQuery CSS3 + Javascript - 将-ms-transition:opacity 1s easy-in-out; 仅在IE 10中工作? - CSS3 + Javascript - Will -ms-transition:opacity 1s ease-in-out; work in IE 10 alone? 如何在Java脚本上实现淡入动画,例如CSS上的Easy-in-out? - How to implement fade-in-out animation on java-script like ease-in-out on css? 我如何使用淡入/淡入淡出/或任何动画来平滑显示我的 HTML、CSS 和 JS 导航菜单? - How do I use fade-in/ease-in-out/or any animation to smooth out the revealing of my HTML, CSS and JS navigation menu? 为 animation 添加“缓出”选项 - Add an "Ease out" option to the animation 如何用Javascript逐渐改变属性值? 例如。 缓入缓出 - How to gradually change attribute values with Javascript? eg. Ease-in, Ease-out 有没有一种方法可以向jQuery添加平滑/轻松过渡效果? - Is there a way to add a smooth/ease transition effect to jQuery? 有没有办法在不添加类的情况下在 js 中添加过渡效果(平滑/缓和)? - Is there a way to add transition effect (smooth/ease) in js without adding a class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM