onMouseover 改变图像并播放声音,onMouseout 重置图像恢复正常
我正在玩 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,但是您需要 JavaScript 来为您完成很多工作。
对于图像,您可以有两个操作:
然后您的两个函数将执行您想要执行的操作:
您也需要声音方面的帮助吗?
Yes, but you will need JavaScript to do a lot of this for you.
For the image, you can have two actions:
Then your two functions will do what you are looking to do:
Do you need help with the sound too?
变化的图像应该用CSS而不是Javascript来处理,声音的播放应该用Javascript来处理。
HTML:
CSS:
Javascript:
声音播放应由 Javascript 处理,您可以使用支持 MP3 的 HTML5 兼容浏览器来执行此操作( Chrome、Safari、IE9),并具有以下功能:
如果您想支持较旧的浏览器,请使用 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:
CSS:
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:
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.