javascript - 单击时如何在两个图像之间进行转换?

标签 javascript html css image

我有一张图像,点击后会发生变化。我通过在单击时更改图像的 src 来做到这一点。我试图在单击时在两个图像之间添加平滑过渡。

我尝试将两个图像放在彼此的顶部,并在单击时更改顶部图像的不透明度,但我无法弄清楚如何在 JavaScript 中更改不透明度,所以我放弃了这个想法,以防万一更简单的方法。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

<style>
.slidecontainer {
  width: 100%;
  opacity: 0;
  transition: opacity .3s cubic-bezier(.4, 0, .2, 1);
}

.slider {
  -webkit-appearance: none;
  width: 100px;
  height: 15px;
  border-radius: 5px;  
  background: #d3d3d3;
  outline: none;
  -webkit-transition: .2s;
  transition: opacity .2s;
  position: relative;
}



.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 25px;
  height: 25px;
  border-radius: 50%;
  background: #4CAF50;
  cursor: pointer;
}

.slider::-moz-range-thumb {
  width: 25px;
  height: 25px;
  border-radius: 50%;
  background: #4CAF50;
  cursor: pointer;
}

.slidecontainer:hover, img:hover + .slidecontainer {
  opacity: 1;
}
</style>

<img id="Lightning" src="https://static.wixstatic.com/media/38d0c2_1f8b71c0bf644137ab295604ceaaecaa~mv2.png" height="100" width="100">

<div class="slidecontainer">
  <input type="range" min="0" max="100" value="100" class="slider" id="volume">
</div>

<audio id="Thunder" loop>
   <source src="http://music.wixstatic.com/preview/38d0c2_064b409cb5594774ab3c1fa24f9afa2f-128.mp3" type="audio/wav" />
</audio>

<script type="text/javascript">

function LightningChange() {
if (Lightning.src == "https://static.wixstatic.com/media/38d0c2_1f8b71c0bf644137ab295604ceaaecaa~mv2.png") {
    Lightning.src = "https://static.wixstatic.com/media/38d0c2_5609c3592b894208b679ce8641031ae8~mv2.png"; 
} else {
    Lightning.src = "https://static.wixstatic.com/media/38d0c2_1f8b71c0bf644137ab295604ceaaecaa~mv2.png";
}}

document.getElementById("Lightning").onclick = function() {
    LightningChange()
    var thunderAudio = document.getElementById("Thunder");
    if (thunderAudio.paused) thunderAudio.play();
    else thunderAudio.pause();
};

volume.addEventListener("mousemove", thunderVolume);

function thunderVolume(){
    document.getElementById("Thunder").volume = document.getElementById("volume").value / 100;
}
    </script>

最佳答案

我将两个图像放在一起并在单击时更改其样式

<style>
    .imageContainer{
         position: absolute;
     }

.hide{
  visibility: hidden;
  opacity: 0;
  transition: opacity .5s, visibility .5s;
}

.visible{
  visibility: visible;
  opacity: 1;
}
</style>


<div id="Lightning" class="container">
    <img class="imageContainer visible" src="https://static.wixstatic.com/media/38d0c2_1f8b71c0bf644137ab295604ceaaecaa~mv2.png" height="100" width="100">
    <img id="secondImg" class="imageContainer hide"   src="https://static.wixstatic.com/media/38d0c2_5609c3592b894208b679ce8641031ae8~mv2.png" height="100" width="100">
</div>


<script type="text/javascript">
document.getElementById("Lightning").onclick = function() {
   document.getElementById("secondImg").classList.add("visible");
};
</script>

关于javascript - 单击时如何在两个图像之间进行转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56601382/

相关文章:

javascript - 忽略 "style"属性获取 div 的宽度和高度

css - 当一个 div 没有扩展以适应它的内容时该怎么办,并且 clear-fixes 不起作用?

javascript - 如何隐藏 javascript window.open 地址栏/位置栏?

javascript - getCurrentPosition() 在 Firefox 中不起作用

html - iPad 3 Webapp 在移动版 Safari 中崩溃

javascript - Popup从底部向上滑动溢出其他div block

javascript - 如何在不离开页面的情况下下载图片?

javascript - jQuery 中的 ASP 风格转发器

javascript - 将 rem 转换为 px 而不回流

javascript - 如何从 Ionic 4 中的 TypeScript 文件调用 iframe 显示的网页中的 JavaScript 函数?