我使用onmousedown事件创建了一个按钮,该事件在单击时循环播放音轨。我正在尝试在单击相同按钮时结束音频循环。我尝试了onmouseup活动,但没有运气,任何想法? (我是JavaScript的新手)
<script> var one = new Audio(); one.src = "files/audio/one.wav"; one.loop = true; </script>
<img class="item" src="files/button.png" onmousedown="one.play()" onmouseup="one.stop()"/>
答案 0 :(得分:1)
尝试使用var
跟踪您的点击次数,只收听click
事件:
<强> JS 强>
var one = new Audio();
one.src = "files/audio/one.wav";
one.loop = true;
var isPlaying = false;
function manage(){
if(isPlaying){
one.pause();
one.currentTime = 0;
isPlaying = false;
}
else{
one.play();
isPlaying = true;
}
}
<强> HTML 强>
<img class="item" src="files/button.png" onclick="manage()"/>