如何使用原生JavaScript实现图像在转换效果下切换到另一个图像?

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

How to make an image change to another one with a transition effect using vanilla Javascript?

问题

在我正在创建的网站上,我有一些手风琴,每个手风琴的顶部都有一张图片。当你打开手风琴时,里面有一个描述,并且图片切换到另一张。然后当你关闭手风琴时,图片切换回第一张。

问题是,我希望这个图片切换具有平滑的过渡效果,带有淡入淡出效果,但现在只是突然切换。我该如何做呢?

假设手风琴按钮的id为"button";包含第一张图片(将切换到第二张图片)的标签的id为"firstimage"。

这是JavaScript中的代码:

let counter = 1;

let button = document.querySelector("#button");
button.addEventListener("click", function () {
  let image = document.querySelector("#firstimage");
  image.setAttribute(
    "src",
    "(第一张图片的URL)"
  );
  counter++;
  if (counter > 2) {
    counter = 1;
    image.setAttribute(
      "src",
      "(第二张图片的URL)"
    );
  }
});

也许这是我应该在CSS中编辑的内容?我已经尝试在CSS中为第一张图片添加过渡效果(transition: all 360ms ease-in-out),但没有效果。

英文:

on a website I'm creating I have some accordions, and each of them has an image at the top. When you open the accordion, there's a description inside, and the image changes to another one. Then when you close the accordion, the image switches back to the first one.
The problem is that I'd like this image change to have a smooth transition, with a fade effect, and right now it's just an abrupt change. How can I do it?

Let's say that the accordion button has an id of "button"; and the <img> tag that contains the first image (which will change to the second image) has an id of "firstimage".

This is the code in Javascript:

let counter = 1;

let button = document.querySelector(&quot;#button&quot;);
button.addEventListener(&quot;click&quot;, function () {
  let image = document.querySelector(&quot;#firstimage&quot;);
  image.setAttribute(
    &quot;src&quot;,
    &quot;(url of the first image)&quot;
  );
  counter++;
  if (counter &gt; 2) {
    counter = 1;
    image.setAttribute(
      &quot;src&quot;,
      &quot;(url of the second image)&quot;
    );
  }
});

Maybe this is something I should edit in the CSS? I already tried adding a transition to the first image in CSS ( transition: all 360ms ease-in-out ) but it didn't work.

答案1

得分: 2

你只需将两张图像叠放在一起并设置不透明度。

<div class="wrapper">
  <img src="http://placekitten.com/200/300" />
  <img src="http://placekitten.com/g/200/300" />
</div>
.wrapper {
  position: relative;
  display: inline-block;
}

.wrapper > img + img {
  position: absolute;
  left: 0;
  opacity: 0;
  transition: opacity 2s ease-in-out;
}

.wrapper.active > img + img {
  opacity: 1;
  transition: opacity 2s ease-in-out;
}
document.querySelector('.wrapper').addEventListener("click", function (e) {
  e.currentTarget.classList.toggle("active");
});
英文:

You can just put two images on top of one another and set the opacity.

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

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

document.querySelector(&#39;.wrapper&#39;).addEventListener(&quot;click&quot;, function (e) {
  e.currentTarget.classList.toggle(&quot;active&quot;);
});

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

.wrapper {
  position: relative;
  display: inline-block;
}

.wrapper &gt; img + img {
  position: absolute;
  left:0;
  opacity: 0;
  transition: opacity 2s ease-in-out;
}

.wrapper.active &gt; img + img {
  opacity: 1;
  transition: opacity 2s ease-in-out;
}

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

&lt;div class=&quot;wrapper&quot;&gt;
  &lt;img src=&quot;http://placekitten.com/200/300&quot; /&gt;
  &lt;img src=&quot;http://placekitten.com/g/200/300&quot; /&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月14日 04:33:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440890.html
匿名

发表评论

匿名网友

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

确定