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

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

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代码:

#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-2.html
匿名

发表评论

匿名网友

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

确定