如何在JS代码中添加淡入淡出过渡效果?

huangapple go评论66阅读模式
英文:

How do I add a fade transition into JS code?

问题

我正在尝试使用以下代码来改变/循环一个特定 div 的背景图像。

代码运行正常,唯一我不知道如何在 JS 中实现淡入/淡出的过渡效果。我尝试在 CSS 中实现,但它只会影响到第一次加载第一张图片时,即整个页面加载完成时。

你能帮我实现图片之间的淡入淡出过渡效果吗?

function displayNextImage() {
    x = (x === images.length - 1) ? 0 : x + 1;
    document.getElementById("slider-background").src = images[x];
}

function changeImage() {
    setInterval(displayNextImage, 5000);
}

var images = [], x = -1;
images[0] = "https://www.example.com/1.jpg";
images[1] = "https://www.example.com/2.jpg";
images[2] = "https://www.example.com/3.jpg";
英文:

I'm trying to get the background image of a certain div, to change/cycle through an array of images using the following code.

It works as supposed, only thing I have no idea is how to implement a fade in/fade out transition effect when it comes to JS. I tried it in CSS, but it only affects the very first load of the first image, when the whole page is loaded.

Can you please help me implement a fade transition between images?

function displayNextImage() {
    x = (x === images.length - 1) ? 0 : x + 1;
    document.getElementById("slider-background").src = images[x];
}
function changeImage() {
    setInterval(displayNextImage, 5000);
}

var images = [], x = -1;
images[0] = "https://www.example.com/1.jpg";
images[1] = "https://www.example.com/2.jpg";
images[2] = "https://www.example.com/3.jpg";

答案1

得分: 2

好的,以下是翻译好的内容:

首先,你需要更新你的CSS,添加这个id:

#slider-background {
  transition: opacity 1s ease-in-out;
  opacity: 1;
}

接下来,你需要在你的JavaScript文件中添加一些内容。我已经对每一部分进行了注释,希望能够理解。

function displayNextImage() {
  var element = document.getElementById("slider-background");
  // 淡出当前图片
  element.style.opacity = 0;
  // 等待淡出过渡完成,然后更换图片并淡入
  setTimeout(function() {
    x = (x === images.length - 1) ? 0 : x + 1;
    element.style.backgroundImage = "url('" + images[x] + "')";
    element.style.opacity = 1;
  }, 1000); // 这个持续时间应该与你的CSS中的过渡持续时间相匹配
}

function changeImage() {
  setInterval(displayNextImage, 5000); // 更改这个时间来控制幻灯片的速度
}

var images = [], x = -1;
images[0] = "https://www.example.com/1.jpg";
images[1] = "https://www.example.com/2.jpg";
images[2] = "https://www.example.com/3.jpg";

// 不要忘记在某个地方调用changeImage函数来启动幻灯片播放
changeImage();

这段代码使得一张图片慢慢消失,然后等待一段时间,切换到新的图片,并使其慢慢出现。它确保了时间的匹配,使得过渡看起来更加平滑。

英文:

Okay so first you'll need to update your css with this id:

#slider-background {
  transition: opacity 1s ease-in-out;
  opacity: 1;
}

Next, you'll need to add quite some stuff in ur javascript file. I commented everything so I hope it's understandable.

function displayNextImage() {
  var element = document.getElementById("slider-background");
  // Fade out the current image
  element.style.opacity = 0;
  // Wait for the fade out transition, then change the image and fade it back in
  setTimeout(function() {
    x = (x === images.length - 1) ? 0 : x + 1;
    element.style.backgroundImage = "url('" + images[x] + "')";
    element.style.opacity = 1;
  }, 1000); // This duration should match the transition duration in your CSS
}

function changeImage() {
  setInterval(displayNextImage, 5000); // Change this time to control the speed of the slide
}

var images = [], x = -1;
images[0] = "https://www.example.com/1.jpg";
images[1] = "https://www.example.com/2.jpg";
images[2] = "https://www.example.com/3.jpg";

// Don't forget to call the changeImage function somewhere to start the slideshow
changeImage();

The code makes a picture slowly vanish, then waits a bit, switches to a new picture, and makes that picture slowly appear. It makes sure the timing matches up so it looks smooth.

答案2

得分: 0

其他解决方案(包括使用不透明度和预加载图像):

let images = [], x = 0;
images[0] = "https://via.placeholder.com/100/FF0000/FFFFFF/?text=Image_1:.*";
images[1] = "https://via.placeholder.com/100/0000FF/FFFFFF/?text=Image_2:.*";
images[2] = "https://via.placeholder.com/100/00FF00/000000/?text=Image_3:.*";

function displayNextImage() {
    const image = document.getElementById("slider-background");
    x = (x === images.length - 1) ? 0 : x + 1;

    if (!image.classList.contains('show') || !image.classList.contains('showhide')) {
        image.classList.add('hide');
    }

    image.classList.replace('show', 'hide');
    setTimeout(() => {
        image.src = images[x];
        setTimeout(() => {
            image.classList.replace('hide', 'show');
        }, 400);
    }, 400);
}

function changeImage() {
    setInterval(displayNextImage, 5000);
}

changeImage();
.show {
    transition: opacity 400ms;
    opacity: 1;
}
.hide {
    opacity: 0;
    transition: opacity 400ms;
}
<link rel="preload" as="image" href="https://via.placeholder.com/100/FF0000/FFFFFF/?text=Image_1:.*">
<link rel="preload" as="image" href="https://via.placeholder.com/100/0000FF/FFFFFF/?text=Image_2:.*">
<link rel="preload" as="image" href="https://via.placeholder.com/100/00FF00/000000/?text=Image_3:.*">

<img src="https://via.placeholder.com/100/FF0000/FFFFFF/?text=Image_1:.*" alt="" id="slider-background" />
英文:

Other solution (include using opacity and preloading images):

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

let images = [], x = 0;
images[0] = &quot;https://via.placeholder.com/100/FF0000/FFFFFF/?text=Image_1:.*&quot;;
images[1] = &quot;https://via.placeholder.com/100/0000FF/FFFFFF/?text=Image_2:.*&quot;;
images[2] = &quot;https://via.placeholder.com/100/00FF00/000000/?text=Image_3:.*&quot;;
    
function displayNextImage() {
    const image = document.getElementById(&quot;slider-background&quot;);
    x = (x === images.length - 1) ? 0 : x + 1;
    
    if (!image.classList.contains(&#39;show&#39;) || !image.classList.contains(&#39;showhide&#39;)) {
        image.classList.add(&#39;hide&#39;);
    }
    
    image.classList.replace(&#39;show&#39;, &#39;hide&#39;);
    setTimeout(() =&gt; {
        image.src = images[x];
        setTimeout(() =&gt; {
            image.classList.replace(&#39;hide&#39;, &#39;show&#39;);
        }, 400);
    }, 400);
}
    
function changeImage() {
    setInterval(displayNextImage, 5000);
}
    
changeImage();

<!-- language: lang-css -->

.show {
    transition: opacity 400ms;
    opacity: 1;
}
.hide {
    opacity: 0;
    transition: opacity 400ms;
}

<!-- language: lang-html -->

&lt;link rel=&quot;preload&quot; as=&quot;image&quot; href=&quot;https://via.placeholder.com/100/FF0000/FFFFFF/?text=Image_1:.*&quot;&gt;
&lt;link rel=&quot;preload&quot; as=&quot;image&quot; href=&quot;https://via.placeholder.com/100/0000FF/FFFFFF/?text=Image_2:.*&quot;&gt;
&lt;link rel=&quot;preload&quot; as=&quot;image&quot; href=&quot;https://via.placeholder.com/100/00FF00/000000/?text=Image_3:.*&quot;&gt;
    
&lt;img src=&quot;https://via.placeholder.com/100/FF0000/FFFFFF/?text=Image_1:.*&quot; alt=&quot;&quot; id=&quot;slider-background&quot; /&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月9日 16:48:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定