onMouseover 改变图像并播放声音,onMouseout 重置图像恢复正常

发布于 2024-11-06 19:42:52 字数 89 浏览 1 评论 0原文

我正在玩 HTML 中的某些内容,我可以在鼠标悬停图像时播放声音,并在鼠标停留在该位置时更改图像吗?

总体开发者观点会很棒!

I'm playing with something in HTML, can I play a sound on mouseover of an image as well as changing the image while the mouse remains there?

Overall developer view would be great!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫竹語嫣☆ 2024-11-13 19:42:52

是的,但是您需要 JavaScript 来为您完成很多工作。

对于图像,您可以有两个操作:

<img id="imgID" scr="img/image.jpg" onmouseover="imgover();" onmouseout="imgout();" />

然后您的两个函数将执行您想要执行的操作:

imgover () {
    document.getElementByID("imgID").scr = "img/newimage.jpg";
    //Code to play sound
}
imgout () {
    document.getElementByID("imgID").scr = "img/image.jpg";
    //Code to stop sound
}

您也需要声音方面的帮助吗?

Yes, but you will need JavaScript to do a lot of this for you.

For the image, you can have two actions:

<img id="imgID" scr="img/image.jpg" onmouseover="imgover();" onmouseout="imgout();" />

Then your two functions will do what you are looking to do:

imgover () {
    document.getElementByID("imgID").scr = "img/newimage.jpg";
    //Code to play sound
}
imgout () {
    document.getElementByID("imgID").scr = "img/image.jpg";
    //Code to stop sound
}

Do you need help with the sound too?

牵强ㄟ 2024-11-13 19:42:52

变化的图像应该用CSS而不是Javascript来处理,声音的播放应该用Javascript来处理。

HTML:

<div class="hover_sound"></div>

CSS:

.hover_sound {
    background: url(image.png) no-repeat;
    height: 200px;
    width: 200px;
}

.hover_sound:hover { url(image_hover.png) no-repeat}

Javascript:

声音播放应由 Javascript 处理,您可以使用支持 MP3 的 HTML5 兼容浏览器来执行此操作( Chrome、Safari、IE9),并具有以下功能:

;(function() {

    // get the hover element and instantiate sound.
    var hoverSoundElement = document.querySelector(".hover_sound")
      , sound = new Audio("mysound.mp3")

    // play the sound when the mouse enters.
    hoverSoundElement.addEventListener("mouseover", sound.play.bind(sound))

    hoverSoundElement.addEventListener("mouseout", function() {

        // stop the sound when the mouse leaves.
        sound.pause()

        // reset sound's position to the beginning.
        sound.currentTime = 0
    })

})()

如果您想支持较旧的浏览器,请使用 jQuery 或 MooTools 之类的工具来处理 DOM 内容,如果您想通过声音支持较旧的浏览器,请使用 SoundManager2 (http://www.schillmania.com/projects/soundmanager2/)或使用多个音频源以获得更好的效果支持,例如 OGG 和 MP3 源。

这里的“正确”答案在实现方面是正确的,但您应该尝试尽可能地分离您的关注点,并且切换图像是一个样式问题,可以通过 CSS 而不是 Javascript 轻松处理。

The changing image should be handled with CSS rather than Javascript, and the sound playing should be handled in Javascript.

HTML:

<div class="hover_sound"></div>

CSS:

.hover_sound {
    background: url(image.png) no-repeat;
    height: 200px;
    width: 200px;
}

.hover_sound:hover { url(image_hover.png) no-repeat}

Javascript:

The sound playing should be handled by Javascript, and you can do that with HTML5-compliant browsers that support MP3 (Chrome, Safari, IE9) with the following:

;(function() {

    // get the hover element and instantiate sound.
    var hoverSoundElement = document.querySelector(".hover_sound")
      , sound = new Audio("mysound.mp3")

    // play the sound when the mouse enters.
    hoverSoundElement.addEventListener("mouseover", sound.play.bind(sound))

    hoverSoundElement.addEventListener("mouseout", function() {

        // stop the sound when the mouse leaves.
        sound.pause()

        // reset sound's position to the beginning.
        sound.currentTime = 0
    })

})()

If you want to support older browsers, then use something like jQuery or MooTools for the DOM stuff and if you want to support older browsers with your sound, use SoundManager2 (http://www.schillmania.com/projects/soundmanager2/) or use multiple audio sources for better support, e.g. OGG and MP3 sources.

The "correct" answer here is right in terms of implementation, but you should try to separate your concerns as much as possible, and switching out an image is a style concern that can easily be handled by CSS instead of Javascript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文