Javascript 单击时如何在两个图像之间转换?
我有一个图像,点击时会发生变化。我通过在单击时更改图像的src来实现这一点。我试图在单击时在两个图像之间添加平滑过渡 我曾尝试将两个图像放置在彼此的顶部,并在单击时更改顶部图像的不透明度,但我无法确定如何在JavaScript中更改不透明度,因此我放弃了这个想法,以防有更简单的方法Javascript 单击时如何在两个图像之间转换?,javascript,html,css,image,Javascript,Html,Css,Image,我有一个图像,点击时会发生变化。我通过在单击时更改图像的src来实现这一点。我试图在单击时在两个图像之间添加平滑过渡 我曾尝试将两个图像放置在彼此的顶部,并在单击时更改顶部图像的不透明度,但我无法确定如何在JavaScript中更改不透明度,因此我放弃了这个想法,以防有更简单的方法 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initia
<!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>
.slidecontainer{
宽度:100%;
不透明度:0;
过渡:不透明度.3s立方贝塞尔(.4,0,2,1);
}
.滑块{
-webkit外观:无;
宽度:100px;
高度:15px;
边界半径:5px;
背景#d3;
大纲:无;
-webkit转换:.2s;
转变:不透明度;
位置:相对位置;
}
.滑块::-webkit滑块拇指{
-webkit外观:无;
外观:无;
宽度:25px;
高度:25px;
边界半径:50%;
背景#4CAF50;
光标:指针;
}
.滑块::-moz范围拇指{
宽度:25px;
高度:25px;
边界半径:50%;
背景#4CAF50;
光标:指针;
}
.slidecontainer:悬停,img:悬停+.slidecontainer{
不透明度:1;
}
函数LightningChange(){
如果(Lightning.src==”https://static.wixstatic.com/media/38d0c2_1f8b71c0bf644137ab295604ceaaecaa~mv2.png){
Lightning.src=”https://static.wixstatic.com/media/38d0c2_5609c3592b894208b679ce8641031ae8~mv2.png”;
}否则{
Lightning.src=”https://static.wixstatic.com/media/38d0c2_1f8b71c0bf644137ab295604ceaaecaa~mv2.png”;
}}
document.getElementById(“Lightning”).onclick=function(){
LightningChange()
var thunderAudio=document.getElementById(“Thunder”);
如果(thunderAudio.paused)thunderAudio.play();
else thunderAudio.pause();
};
volume.addEventListener(“mousemove”,thunderVolume);
函数卷(){
document.getElementById(“Thunder”).volume=document.getElementById(“volume”).value/100;
}
我将两幅图像放在一起,然后单击更改其样式
.imageContainer{
位置:绝对位置;
}
.隐藏{
可见性:隐藏;
不透明度:0;
转换:不透明度.5s,可见性.5s;
}
.可见{
能见度:可见;
不透明度:1;
}
document.getElementById(“Lightning”).onclick=function(){
document.getElementById(“secondImg”).classList.add(“可见”);
};
您不能在src
中转换更改,因为它们不是数值。可能是重复的