gpt4 book ai didi

javascript - 声音和图像

转载 作者:行者123 更新时间:2023-12-03 02:25:20 24 4
gpt4 key购买 nike

当我单击图像 1 (001.png) 时,我希望播放音频 1 (001.mp3),图像 2/音频 2 等也同样如此。但是,如何才能使单击图像 1 时单击图像 2 时仍会播放音频。我只希望用户只需单击图像,音频就会播放,然后单击另一个图像,并且在第一个音频播放时,不同的音频仍然会播放,但是现在我只需要首先单击的任何图像都会播放。

     <!DOCTYPE html>
<html lang="en">
<!-- <audio src="${ ../_assets/images/001.mp3 }" id="audio"></audio>
<i class="glyphicon glyphicon-play-circle b-play" id="play" onclick="play()"></i> -->


<head>


<meta charset="UTF-8">
<!-- <script src="scripts.js"></script> -->
<link rel="stylesheet" href="style.css">
<title>Draft</title>


<style>
.wrapper {
width:1000px;
margin-right:auto;
margin-left:auto;

}


body {
background-image: url("../_assets/videos/giphy.gif");
background-color: #cccccc;
background-repeat: no-repeat;
background-size: 100%;
}


img {
opacity: 0.5;
filter: alpha(opacity=75); /* For IE8 and earlier */
border-radius: 50%;
-webkit-transition: -webkit-transform .8s ease-in-out;
transition: transform .8s ease-in-out;
}
img:hover {
opacity:1.0;
filter: alpha(opacity=100); /* For IE8 and earlier */
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}


img: visted {
opacity: 0.5;
filter: alpha(opacity=75);
}
</style>

</head>
<body>

<div class="wrapper">


<!-- <img src="../_assets/images/001.png" height="20%" width="20%"> -->
</div>




</div>


<body>

<script>
function play(){
var audio = document.getElementById("audio");
audio.play();
}

function play(){
var audio = document.getElementById("audio2");
audio.play();
}




</script>
<div class="purple">
<img src="../_assets/images/001.png" height="20%" width="20%" value="PLAY" onclick="play()">
<audio id="audio" src=" ../_assets/images/001.mp3" > </audio>

<br>
</div>

<div class="purple">

<img src="../_assets/images/002.png" height="20%" width="20%" value="PLAY" onclick="play()">
<audio id="audio2" src=" ../_assets/images/002.mp3" > </audio>


</body>
</body>
</html>

最佳答案

更改您的功能:play()如下:

<script>
function play(element){
var audio = element.nextElementSibling;
audio.play();
}
</script>

然后,您需要传递 DOM 的元素。

<img src="../_assets/images/002.png"  height="20%" width="20%"  value="PLAY"  onclick="play(this)">
<audio id="audio2" src=" ../_assets/images/002.mp3" > </audio>

由于使用了nextElementSibling属性,audio标签必须是img的下一个标签。

关于javascript - 声音和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48976927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com