gpt4 book ai didi

javascript - 鼠标悬停时发出声音,鼠标悬停时停止

转载 作者:行者123 更新时间:2023-12-03 01:39:44 25 4
gpt4 key购买 nike

我正在开发一个项目,当您将鼠标悬停在图像上并在鼠标移出时停止播放时会播放声音。我希望它每次都重新开始,并且每页上必须有多个图像。我正在寻找使此代码工作的最有效方法。这是示例页面:http://inventivewebdesign.com/audio-img/sound.html

代码:

<body>
<script>
function PlaySound(soundobj) {
var thissound=document.getElementById(soundobj);
thissound.play();
}

function StopSound(soundobj) {
var thissound=document.getElementById(soundobj);
thissound.pause();
thissound.currentTime = 0;
}
</script>


<a onmouseover="PlaySound('violin')" onmouseout="StopSound('violin')"><img src="violin.png"></a>

<a onmouseover="PlaySound('xy')" onmouseout="StopSound('xy')"><img src="xy.png" width="300px" height="300px"></a>

<a onmouseover="PlaySound('piccolo')" onmouseout="StopSound('piccolo')"><img src="piccolo.png"></a>

<audio id='piccolo' src='piccolo.wav'/>
<audio id='violin' src='violin.mp3'/>
<audio id='xy' src='xy.mp3'/>

</body>

这一切都有效。我唯一的问题是如何让代码更有效率。
我看到我应该能够做这样的事情:
<audio>
<source id='piccolo' src='piccolo.wav'>
<source id='violin' src='violin.mp3'>
<source id='xy' src='xy.mp3'>
</audio>

但它不起作用。上面的第一组代码是最好的方法吗?

最佳答案

好问题@MattM。我只需添加一个函数来创建音频悬停的新实例,从而更容易实现更多声音而无需重复代码。

function newHover(e){
var audio = new Audio(); // creates audio element
audio.src = e.audio; // the audios location
var image = new Image(); //creates the image element
image.src = e.image; // the images location
image.className = e.title; // applies the name of the instrument to both elements
audio.className = e.title;
image.addEventListener("mouseover", PlaySound); // listens for mouse movement
image.addEventListener("mouseout", StopSound);
document.body.appendChild(audio); // appends the audio and image to the body of the document
document.body.appendChild(image);
}

这样,下面的代码块就不需要了:
<a onmouseover="PlaySound('violin')" onmouseout="StopSound('violin')">
<img src="violin.png">
</a>

<a onmouseover="PlaySound('xy')" onmouseout="StopSound('xy')">
<img src="xy.png" width="300px" height="300px">
</a>

<a onmouseover="PlaySound('piccolo')" onmouseout="StopSound('piccolo')">
<img src="piccolo.png">
</a>

<audio id='piccolo' src='piccolo.wav'/>
<audio id='violin' src='violin.mp3'/>
<audio id='xy' src='xy.mp3'/>

在您的代码中实现这一点,此解决方案中不需要上述任何 HTML。

(注意:在 PlaySound 和 PauseSound 函数中,getElementById 改为 querySelector)

function PlaySound(e) {
var thissound = document.querySelector("audio." + e.target.className);
thissound.play();
}
function StopSound(e) {
var thissound = document.querySelector("audio." + e.target.className);
thissound.pause();
thissound.currentTime = 0;
}
function newHover(e){
var audio = new Audio();
audio.src = e.audio;
var image = new Image();
image.src = e.image;
image.className = e.title;
audio.className = e.title;
image.addEventListener("mouseover", PlaySound);
image.addEventListener("mouseout", StopSound);
document.body.appendChild(audio);
document.body.appendChild(image);
}


window.onload = function(){
newHover({
audio: "http://inventivewebdesign.com/audio-img/piccolo.wav", // Path to the audio file (the src property of audio tag)
image: "http://inventivewebdesign.com/audio-img/piccolo.png", // Path to the image file (the src property of the image tag)
title: "piccolo" // The name of the instrument (applied as a className to both the image and the audio elements)
});
newHover({
audio: "http://inventivewebdesign.com/audio-img/xy.mp3",
image: "http://inventivewebdesign.com/audio-img/xy.png",
title: "xy"
});
newHover({
audio: "http://inventivewebdesign.com/audio-img/violin.mp3",
image: "http://inventivewebdesign.com/audio-img/violin.png",
title: "violin"
});
}
a{
position: absolute;
}
img{
width: 100px;
}
<body>
<a>Hover over the images!</a>
</body>


总之,您的解决方案有效地工作,但是它会使您更容易拥有为您创建音频悬停的功能。

我查看了 creativewebdesign.com,那里的网站非常好!

编辑>>>>>>>>>>>>

注意 - 我已经在 Firefox 和 Chrome 中测试了该脚本。它应该可以正常工作。

有关如何使用该功能的说明:

插入图像。调用 newHover 函数像这样:
    newHover({
audio: /*audio src here eg. music.wav*/,
image: /*image src here eg. picture.png*/,
title: /*title here eg. violin / xylophone / piccolo*/
});

图像将被插入到文档中。然后,您可以应用 css 样式或对它们执行任何其他操作。

关于javascript - 鼠标悬停时发出声音,鼠标悬停时停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48372676/

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